第五章例题

 5-8

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <string>
 6 
 7 
 8 using namespace std;
 9 
10 int const maxn=60;
11 
12 vector<string> stv;
13 
14 void print(string myst,unsigned len,char exch)
15 {
16     cout<<myst;
17 
18     for(unsigned i=0;i<len-myst.length();i++)
19         cout<<exch;
20 }
21 
22 
23 int main()
24 {
25     int n,dec;
26     unsigned maxlen=0;
27 
28     cin>>n;
29     dec=n;
30 
31     /*数据输入*/
32 
33     while(dec--)
34     {
35         string temp;
36 
37         cin>>temp;
38         stv.push_back(temp);
39         maxlen=max(maxlen,temp.length());
40     }
41 
42 
43     sort(stv.begin(),stv.end());
44 
45     /*计算cols和rows*/
46     print("",maxn,'-');
47 
48     int cols=(maxn-maxlen)/(maxlen+2)+1;
49     int rows=(n-1)/cols+1;
50 
51     printf("cols:%d rows:%d\n",cols,rows);
52 
53     for(int i=0;i<rows;i++)
54     {
55         for(int j=0;j<cols;j++)
56         {
57             int pos=i+j*rows;
58 
59             if(pos<n)             /*并没有排满row行col列*/
60                 print(stv[pos],j==cols-1? maxlen:maxlen+2,' ');
61         }
62         cout<<endl;
63     }
64 
65     return 0;
66 }

 

5-9 Databa

 1 #include <iostream>
 2 #include <map>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 const int maxn=1000;
10 
11 string Database[maxn][maxn];
12 map<string,int> st2in;
13 map<pair<int,int>,int> pama;
14 
15 
16 
17 int main()
18 {
19     memset(Database,0,sizeof(Database));
20     st2in.clear();
21     pama.clear();
22 
23     int m,n;
24 
25     cin>>n;
26     cin>>m;
27 
28     getchar();                              //注意输入m后'\n'留着缓冲区,要先读出来才行
29 
30     for(int i=0;i<n;i++)
31         for(int j=0;j<m;j++)
32         {
33             string temp;
34             //cin>>temp;
35             getline(cin,temp);
36 
37             //cout<<temp<<endl;
38 
39             Database[i][j]=temp;
40 
41             if(!st2in.count(temp))
42                 st2in[temp]=m*i+j;
43         }
44 
45 
46     for(int i=0;i<m;i++)
47         for(int j=i+1;j<m;j++)
48         {
49             for(int row=0;row<n;row++)
50             {
51                 string col1=Database[row][i];
52                 string col2=Database[row][j];
53 
54                 int pos1=st2in[col1];
55                 int pos2=st2in[col2];
56 
57                 if(pama.count(make_pair(pos1,pos2)))
58                 {
59                     cout<<pama[make_pair(pos1,pos2)]<<" "<<i+1<<" ";
60                     cout<<row+1<<" "<<i+1<<endl;
61 
62                     cout<<pama[make_pair(pos1,pos2)]<<" "<<j+1<<" ";
63                     cout<<row+1<<" "<<j+1<<endl;
64                 }
65                 else
66                 {
67                     pama[make_pair(pos1,pos2)]=row+1;
68                 }
69             }
70 
71             pama.clear();
72         }    
73 }

 

posted @ 2017-07-24 14:47  lan126  阅读(105)  评论(0编辑  收藏  举报