• {purple5练习题}


    UVA 1593

    学习了重定向。。。

     1 //#define LOCAL
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 vector<string> s[1005];
     5 int len[1850];
     6 
     7 int main(){
     8 #ifdef LOCAL
     9     freopen("in.txt", "r", stdin);
    10     freopen("out.txt", "w", stdout);
    11 #endif
    12     string line,buf;
    13     int i = 0,j=0;
    14     while (getline(cin, line)){
    15         stringstream stream(line);
    16         while (stream >> buf){
    17             len[j] = max(len[j],(int)buf.length());
    18             j++;
    19             s[i].push_back(buf);
    20         }
    21         i++; j = 0;
    22     }
    23     cout << setiosflags(ios::left);
    24     
    25     for (int k = 0; k < i; k++){
    26         int l=0;
    27         for (; l < s[k].size()-1; l++){
    28             cout << setw(len[l] + 1) << s[k][l];
    29         }
    30         cout << s[k][l]<< endl;                 //每行单词最后一个单词后面不应该有空格输出
    31     }
    32     return 0;
    33 }
    View Code

    UVA 1594

    好久不刷题了,,这都写了一堆bug

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 //#define LOCAL
     4 int a[20];
     5 int main(){
     6             #ifdef LOCAL
     7                 freopen("in.txt","r",stdin);
     8                 freopen("out.txt","w",stdout);
     9             #endif // LOCAL
    10     int t,n;
    11     scanf("%d",&t);
    12     while(t--){
    13         int ok=0;
    14         scanf("%d",&n);
    15         for(int i=0;i<n;i++) scanf("%d",&a[i]);
    16         for(int i=0;i<1001;i++)
    17         {
    18             int temp=a[0];
    19             for(int j=0;j<n-1;j++) a[j]=abs(a[j]-a[j+1]);
    20             a[n-1]=abs(a[n-1]-temp);
    21         }
    22         for(int i=0;i<n;i++) if(a[i]==0) ok++;
    23 
    24         if(ok==n) puts("ZERO");
    25         else puts("LOOP");
    26     }
    27     return 0;
    28 }
    View Code

     UVA 10935

    队列queue

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 queue<int> q,ans;
     4 int main(){
     5             //#define LOCAL
     6             #ifdef LOCAL
     7                 freopen("in.txt","r",stdin);
     8                 freopen("out.txt","w",stdout);
     9             #endif // LOCAL
    10     int n;
    11     while(scanf("%d",&n)&&n)
    12     {
    13         while(!q.empty()) q.pop();
    14         while(!ans.empty()) ans.pop();
    15         for(int i=1;i<=n;i++) q.push(i);
    16         int ok=1;
    17         while(q.size()>1)
    18         {
    19             if(ok){
    20                 int temp=q.front();
    21                 q.pop();
    22                 ans.push(temp);
    23                 ok=!ok;
    24             }
    25             else
    26             {
    27                 int temp=q.front();
    28                 q.pop();
    29                 q.push(temp);
    30                 ok=!ok;
    31             }
    32         }
    33         printf("Discarded cards:");
    34         while(ans.size()>1)
    35         {
    36             printf(" %d,",ans.front());
    37             ans.pop();
    38         }
    39         if(!ans.empty())
    40         printf(" %d",ans.front());
    41         printf("
    Remaining card: %d
    ",q.front());
    42 
    43 
    44     }
    45     return 0;
    46 }
    View Code

    UVA 10763

    map

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef pair<int,int> pii;
     5 map<pii,int> m;
     6 int n,a,b;
     7 int main()
     8 {
     9    // #define LOCAL
    10     #ifdef LOCAL
    11         freopen("in.txt","r",stdin);
    12         freopen("out.txt","w",stdout);
    13     #endif // LOCAL
    14 
    15     while(scanf("%d",&n)&&n)
    16     {
    17         int cnt=0;
    18         for(int i=0;i<n;i++)
    19         {
    20             scanf("%d%d",&a,&b);
    21             pii p1(b,a);
    22             if(!m[p1])
    23             {
    24                 cnt++;
    25                 m[pii(a,b)]=1;
    26             }
    27             else cnt--;
    28         }
    29         if(cnt) puts("NO");
    30         else puts("YES");
    31     }
    32     return 0;
    33 
    34 }
    View Code

    UVA - 10391

    set

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 set<string> s;
     5 
     6 int main()
     7 {
     8     //#define LOCAL
     9     #ifdef LOCAL
    10         freopen("in.txt","r",stdin);
    11         freopen("out.txt","w",stdout);
    12     #endif // LOCAL
    13 
    14     string t;
    15     while(cin>>t) s.insert(t);
    16     set<string> ::iterator it=s.begin();
    17     for(;it!=s.end();it++)
    18     {
    19         string temp=*it;
    20         int len=temp.length();
    21         for(int i=1;i<len-1;i++)
    22         {
    23             string a=temp.substr(0,i);
    24             string b=temp.substr(i,len);
    25            // cout<<"_______"<<a<<"  "<<b<<endl;
    26             if(s.find(a)!=s.end()&&s.find(b)!=s.end())
    27             {
    28                 cout<<temp<<endl;
    29                 break;
    30             }
    31         }
    32     }
    33     return 0;
    34 }
    View Code

    UVA - 1595

    set

    对称轴是x坐标之和的平均值

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=1024;
     5 typedef pair<int,int> pii;
     6 set<pii> s;
     7 
     8 
     9 int main()
    10 {
    11    // #define LOCAL
    12     #ifdef LOCAL
    13         freopen("in.txt","r",stdin);
    14         freopen("out.txt","w",stdout);
    15     #endif // LOCAL
    16     int t;
    17     int n;
    18     scanf("%d",&t);
    19     while(t--)
    20     {
    21         s.clear();
    22         int ok=1;
    23         double temp=0;
    24         scanf("%d",&n);
    25         for(int i=0;i<n;i++)
    26         {
    27             int a,b;
    28             scanf("%d%d",&a,&b);
    29             s.insert(pii(a,b));
    30             temp+=a;
    31         }
    32 
    33         temp=temp/n;
    34         set<pii>::iterator it=s.begin();
    35         for(;it!=s.end();it++)
    36         {
    37             pii co=*it;
    38             pii r=pii(2*temp-co.first,co.second);
    39             if(!s.count(r))
    40             {
    41                 ok=0;
    42                 break;
    43             }
    44         }
    45         if(ok)
    46         {
    47             puts("YES");
    48         }
    49         else
    50         {
    51             puts("NO");
    52         }
    53     }
    54 }
    View Code

    UVA - 12100

    模拟queue

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int MAXN = 111;
     5 int t, n, m, _time;
     6 int que[MAXN*MAXN];
     7 
     8 void process(){
     9     int front = 0, rear = n;
    10     while(true){
    11         int maxi = que[front];
    12         for(int i = front; i < rear; ++i){
    13             if(que[i] > maxi){
    14                 if(front == m) m = rear;
    15                 que[rear++] = que[front++];
    16                 break;
    17             }
    18             else if(i == rear - 1){
    19                 ++_time;
    20                 if(front == m) return ;
    21                 front++;
    22             }
    23         }
    24     }
    25 }
    26 int main(){
    27     cin >> t;
    28     while(t--){
    29         _time = 0;
    30         cin >> n >> m;
    31         for(int i = 0; i < n; ++i) cin >> que[i];
    32         process();
    33         cout << _time << endl;
    34     }
    35     return 0;
    36 }
    copy

    UVA - 230

    set,map,string

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct node
     5 {
     6     string book,name;
     7     bool operator <(const node &a)const
     8     {
     9         if(name!=a.name) return name<a.name;
    10         return book<a.book;
    11     }
    12 };
    13 set<node> b,r;
    14 map<string,string> mp;
    15 string s;
    16 int main()
    17 {
    18     //freopen("in.txt","r",stdin);
    19     //freopen("out.txt","w",stdout);
    20     node temp;
    21     while(getline(cin,s))
    22     {
    23         if(s[0]=='E') break;
    24         int p=s.find('"',1);
    25         temp.book=s.substr(0,p+1);
    26         temp.name=s.substr(p+5);
    27         mp[temp.book]=temp.name;
    28         b.insert(temp);
    29     }
    30     while(getline(cin,s))
    31     {
    32         if(s[0]=='E') break;
    33         if(s[0]=='S')
    34         {
    35             set<node> ::iterator itb,itr;
    36             for(itr=r.begin();itr!=r.end();itr++)
    37             {
    38                 cout<<"Put "<<itr->book<<" ";
    39                 itb=b.lower_bound(*itr);
    40                 if(b.empty()||itb==b.begin()) cout<<"first
    ";
    41                 else cout<<"after "<<(--itb)->book<<endl;
    42                 b.insert(*itr);
    43             }
    44             r.clear();
    45             cout<<"END
    ";
    46         }
    47         else
    48         {
    49             int p=s.find('"');
    50             string book=s.substr(p);
    51             node temp;
    52             temp.book=book;
    53             temp.name=mp[book];
    54             if(s[0]=='B') b.erase(temp);
    55             else r.insert(temp);
    56         }
    57     }
    58 }
    co

    UVA - 1596 

    不会做,看的别人代码

    用两个map,一个存数组,一个存大小,,递归得到下标。。。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef unsigned int uint;
     5 const int maxn = 1010;
     6 int dcnt, ans;
     7 bool bug;
     8 vector<string> vs;
     9 map<string, string> value;
    10 map<string, unsigned int> mdes;
    11 string get_val(string index, string name) //递归调用,返回下标的实数值(字符串形式)
    12 {
    13     uint v = 0;
    14     if(index.find('[') == index.npos) //直接是一个实数
    15     {
    16         stringstream ss(index); ss >> v;
    17         if(name != " " && mdes[name] <= v) //容易理解mdes[name] <= v表示数组的下标越界
    18                                            //而name == " "的情况为定义语句a[3];或赋值语句等号右值(这里理解的也不透彻。。。)
    19             bug = true;
    20         return index;
    21     }
    22     string pname, pindex;
    23     pname = index.substr(0, index.find('['));
    24     pindex = index.substr(index.find('[')+1, index.find_last_of(']')-2);
    25     pindex = get_val(pindex, pname);
    26     if(bug) return " ";
    27     string vv;
    28     vv = pname+"["+pindex+"]";
    29     if(!value.count(vv)) bug = true; //若下标未初始化,则bug
    30     return value[vv];
    31 }
    32 void Define(string str)
    33 {
    34     string name, index; uint v = 0;
    35     name = str.substr(0, str.find('['));//提取数组名
    36     index = str.substr(str.find('[')+1, str.find_last_of(']')-2); //提取数组下标
    37     index = get_val(index, " "); //此处" "意在区别赋值语句调用该函数的情况;
    38     stringstream ss(index);     ss >> v;
    39     mdes[name] = v;//存入数组name的范围
    40 }
    41 void Assign(string str)
    42 {
    43     string L, R, name, index; uint v = 0;
    44     L = str.substr(0, str.find('=')); R = str.substr(str.find('=')+1);
    45     /*等号左边*/
    46     name = L.substr(0, L.find('['));
    47     ///find_last_of();
    48     index = L.substr(L.find('[') + 1, L.find_last_of(']')-2);
    49     index = get_val(index, name);
    50     if(bug) {return;}
    51     stringstream ss(index);     ss >> v;
    52     if(v >= mdes[name]) {bug = true; return;}
    53     /*赋值*/
    54     string left_value, right_value;
    55     left_value = name+"["+index+"]";
    56     right_value = get_val(R, " ");
    57     value[left_value] = right_value;
    58 }
    59 void init()
    60 {
    61     mdes[" "] = 0;//初始化" "代表的数组大小为0,此主要为以后求数组下标值时区别赋值语句与定义语句(及赋值语句等号右边)。
    62     vs.clear();
    63     mdes.clear();
    64     value.clear();
    65 }
    66 int main()
    67 {
    68     //freopen("in.txt", "r", stdin);
    69     string str;
    70     while(cin >> str && str[0] != '.')
    71     {
    72         init();
    73         vs.push_back(str);
    74         while(cin >> str && str[0] != '.')
    75         {
    76             vs.push_back(str);
    77         }
    78         bug = false;
    79         for(int i = 0; i < vs.size(); i++)
    80         {
    81             if(vs[i].find('=') == vs[i].npos)
    82             {
    83                 Define(vs[i]);
    84             }
    85             else
    86                 Assign(vs[i]);
    87             if(bug)
    88             {
    89                 cout << i+1 << endl;
    90                 break;
    91             }
    92             if(!bug && i == vs.size()-1)
    93                 cout << 0 << endl;
    94         }
    95     }
    96     return 0;
    97 }
    COPY

    UVA - 1597

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include <map>
      5 #include <cctype>
      6 #include <string>
      7 #include <set>
      8 #define INF 0x3f3f3f3f
      9 #define LL long long
     10 #define N 5005
     11 using namespace std;
     12 
     13 typedef map<string, int>::iterator mit;
     14 typedef set<int>::iterator sit;
     15 struct document{
     16     char text[1510][120];
     17     multimap<string, int> mp;
     18     int num;
     19 }dou[110];
     20 char s[1000];
     21 set<int> se;
     22 
     23 int main()
     24 {
     25     int n, q;
     26     scanf("%d", &n);
     27     getchar();
     28     for (int i = 0; i < n; i++){
     29         int j = 1;
     30         while (gets(dou[i].text[j++]), strcmp(dou[i].text[j-1], "**********")){
     31             char *p = dou[i].text[j-1];
     32             int len = strlen(p);
     33             for (int x = 0; x < len; ){
     34                 if (isalpha(p[x])){
     35                     string tm = "";
     36                     while (x < len && isalpha(p[x])) tm += tolower(p[x]), x++;
     37                     dou[i].mp.insert(make_pair(tm, j-1));
     38                 }
     39                 else x++;
     40             }
     41         }
     42         dou[i].num = j-1;
     43     }
     44     scanf("%d", &q);
     45     getchar();
     46     while (q--){
     47         int cnt = 0, k = 0;
     48         char t[10][100];
     49         gets(s);
     50         char *p = strtok(s, " ");
     51         while (p) strcpy(t[k++], p), p = strtok(NULL, " ");
     52         if (k == 1){
     53             for (int i = 0; i < n; i++){
     54                 if (dou[i].mp.count(t[0])){
     55                     se.clear();
     56                     if (cnt++) puts("----------");
     57                     mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]);
     58                     for (mit p = s; p != e; p++) se.insert(p->second);
     59                     for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]);
     60                 }
     61             }
     62         }
     63         else if (k == 2){
     64             for (int i = 0; i < n; i++){
     65                 if (!dou[i].mp.count(t[1])){
     66                     if (cnt++) puts("----------");
     67                     for (int j = 1; j < dou[i].num; j++) puts(dou[i].text[j]);
     68                 }
     69             }
     70         }
     71         else {
     72             if (strcmp(t[1], "AND") == 0){
     73                 for (int i = 0; i < n; i++){
     74                     if (dou[i].mp.count(t[0]) && dou[i].mp.count(t[2])){
     75                         se.clear();
     76                         if (cnt++) puts("----------");
     77                         mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]);
     78                         for (mit p = s; p != e; p++) se.insert(p->second);
     79                         s = dou[i].mp.lower_bound(t[2]), e = dou[i].mp.upper_bound(t[2]);
     80                         for (mit p = s; p != e; p++) se.insert(p->second);
     81                         for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]);
     82                     }
     83                 }
     84             }
     85             else{
     86                 for (int i = 0; i < n; i++){
     87                     if (dou[i].mp.count(t[0]) || dou[i].mp.count(t[2])){
     88                         se.clear();
     89                         if (cnt++) puts("----------");
     90                         mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]);
     91                         for (mit p = s; p != e; p++) se.insert(p->second);
     92                         s = dou[i].mp.lower_bound(t[2]), e = dou[i].mp.upper_bound(t[2]);
     93                         for (mit p = s; p != e; p++) se.insert(p->second);
     94                         for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]);
     95                     }
     96                 }
     97             }
     98         }
     99         if (!cnt) puts("Sorry, I found nothing.");
    100         puts("==========");
    101     }
    102     return 0;
    103 }
    COPY
  • 相关阅读:
    PHP(运算符,语句,函数)
    数据库高级查询
    MySQL数据库常用函数
    mySQL数据库数据查询操作详解
    mySQL数据库设计范式及增删改查操作
    mySQL-创建表
    JavaScript-选项卡
    JavaScript做DIV下拉列表
    JavaScript-事件
    JavaScript函数-递归
  • 原文地址:https://www.cnblogs.com/yijiull/p/7061554.html
Copyright © 2020-2023  润新知