51nod 1090 1267 【二分简单题】
生活随笔
收集整理的這篇文章主要介紹了
51nod 1090 1267 【二分简单题】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
做法:從左往右枚舉前兩個數的和sum,剩余的數二分找-sum是否存在。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include?<bits/stdc++.h> using?namespace?std; struct?Node?{ ????int?a,?b,?c; }temp; int?main()?{ ????int?n; ????int?a[1010]; ????scanf("%d",?&n); ????for(int?i?=?0;?i?<?n;?i++)?scanf("%d",?&a[i]); ????sort(a,?a+n); ????vector<Node>ans; ????for(int?i?=?0;?i?<?n-2;?i++)?{ ????????for(int?j?=?i+1;?j?<?n-1;?j++)?{ ????????????int?sum?=?a[i]?+?a[j]; ????????????int?pos?=?lower_bound(a+j+1,?a+n,?-sum)?-?a; ????????????if(pos?>=?n?||?a[pos]?!=?-sum)?continue; //????????????cout?<<?i?<<?'?'?<<?j?<<'?'?<<?pos?<<?endl; ????????????temp.a?=?a[i]; ????????????temp.b?=?a[j]; ????????????temp.c?=?a[pos]; ????????????ans.push_back(temp); ????????} ????} ????if(ans.size()?==?0)?{ ????????puts("No?Solution"); ????????return?0; ????} ????for(int?i?=?0;?i?<?ans.size();?i++)?{ ????????cout?<<?ans[i].a?<<?'?'?<<?ans[i].b?<<?'?'?<<?ans[i].c?<<?endl; ????} } |
?
?
?
感覺比第一題還簡單,是因為數據太弱? 三個for枚舉前三個數和sum, 二分剩余的數找-sum。
測試數據三個數sum居然不會爆int
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include?<bits/stdc++.h> using?namespace?std; int?main()?{ ????int?n; ????int?a[1010]; ????scanf("%d",?&n); ????for(int?i?=?0;?i?<?n;?i++)?scanf("%d",?&a[i]); ????sort(a,?a+n); ????for(int?i?=?0;?i?<?n-3;?i++)?{ ????????for(int?j?=?i+1;?j?<?n-2;?j++)?{ ????????????for(int?k?=?j+1;?k?<?n-1;?k++)?{ ????????????????int?sum?=?a[i]?+?a[j];?sum?+=?a[k]; ????????????????int?pos?=?lower_bound(a+k+1,?a+n,?-sum)?-?a; ????????????????if(pos?>=?n?||?a[pos]?!=?-sum)?continue; ????????????????puts("Yes"); ????????????????return?0; ????????????} ????????} ????} ????puts("No"); } |
轉載于:https://www.cnblogs.com/bestwzh/p/6636963.html
總結
以上是生活随笔為你收集整理的51nod 1090 1267 【二分简单题】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 集合——PHP实现
- 下一篇: 数据挖掘之人工神经网络BP算法