String Successor(模拟)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3490

题意:给出一个字符串,一个操作次数,每次操作从当前字符串最右边的字符开始,如果字符串没有数字和字母则每次使其当前字符ASCII+1,否则,如果当前字符为z(Z),则其加1后为a(A ),并向前进位1给左边的最接近自己的字符或数字,如果当前字符为9,则加1后为0,并),并向前进位1给左边的最接近自己的字符或数字。如果某个数字或字符为最左边的数字或字符,并向前进位1,则在该字符或数字左边增加一个与其相同的字符或数字。

思路:理解题意并注意进位就可以了。。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <algorithm>
 6 using namespace std;
 7 int isdigit(char ch)
 8 {
 9     if (ch >= '0'&& ch <= '9')
10         return 1;
11     return 0;
12 }
13 int isalpha(char ch)
14 {
15     if (ch >= 'A'&&ch <= 'Z')
16         return 1;
17     else if (ch >= 'a'&&ch <= 'z')
18         return 2;
19     else
20         return 0;
21 }
22 void judge(string &s,int i)
23 {
24     char ch;
25     if (s[i]=='Z'||s[i]=='z'||s[i]=='9')
26     {
27         if (s[i]=='Z') {ch = 'A';s[i] = 'A';}
28         if (s[i]=='z') {ch = 'a';s[i] = 'a';}
29         if (s[i]=='9') {ch = '1';s[i] = '0';}
30     }
31     else
32     {
33         s[i]++;
34         return ;
35     }
36     int j;
37     for (j = i-1; j >= 0; j--)
38     {
39         if (isdigit(s[j])||isalpha(s[j]))
40         break;
41     }
42     string :: iterator it = s.begin();
43     if (j < 0)
44         s.insert(it+i,ch);
45         else
46          judge(s,j);
47 }
48 int main()
49 {
50     int t,k;
51     scanf("%d%*c",&t);
52     string s;
53     while(t--)
54     {
55         cin>>s;
56         scanf("%d",&k);
57         while(k--)
58         {
59             int len = s.size();
60             int flag = 0,i;
61             for (i = len-1; i >= 0; i--)
62             {
63                 if (isdigit(s[i])||isalpha(s[i]))
64                 {
65                     flag = 1;
66                     break;
67                 }
68             }
69             if(!flag)
70             {
71                 s[len-1]+=1;
72                 cout<<s<<endl;
73                 continue;
74             }
75             judge(s,i);
76             cout<<s<<endl;
77         }
78         puts("");
79     }
80     return 0;
81 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3599877.html