pat:1048. Find Coins (25) (double pointer method) AC

Source: Internet
Author: User

#include<stdio.h>#include<algorithm>Usingnamespace Std;int arr[100066];int main () {int n,m; scanf"%d%d", &n,&m);Forint i=0; I<n; ++i) scanf ("%d", &arr[i]); Sort (arr,arr+n);int l=0,r=n-1; //around pointer while (l<r) //"caution" There can be no "=" here, equals words to go in l=r the same coordinate cannot be 2 numbers {int tmp=arr[l]+arr[r]; if (tmp==m) //the sum of the numbers pointing to the left and right pointers is equal to M, that is to find {printf ( "%d%d" arr[l],arr[r]); return 0;} else if (tmp<m) //and less than m, left pointer back, increase TMP ++l; else //greater than M, right pointer forward, decrease TMP--r;} printf (" no solutionreturn 0;}          

pat:1048. Find Coins (25) (double pointer method) AC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.