数组和字符串

欢迎找茬


UVA 1585
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
#define _for(i,n) for(int i = 0; i < (n); i++)

int main(){
   int n, sum, len, cnt;
   char s[100];
   scanf("%d",&n);
   _for(i, n){
       scanf("%s",s);
       sum = cnt = 0;
       len = strlen(s);
       _for(k, len){
            if(s[k] == 'X')  cnt = 0;
            else   sum += (++cnt);
       }
       printf("%d\n",sum);
   }
   return 0;
}
/*
Sample Input

5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX

Sample Output
10
9
7
55
30
*/





UVA  1586
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
#define _for(i,a,b) for(auto i = (a); i < (b); i++)

char s[100];

double g(char c){
   switch(c){
      case 'C' : return 12.01;
      case 'H' : return 1.008;
      case 'O' : return 16.00;
      case 'N' : return 14.01;
   }
}

double f(char *p, char *q){
   if(q - p == 1) return g(*p);
   int cnt = 0;
   _for(k, p+1, q)
      cnt = cnt*10 + *k - '0';
   return (g(*p) * cnt);
}

int main(){
   int n;
   scanf("%d",&n);
   _for(k, 0, n){
      scanf("%s",s);
      char *p = s, *q = s+1;
      double sum = 0;
      while(*p != '\0'){
         while(*q != '\0' && isdigit(*q) ) q++;
         sum += f(p, q);
         p = q++;
      }
      printf("%.3f\n",sum);
   }
   return 0;
}




UVA 1225
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
#define _for(i,a,b) for(auto i = (a); i < (b); i++)

int a[10];

int main(){
   int n, m;
   scanf("%d",&n);
   _for(k, 0, n){
      memset(a, 0, sizeof(a));
      scanf("%d",&m);
      _for(i, 1, m+1){
         int t = i;
         while(t > 0){
            a[t % 10] ++;
            t = t/10;
         }
      }

      _for(i, 0, 10)  printf("%d%c",a[i],(i == 9 ? '\n' : ' '));
   }
   return 0;
}
/*
Sample Input

2
3
13

Sample Output

0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1
*/




UVA 455
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
#define _for(i,a,b) for(auto i = (a); i < (b); i++)

char s[100];

int main(){
   int n;
   scanf("%d",&n);
   _for(k, 0, n){
      scanf("%s",s);
      int len = strlen(s);
      bool ok;
      _for(T, 1, len+1){
         if(len % T != 0) continue;
         if(T > len/2) {printf("%d\n",len); break;}
         _for(i , 0, T){
            int k = i;
            ok = true;
            while(k < len){
               if(s[k] != s[i] ){
                 ok = false; break;
               }
               k += T;
            }
            if(!ok) break;
         }
         if(ok) {printf("%d\n",T);  break;}
      }
      if(k != n - 1) printf("\n");
   }
   return 0;
}
/*
Sample Input

1

HoHoHo

Sample Output

2

*/





UVA 227

 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;


const int maxn = 10;
char frame[maxn][maxn];
int spacex, spacey;
bool ok;

bool inside(int x, int y){
   if(x < 0 || y < 0 || x > 4 || y > 4) return false;
   return true;
}

bool read_input(){
    for(int i = 0; i < 5; i++){
        fgets(frame[i], maxn, stdin);
        if(frame[0][0] == 'Z') return false;
    }
    for(int x = 0; x < 5; x++)
       for(int y = 0; y < 5; y++){
           if(frame[x][y] == ' '){
               spacex = x; spacey = y;
               return true;
           }
       }
    return false;
}

bool op(char c){
    int tempx, tempy;
    if(!ok) return false;
    switch(c){
        case 'A':{tempx = spacex - 1; tempy = spacey; break;}
        case 'B':{tempx = spacex + 1; tempy = spacey; break;}
        case 'L':{tempx = spacex; tempy = spacey - 1; break;}
        case 'R':{tempx = spacex; tempy = spacey + 1; break;}
    }
    if(inside(tempx, tempy)){
        swap(frame[tempx][tempy],frame[spacex][spacey]);
        spacex = tempx; spacey = tempy;
        return true;
    }
    return false;
}

