PAT甲级1048 Find Coins

"Evaloves从宇宙各地收集硬币,包括火星。一天,她在全宇宙购物中心购物,但付款有一个特殊规则:每次付款必须精确使用两枚硬币。她面临的问题是确定对于任意金额,是否能找到两枚硬币进行精确支付。给定硬币面值和所需支付的总金额,程序需要找出是否存在这样的硬币组合。如果存在多个解决方案,输出面值较小的硬币组合。如果无法找到解决方案,则输出"NoSolution"。"
摘要由CSDN通过智能技术生成
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 10 
5
  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 (10 
5
 , the total number of coins) and M (10 
3
 , 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 V 
1and V 
2(separated by a space) such that V 
1+V 
2=M and V 
1
​
 ≤V 
2. If such a solution is not unique, output the one with the smallest V 
1. If there is no solution, output No Solution instead.

Sample Input 1:
8 15
1 2 8 7 2 4 11 15
结尾无空行
Sample Output 1:
4 11
结尾无空行
Sample Input 2:
7 14
1 8 7 2 4 11 15
结尾无空行
Sample Output 2:
No Solution
结尾无空行

要记得过滤M - coin[i]大于500的值

用例:

66249 814
310 229 308 240 161 254 83 59 319 275 242 157 461 173 486 2 300 130 93 302 248 313 175 256 186 404 328 113 51 93 481 316 49 223 274 176 225 410 158 331 18 197 43 45 387 120 377 278 98 88 88 82 41 204 299 387 222 100 13 322 391 57 76 415 299 474 270 336 53 141 336 490 158 292 457 74 200 118 278 326 474 143 251 379 291 350 130 86 35 306 180 472 433 157 70 446 349 331 494 449 319 182 358 223 169 286 211 5 444 284 435 468 367 249 245 300 360 399 91 370 186 398 251 361 339 371 238 486 101 79 428 287 455 403 361 83 477 297 78 333 241 399 151 47 475 403 293 438 209 366 289 295 251 387 159 270 306 168 431 349 199 432 188 9 62 277 384 7 237 2 269 491 32 195 261 62 477 114 97 307 329 322 37 201 177 225 145 191 92 373 461 43 26 459 201 354 494 278 126 371 168 213 50 131 15 141 82 409 178 411 402 51 257 225 180 431 63 243 411 297 247 134 184 55 363 108 253 261 368 236 148 127 6 284 333 481 44 251 160 117 143 59 181 407 26 349 142 97 107 144 98 450 250 27 461 65 492 302 433 75 246 331 129 31 208 453 439 105 110 263 191 269 75 324 464 500 271 342 466 312 429 39 91 314 71 258 413 180 310 102 467 21 133 216 101 492 186 108 224 498 362 31 305 348 71 172 363 243 56 498 2 9 91 351 257 79 17 472 447 121 325 353 284 266 499 19 12 205 353 408 269 158 221 255 482 25 498 14 433 342 117 326 23 2 386 349 414 195 47 236 280 151 457 134 249 488 221 204 81 486 90 75 462 284 24 47 479 328 99 203 306 113 199 158 204 428 15 159 431 28 43 399 462 449 236 225 138 481 363 489 407 156 305 369 476 57 443 396 170 489 47 138 235 255 220 26 42 321 353 207 296 338 284 480 176 413 466 157 24 188 306 410 128 23 431 334 175 250 282 234 347 348 363 293 298 401 2 81 421 170 286 234 436 453 372 453 464 356 196 223 99 488 46 467 50 356 255 405 328 362 310 69 147 142 211 447 417 325 223 295 152 322 171 457 394 263 7 444 431 486 423 388 415 301 498 395 93 110 306 157 35 175 271 394 362 325 172 4 272 491 498 452 271 181 46 473 268 169 181 93 28 363 239 94 281 413 397 99 133 465 185 421 1 54 22 31 92 197 51

输出:
314 500
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    int N,M;
    cin >> N >> M;
    vector<int> coin(N);
    vector<int> coin_face(501);
    for(int i = 0;i < N;++i){
        cin >> coin[i];
        coin_face[coin[i]]++;
    }
    sort(coin.begin(),coin.end());
    
    for(int i = 0;i < N;++i){
        if(coin[i] >= M||M - coin[i] > 500){
            continue;
        }
        coin_face[coin[i]]--;
        if(coin[i] <= M - coin[i]&&coin_face[M - coin[i]] != 0){
            cout << coin[i] << ' ' << M - coin[i] << endl;
            return 0;
        }
        coin_face[coin[i]]++;
    }
    
    cout << "No Solution" << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值