POJ 1552 Doubles (C++ STL set使用)
題目:
題意:題意:給出幾個正數(2~15個),然后就是求有這些數字的2倍有沒有和原先的正數相同的,求出有幾個,沒有就是0.
分析:水題。用數組解決,開一個數組存正數,另開一個數組用來存這些數的2倍,接著就搜索,然后注意一下結束的時候怎么處理就行。
?
c普通方法:
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack> 6 #include<queue> 7 #include<cmath> 8 #include<algorithm> 9 using namespace std; 10 11 int main() 12 { 13 int i,j,a[1000],b[1000],sum,t,x; 14 while(cin>>t&&t!=-1) 15 { 16 sum=0; 17 x=0; 18 a[x]=t; b[x++]=2*t; 19 while(cin>>t&&t) 20 { 21 a[x]=t; b[x++]=2*t; 22 } 23 for(i=0; i<x; i++) 24 for(j=0; j<x; j++) 25 { 26 if(b[i]==a[j]) 27 { 28 sum++; 29 break; 30 } 31 } 32 cout<<sum<<endl; 33 } 34 return 0; 35 }
?
STL? set知識點 :http://blog.sina.com.cn/s/blog_779cf3410101389s.html?
http://blog.sina.com.cn/s/blog_6fe0eb190100s7qn.html
http://blog.sina.com.cn/s/blog_60e96a410100g0k9.html
http://blog.csdn.net/lansetiankong_yiyi/article/details/5816362
?
//set集合容器,實際是一棵樹,每棵子樹的左結點小于根節點的值,
//而根節點的值小于右節點的值,整棵樹可以用中序遍歷得到一個
不允許元素重復, 如果有重復元素用multiset,multiset的用法與set類似。
) 不能直接改變元素值,因為那樣會打亂原本正確的順序,要改變元素值必須先刪除舊元素,則插入新元素
2) 不提供直接存取元素的任何操作函數,只能通過迭代器進行間接存取,而且從迭代器角度來看,元素值是常數
3) 元素比較動作只能用于型別相同的容器(即元素和排序準則必須相同)
默認情況下,set元素按其所屬型別的less-than進行排列,?
默認從小到大排序。?
1 #include<iostream> 2 #include<set> 3 using namespace std; 4 int main() 5 { 6 set<int> s; 7 set<int>::iterator t; 8 int temp; 9 cin>>temp; 10 while(temp != -1) 11 { 12 s.clear(); 13 while(temp != 0) 14 { 15 s.insert(temp); 16 cin>>temp; 17 } 18 int c = 0; 19 for(t = s.begin(); t != s.end(); t++) 20 { 21 if(s.count((*t)*2) != 0) 22 c++; 23 } 24 cout<<c<<endl; 25 cin>>temp; 26 } 27 }
?
?
?
轉載于:https://www.cnblogs.com/bfshm/p/3231116.html
總結
以上是生活随笔為你收集整理的POJ 1552 Doubles (C++ STL set使用)的全部內容,希望文章能夠幫你解決所遇到的問題。