int main(){
   int kase = 0;
   while(read_input()){
      char c;
      ok = true;;
      while((c = getchar() ) != '0')  if(c != '\n') ok = op(c);
      getchar();

      if(kase) printf("\n");
      printf("Puzzle #%d:\n",++kase);
      if(ok){
          for(int x = 0; x < 5; x++)
              for(int y = 0; y < 5; y++)
                  printf("%c%c",frame[x][y], (y == 4)?'\n':' ');

      }
      else printf("This puzzle has no final configuration.\n");
   }
   return 0;
}






UVA 232
待修正
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;

const int maxn = 10 + 5;
int n, m;
char word[maxn][maxn];
int across[maxn][maxn], down[maxn][maxn];
int kase = 0;

void read_input(){
   memset(across,0,sizeof(across));
   memset(down,0,sizeof(down));
   memset(word,'*',sizeof(word));
   int flag;
   for(int i = 0; i < n; i++) scanf("%s",word[i]);
   int t = 1;
   for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++){
          flag = 0;
          if(word[i][j] != '*' && (j == 0  || (j && word[i][j-1] == '*' ))) {across[i][j] = t; flag = 1;}
          if(word[i][j] != '*' && (i == 0  || (i && word[i-1][j] == '*' ))) {down[i][j] = t; flag = 1;}
          if(flag) t++;
      }
}

void Print(){
    if(kase) printf("\n");
    printf("puzzle #%d:\n",++kase);
    printf("Across\n");
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++)
            if(across[i][j]){
                printf("%3d.",across[i][j]);
                while(word[i][j] != '*')
                   printf("%c",word[i][j++]);
                printf("\n");
            }
    }
    printf("Down\n");
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++)
            if(down[i][j]){
                int ii = i;
                printf("%3d.",down[ii][j]);
                while(word[ii][j] != '*')
                   printf("%c",word[ii++][j]);
                printf("\n");
            }
    }
}
int main(){
   while(scanf("%d",&n) == 1 && n && scanf("%d",&m) == 1){
      read_input();
      Print();
   }
   return 0;
}

/*
Sample Input

2 2
AT
*O
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0

Sample Output

puzzle #1:
Across
  1.AT
  3.O
Down
  1.A
  2.TO

puzzle #2:
Across
  1.AIM
  4.DEN
  7.ME
  8.ONE
  9.UPON
 11.TO
 12.SO
 13.ERIN
 15.SA
 17.OR
 18.IES
 19.DEA
Down
  1.A
  2.IMPOSE
  3.MEO
  4.DO
  5.ENTIRE
  6.NEON
  9.US
 10.NE
 14.ROD
 16.AS
 18.I
 20.A

*/





UVA 1368
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;

int n ,m;
int a[1000 + 5][4];
char s[1000+5];
char w[5] = "ACGT";
int main(){
   int T;
   scanf("%d",&T);
   while(T--){
      memset(a,0,sizeof(a));
      scanf("%d%d",&m,&n);
      while(m--){
         scanf("%s",s);
         int len = strlen(s);
         for(int i = 0; i < len; i++){
            switch(s[i]){
               case 'A':{a[i][0]++;break;}
               case 'C':{a[i][1]++;break;}
               case 'G':{a[i][2]++;break;}
               case 'T':{a[i][3]++;break;}
            }
         }
      }
      int sum = 0;
      for(int i = 0; i < n; i++){
         int maxn = max( max(a[i][0],a[i][1]),max(a[i][2],a[i][3]) );
         sum += ( a[i][0] + a[i][1] + a[i][2] + a[i][3] );
         sum -= maxn;
         for(int k = 0; k < 4; k++){
            if(a[i][k] == maxn) {
                printf("%c",w[k]);
                break;
            }
         }
      }
      printf("\n%d\n",sum);
   }
   return 0;
}







UVA 202

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

int a, b, c, k, cnt;
unordered_map<int,vector<int> >my_map;
vector<int>v;

void solve(){
   v.clear();
   my_map.clear();
   while(1){
      c = a / b;
      a = a % b;
      k = 1;
      for(auto it = my_map[c].begin(); it != my_map[c].end(); k++,it++)
         if(*it == a) return ;
      my_map[c].push_back(a);
      v.push_back(c);
      a = a * 10;
   }
}

void print(){
   for(int i = 0; i < v.size(); i++){
      if(i > 49) {printf("..."); break;}
      if(v[i] == c && --k == 0) {cnt = v.size() - i; printf("(");}
      printf("%d",v[i]);
   }
   printf(")\n   %d = number of digits in repeating cycle\n",cnt);
}

