浙大oj3490 String Successor 求助~~
String Successor--------------------------------------------------------------------------------
Time Limit: 2 Seconds Memory Limit: 65536 KB
--------------------------------------------------------------------------------
The successor to a string can be calculated by applying the following rules:
Ignore the nonalphanumerics unless there are no alphanumerics, in this case, increase the rightmost character in the string.
The increment starts from the rightmost alphanumeric.
Increase a digit always results in another digit ('0' -> '1', '1' -> '2' ... '9' -> '0').
Increase a upper case always results in another upper case ('A' -> 'B', 'B' -> 'C' ... 'Z' -> 'A').
Increase a lower case always results in another lower case ('a' -> 'b', 'b' -> 'c' ... 'z' -> 'a').
If the increment generates a carry, the alphanumeric to the left of it is increased.
Add an additional alphanumeric to the left of the leftmost alphanumeric if necessary, the added alphanumeric is always of the same type with the leftmost alphanumeric ('1' for digit, 'A' for upper case and 'a' for lower case).
Input
There are multiple test cases. The first line of input is an integer T ≈ 10000 indicating the number of test cases.
Each test case contains a nonempty string s and an integer 1 ≤ n ≤ 100. The string s consists of no more than 100 characters whose ASCII values range from 33('!') to 122('z').
Output
For each test case, output the next n successors to the given string s in separate lines. Output a blank line after each test case.
Sample Input
4
:-( 1
cirno=8 2
X 3
/**********/ 4
Sample Output
:-)
cirno=9
cirnp=0
Y
Z
AA
/**********0
/**********1
/**********2
/**********3
代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
using namespace std;
char f(char x)
{
if(x=='9'){return '0';}
if(x=='z'){return 'a';}
if(x=='Z'){return 'A';}
else
{return 'n';}
}
int main()
{
int T,i,n,j,k,flag,q;
char s[105],c[105];
cin>>T;
for(i=0;i<T;i++)
{
memset(c,0,sizeof(c));
scanf("%s%d",s,&n);
for(q=0;q<n;q++)
{
k=0;flag=0;
for(j=strlen(s)-1;j>=0;j--)
{
if(isalnum(s[j]))
{flag=1;}
c[k]=s[j];
k++;
}
if(flag==1)
{
for(j=0;j<strlen(s);j++)
{
if(isalnum(c[j])){break;}
}
if(f(c[j])!='n')//jin wei
{
c[j]=f(c[j]);
j++;
while(j<strlen(s))
{
if(isalnum(c[j]))
{
if(f(c[j])!='n')
{c[j]=f(c[j]);}
else
{c[j]++;break;}
}
j++;
}
if(j>=strlen(s))
{
for(j=strlen(s)-1;j>=0;j--)
{
if(isalnum(c[j])){break;}
}
for(k=j+1;k<=strlen(s)-1;k++)
{
c[k+1]=c[k];
}
c[k+1]='\0';
if(isdigit(c[j]))
{c[j+1]='1';}
else
{c[j+1]=c[j];}
}
}
else
{c[j]++;}
}
else
{c[0]++;}
k=0;
for(j=strlen(c)-1;j>=0;j--)
{
s[k]=c[j];
k++;
printf("%c",c[j]);
}
printf("\n");
}
}
}
试了好多数据都找不出错的,提交上去就是不对。。。求指点~!