1. 程式人生 > >北理工計算機復試上機 2009

北理工計算機復試上機 2009

div space ostream 設計 ron nbsp void begin ++

1.請輸入字符串,最多輸入4個字符串,要求後輸入的字符串排在前面,例如
  輸入:EricZ
  輸出:1=EricZ
  輸入:David
  輸出:1=David 2=EricZ

 1 /**
 2 1.請輸入字符串,最多輸入4個字符串,要求後輸入的字符串排在前面,例如
 3 輸入:EricZ
 4 輸出:1=EricZ
 5 輸入:David
 6 輸出:1=David 2=EricZ
 7 */
 8 #include<iostream>
 9 #include<list>
10 #include<string>
11 
12 using namespace std;
13 14 int main(){ 15 list<string> l; 16 string s; 17 int i=0; 18 list<string>::iterator x; 19 cout<<"請輸入至多4個字符串,輸入00結束"<<endl; 20 int j; 21 while(cin>>s){ 22 if(s=="00")break; 23 i++; 24 if(i==4)break; 25 l.push_front(s);
26 for(x=l.begin(),j=1;x!=l.end();x++,j++){ 27 cout<<j<<" "<<(*x)<<" "; 28 } 29 } 30 return 0; 31 }//main

2.把上述最後結果保存到Name.txt中

 1 /**
 2 1.請輸入字符串,最多輸入4個字符串,要求後輸入的字符串排在前面,例如
 3 輸入:EricZ
 4 輸出:1=EricZ
 5 輸入:David
 6 輸出:1=David 2=EricZ
 7 2.
 8 把上述最後結果保存到Name.txt中
9 */ 10 #include<iostream> 11 #include<list> 12 #include<string> 13 #include<fstream> 14 15 using namespace std; 16 17 int main(){ 18 list<string> l; 19 string s; 20 int i=0; 21 list<string>::iterator x; 22 cout<<"請輸入至多4個字符串,輸入00結束"<<endl; 23 int j; 24 ofstream fs; 25 fs.open("Name.txt"); 26 27 while(cin>>s){ 28 if(s=="00")break; 29 i++; 30 if(i==4)break; 31 l.push_front(s); 32 33 for(x=l.begin(),j=1;x!=l.end();x++,j++){ 34 cout<<j<<" "<<(*x)<<" "; 35 } 36 } 37 for(x=l.begin(),j=1;x!=l.end();x++,j++){ 38 fs<<(*x)<<endl; 39 } 40 return 0; 41 }//main

3.先輸入一組數,然後輸入其分組,按照分組統計出現次數並輸出
例如,輸入數據3,2,3,8,8,2,3 輸入對應分組1,2,3,2,1,3,1
輸出:
1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}
即每組中各數據出現的次數(抄的,絕妙的設計,高手)

 1 #include<iostream>
 2 #include<deque>
 3 #include<map>
 4 using namespace std;
 5 void show(map<int,int>a)
 6 {
 7     map<int,int>::iterator i;
 8     for(i=a.begin(); i!=a.end(); i++)
 9         cout<<i->first<<"="<<i->second<<" ";
10 }
11 main()
12 {
13     int    a;
14     deque<int> arr1,arr2;
15     map<int,map<int,int>>    mmap;
16     cout<<"請輸入數據"<<endl;
17     while(cin>>a)
18     {
19         if(a==0)break;
20         arr1.push_front(a);
21     }
22     cout<<"請輸入分組"<<endl;
23     while(cin>>a)
24     {
25         if(a==0)break;
26         arr2.push_front(a);
27     }
28     deque<int>::iterator  it1,it2;
29     for(it1=arr1.begin(),it2=arr2.begin(); it1!=arr1.end(); it1++,it2++)
30         ((mmap[*it2])[*it1])++;
31     map<int,map<int,int>>::iterator    i;
32     for(i=mmap.begin(); i!=mmap.end(); i++)
33     {
34         cout<<i->first<<"={";
35         show(i->second);
36         cout<<"}"<<endl;
37     }
38 }

北理工計算機復試上機 2009