int main(){
   int kase = 0;
   while(scanf("%d%d",&a,&b) == 2){
      if(kase++) printf("\n");
      printf("%d/%d = %d.", a, b, a/b);
      a = ( a % b ) * 10;
      solve();
      print();
   }
   printf("\n");
   return 0;
}



UVA 10340
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < (b); i++)

const int maxn = 100000 + 5;
char s[maxn], t[maxn];

int main(){
   while(scanf("%s%s",s,t) == 2){
      int n = strlen(s), m = strlen(t);
      int j = -1, i = 0;
      for(; i < n; i++){
         while(j < m){
           j++ ;
           if(s[i] == t[j]) break;
         }
      }
      if(i == n && j < m) printf("Yes\n");
      else printf("No\n");
   }
   return 0;
}






UVA 1587
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define _for(i,a,b) for(int i = (a); i < (b); i++)

struct node{
   int a[2];
   bool operator < (const node& rhm) const{
        return (a[0] < rhm.a[0]) || ((a[0] == rhm.a[0]) && (a[1] < rhm.a[1]) );
   }
}S[6];



int main(){
   while(1){
      bool ok = true;
      _for(i,0,6){
        if(scanf("%d%d",&S[i].a[0], &S[i].a[1]) != 2) return 0;
        sort(S[i].a,S[i].a+2);
      }
      sort(S,S+6);
      for(int i = 0; i < 6; i = i+2){
         if(S[i].a[0] != S[i+1].a[0] || S[i].a[1] != S[i+1].a[1]){
            ok = false; break;
         }
      }
      if(!ok || S[0].a[0] != S[2].a[0] || S[0].a[1] != S[4].a[0] || S[2].a[1] != S[4].a[1]) ok = false;
      if(ok) printf("POSSIBLE\n");
      else printf("IMPOSSIBLE\n");
   }
   return 0;
}


UVA 272 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

const int maxn = 100000 + 5;
char buf[maxn];
int main(){
  int flag = 1;
  while(fgets(buf,maxn,stdin)){
     int len = strlen(buf);
     for(int i = 0; i < len; i++){
        if(buf[i] == '"' && flag) {
            printf("``"); flag = !flag;
        }
        else if(buf[i] == '"' && !flag){
            printf("''"); flag = !flag;
        }
        else printf("%c",buf[i]);
     }
  }
  return 0;
}





/*
Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

*/





UVA 10082
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;


