ZOJ 3490 String Successor

点我看题目

题意 : 给你一个字符串,让你按照给定规则进行处理。

  • 如果字符串里有字母或者是数字就忽略非字符数字,如果没有,就让最右边的那个字符+1.
  • 增量都是从最右边的字母或者数字开始的。
  • 增加一个数字的方法是加1到另一个数字('0' -> '1', '1' -> '2' ... '9' -> '0')。
  • 增加一个大写字母的方法是加一位到另一个字母 ('A' -> 'B', 'B' -> 'C' ... 'Z' -> 'A')。
  • 增加一个小写字母的方法是加一位到另一个字母('a' -> 'b', 'b' -> 'c' ... 'z' -> 'a')。
  • 如果增加到了9或者z或者Z,就需要进位,进给它左边离它最近的字母或者数字
  • 如果进位的时候前边没有字母或者数字了,就在这位的前边加一位,例如9进位之后要变为0,前边要加一个1,A前边加一个A,a前边加一个a。

思路 : 就是一个模拟,用string中的insert非常方便也不容易错,我比完赛之后脑抽了一下,想用普通的方法去写,结果改了好几天。。。。。T_T......郁闷啊

insert版的

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std ;

string ch ;


int judge1(int n)
{
    for(int i = n ; i >= 0 ; i--)
    {
        if(isalnum(ch[i]))
            return 0 ;
    }
    return 1 ;
}
void carry(int len)
{
    for(int i = len ; i >= 0 ; i--)
    {
        if(isalnum(ch[i]))
        {
            if(ch[i] == '9')
            {
                ch[i] = '0' ;
                if(i == 0 || judge1(i-1))
                    ch.insert(i,"1") ;
                else carry(i-1) ;
            }
            else if(ch[i] == 'z')
            {
                ch[i] = 'a' ;
                if(i == 0 || judge1(i-1))
                {
                    ch.insert(i,"a") ;
                }
                else carry(i-1) ;
            }
            else if(ch[i] == 'Z')
            {
                ch[i] = 'A' ;
                if(i == 0 || judge1(i-1))
                {
                    ch.insert(i,"A") ;
                }
                else carry(i-1) ;
            }
            else ch[i]++ ;
            return ;
        }
    }
}
int main()
{
    int T,n ;
    scanf("%d",&T) ;
    while(T--)
    {
        cin>>ch>>n ;
        for(int i = 0 ; i < n ; i++)
        {
            if(!judge1(ch.size()-1)) carry(ch.size()-1) ;
            else
                ch[ch.size()-1]++ ;
           cout<<ch<<endl ;
        }
        printf("
") ;
    }
    return 0 ;
}
View Code

普通for循环的

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std ;

int len1;
char ch[110],ch1[110] ;

int judge(char sh)
{
    if((sh >= '0'&&sh <= '9')||(sh >= 'A'&&sh <= 'Z')||(sh >= 'a' && sh <= 'z'))
        return 1 ;
    return 0 ;
}
int judge1(int n)
{
    for(int i = n ; i >= 0 ; i--)
    {
        if(judge(ch[i]))
            return 0 ;
    }
    return 1 ;
}
void carry(int len)
{
    int k ;
    for(int i = len ; i >= 0 ; i--)
    {
        if(judge(ch[i]))
        {
            if(ch[i] == '9')
            {
                ch[i] = '0' ;
                if(i == 0 || judge1(i-1))
                {
                    for(k = len1 ; k >= i ; k--)
                        ch[k+1] = ch[k] ;
                    ch[i] = '1' ;
                    len1++;
                }
                else carry(i-1) ;
            }
            else if(ch[i] == 'z')
            {
                ch[i] = 'a' ;
                if(i == 0 || judge1(i-1))
                {
                    for(k = len1 ; k >= i ; k--)//一开始是从前往后移,但是都被覆盖了,是我没想到
                        ch[k+1] = ch[k] ;
                    len1++;
                }
                else carry(i-1) ;
            }
            else if(ch[i] == 'Z')
            {
                ch[i] = 'A' ;
                if(i == 0 || judge1(i-1))
                {
                    for(k = len1 ; k >= i ; k--)
                        ch[k+1] = ch[k] ;
                    len1++;
                }
                else carry(i-1) ;
            }
            else ch[i]++ ;
            return ;
        }
    }
}
int main()
{
    int T,n ;
    scanf("%d",&T) ;
    while(T--)
    {
        memset(ch,0,sizeof(ch));
        scanf("%s %d",ch,&n) ;
        for(int i = 0 ; i < n ; i++)
        {
            len1 = strlen(ch) ;//一开始len用的是局部变量,但是当从Z变成AA的时候长度没有跟着变,所以就改成了全局
            if(!judge1(len1-1)) carry(len1-1) ;
            else ch[len1-1]++ ;
            cout<<ch<<endl ;
        }
        printf("
") ;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3599242.html