

#include<cstdio>
#include<algorithm>
using namespace std;
int coinHarsh[510] = { 0 };
int coins[100010];
int main() {
int N, M;
int temp;
bool solute = false;
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%d", &temp);
coins[i] = temp;
coinHarsh[temp] ++;
}
sort(coins, coins + N);
for (int i = 0; i < N; i++) {
int a = coins[i];
int b = M - a;
if (a == b && coinHarsh[a] > 1) {
printf("%d %d", a, b);
solute = true;
break;
}
if (coinHarsh[b] > 0 && coinHarsh[a] > 0) {
printf("%d %d", a, b);
solute = true;
break;
}
}
if (!solute) {
printf("No Solution");
}
return 0;
}
uj5u.com熱心網友回復:
https://blog.csdn.net/qq_36491095/article/details/104451454希望能幫到你你的問題出在輸入一個m/2時候會錯誤輸出
uj5u.com熱心網友回復:
雖然可能有10^5個硬幣,但是硬幣面額都是不超過500加載硬幣的時候, 同步記錄不同面額數值的硬幣的數量到coinHarsh陣列當中
而后只要在coinHarsh當中找到兩個不是0,而且和是M的不就可以了嗎
coins陣列還排序有什么意義呢?白浪費時間啊, 你的部分正確是不是超時呢?
coins陣列存在的意義都木有啊, 加載的程序直接統計結果到coinHarsh當中就可以了
for (int index=1; index<= 500; index++) { // 沒有面額是0的,回圈從1開始
if ( index*2 > M ) { // 超過M/2了,還沒有找到符合要求的硬幣,就不可能有解了
輸出無解,退出程式
}
if ( 0 == coinHarsh[index] ) continue; // 沒有這個面額的硬幣
if ( M == index *2 && coinHarsh[index]>1) { //兩個同面額的硬幣才行
輸出答案
}
if( coinHarsh[M - index ] >0) { // 找到答案了
輸出 index 和 M-index
結束程式
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/137796.html
標籤:新手樂園
上一篇:高手來幫忙