char w[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";


int main(){
  char c;
  while((c = getchar()) != EOF){
     char *p = w;
     while(*p != '\0' && *(p+1) != c) p++;
     if(*p != '\0') putchar(*p);
     else putchar(c);
  }
  return 0;
}





/*
Sample Input

O S, GOMR YPFSU/

Sample Output

I AM FINE TODAY.

*/





UVA401
待修正
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

const char *S[] = {" -- is not a palindrome.\n\n",
                  " -- is a regular palindrome.\n\n",
                  " -- is a mirrored string.\n\n",
                  " -- is a mirrored palindrome.\n\n"};

char word[256], s[1000];

bool f(char a,char b){
    if(word[a] != b) return false;
    else return true;
}

int c1, c2, len;

int is_palindrome(char *s){
    int i = 0, j = len -1;
    while(i < j)
        if(s[i++] != s[j--] ) return 0;
    return 1;
}

int is_mirrored(char *s){
    int i = 0, j = len -1;
    while(i < j)
        if( !f( s[i++], s[j--]) ) return 0;
    return 1;
}

int main(){
  memset(word,' ',sizeof(word));
  word['A'] =  'A'; word['E'] = '3'; word['H'] = 'H'; word['I'] = 'I'; word['J'] = 'L';
  word['L'] =  'J'; word['M'] = 'M'; word['O'] = 'O'; word['S'] = '2'; word['T'] = 'T';
  word['U'] =  'U'; word['V'] = 'V'; word['W'] = 'W'; word['X'] = 'X'; word['Y'] = 'Y';
  word['Z'] =  '5'; word['1'] = '1'; word['2'] = 'S'; word['3'] = 'E'; word['5'] = 'Z';
  word['8'] =  '8';
  while(scanf("%s",s) == 1){
     len = strlen(s);
     c1 = is_mirrored(s);
     c2 = is_palindrome(s);
     printf("%s%s",s,S[2*c1+c2]);
  }
  return 0;
}





/*
Sample Input

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA

Sample Output

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.

*/




UVA340
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

const int maxn = 1000 + 5;
int n, a, b, guess;
int s[maxn];

int main(){
   int kase = 0;
   while(scanf("%d",&n) == 1 && n){
      int s1[10] = {0};
      printf("Game %d:\n",++kase);
      for(int i = 0; i < n; i++) {
          scanf("%d",&s[i]);
          s1[ s[i] ]++;
      }
      while(1){
         int s2[10] = {0}, s3[10] = {0};
         a = b = 0;
         for(int i = 0; i < n; i++){
             scanf("%d",&guess);
             if(guess == s[i]) {a++; s3[guess]++;}
             s2[guess]++;
         }
         if(guess == 0) break;
         for(int i = 1; i < 10; i++)
            if(s1[i] > 0)
               b += min(s1[i] - s3[i], s2[i] - s3[i]);

         printf("    (%d,%d)\n",a,b);
      }
   }
   return 0;
}


UVA 11809
待修正
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;


double A;
int B;
char line[30];

int main(){
   while(scanf("%s", line) == 1 && strcmp(line, "0e0") != 0){
     *strchr(line, 'e') = ' ';
     sscanf(line, "%lf%d", &A, &B);
     for(int m = 0; m <= 9; m++){
         double e = log10((log10(A) + B  - log10(pow(2,m+1) -1) + (m + 1) * log10(2)) / log10(2) + 1) / log10(2) ;
         int E = round(e);
         if(abs(e - E) < 1e-6){
             printf("%d %d\n",m,E);
             break;
         }
     }
   }
   return 0;
}


/*
Sample Input

5.699141892149156e76
9.205357638345294e18
0e0

Sample Output

5 8
8 6

*/

UVA 1583
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

const int maxn = 100000;
int S[maxn];

int f(int i){
   int sum = i;
   while(i > 0){
      sum += (i%10);
      i = i/10;
   }
   return sum;
}

int main(){
    memset(S,0,sizeof(S));
    for(int i = 1; i < maxn; i++){
        int t = f(i);
        if(!S[t])
           S[t] = i;
    }
    int n, a;
    scanf("%d",&n);
    while(n--){
        scanf("%d",&a);
        printf("%d\n",S[a]);
    }
    return 0;
}





/*
Sample Input

3
216
121
2005

Sample Output

198
0
1979

*/


UVA1588
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

string s1, s2;

int main(){
    while( cin>>s1>>s2 ){

        int n = s1.length(), m = s2.length();
        int ans = m + n;

        for(int i = 0; i < n; i++){
            bool ok = true;
            int ii = i;
            for(int j = 0; j < m; j++){
                if(s1[ii++] - '0' + s2[j] - '0' > 3){
                    ok = false; break;
                }
                if(ii == n) break;
            }
            if(ok) {ans = max(n, i + m);break;}
        }

        swap(s1,s2); swap(n,m);
        for(int i = 0; i < n; i++){
            bool ok = true;
            int ii = i;
            for(int j = 0; j < m; j++){
                if(s1[ii++] - '0' + s2[j] - '0' > 3){
                    ok = false; break;
                }
                if(ii == n) break;
            }
            if(ok) {ans = min(ans, max(n, i + m));break;}
        }


        printf("%d\n",ans);
    }
    return 0;
}


/*
Sample Input

2112112112
2212112
12121212
21212121
2211221122
21212


Sample Output

10
8
15

*/



UVA 1584
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<cmath>
#include<queue>
#include<cctype>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;

const int maxn = 100 + 5;
char s[maxn];
int len, best;

void f(int i){
    for(int k = 0; k < len; k++)
       if(s[ (i+k) % len ] > s[ (best+k) % len ]) break;
       else if(s[ (i+k) % len ] < s[ (best+k) % len ]) {best = i; break;}
}

int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        scanf("%s",s);
        len = strlen(s);
        best = 0;
        for(int i = 0; i < len; i++) f(i);
        for(int i = 0; i < len; i++)
           printf("%c",s[(best+i) % len]);
        putchar('\n');
    }
    return 0;
}





/*
Sample Input

2
CGAGTCAGCT
CTCC

Sample Output

AGCTCGAGTC
CCCT

*/

猜你喜欢

转载自blog.csdn.net/a874288174/article/details/80084160