pat1048. Find Coins (25)

來源:互聯網
上載者:User

標籤:

1048. Find Coins (25)時間限制50 ms記憶體限制65536 kB代碼長度限制16000 B判題程式Standard作者CHEN, Yue

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output "No Solution" instead.

Sample Input 1:
8 151 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 141 8 7 2 4 11 15
Sample Output 2:
No Solution

提交代碼

 

 1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<string> 9 #include<map>10 #include<set>11 using namespace std;12 int line[100005];13 int main(){14     //freopen("D:\\INPUT.txt","r",stdin);15     int n,m;16     scanf("%d %d",&n,&m);17     int i,j;18     for(i=0;i<n;i++){19         scanf("%d",&line[i]);20     }21     sort(line,line+n);22 23     /*for(i=0;i<n;i++){24         cout<<i<<" "<<line[i]<<endl;25     }*/26 27     i=0;28     j=n-1;29     while(i<j){30         if(line[i]+line[j]>m){31             j--;32             continue;33         }34         if(line[i]+line[j]<m){35             i++;36             continue;37         }38         break;39     }40     if(i<j){41         printf("%d %d\n",line[i],line[j]);42     }43     else{44         printf("No Solution\n");45     }46     return 0;47 }

 

pat1048. Find Coins (25)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.