码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3490 String Successor

时间:2015-04-22 22:01:33      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

简单的模拟题,类似于高精度加法运算。理解题意即可。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = 111;
char s[maxn];
char ji[maxn];
int gg[maxn];
void RevStr(char *str)
{
    int len;
    char *ptr;
    len = strlen(str);
    ptr = str + len - 1;
    while (str != ptr)
    {
        char ch; ch = *str; *str = *ptr; *ptr = ch; str++;
        if (str == ptr)break;
        else ptr--;
    }
}
int main()
{
    int n, i, y, m;
    int sb;
    scanf("%d", &n);
    for (sb = 0; sb < n; sb++)
    {
        memset(s, 0, sizeof(s));
        memset(ji, 0, sizeof(ji));
        scanf("%s", s);
        scanf("%d", &m);
        y = strlen(s);
        int q = 0;
        for (i = 0; i < y; i++)
        {
            if ((s[i] >= 0&&s[i] <= 9) || (s[i] >= a&&s[i] <= z) || (s[i] >= A&&s[i] <= Z))
            {
                ji[q] = s[i];
                gg[q] = i;
                q++;
            }
        }
        int bu = 0;
        while (q == 0)
        {
            s[y - 1]++;
            printf("%s\n", s);
            if ((s[y - 1] >= 0&&s[y - 1] <= 9) || (s[y - 1] >= a&&s[y - 1] <= z) || (s[y - 1] >= A&&s[y - 1] <= Z))
            {
                ji[q] = s[y - 1];
                gg[q] = y - 1;
                q++;
            }
            bu++;
            if (bu >= m) break;
        }
        if (bu < m)
        {
            for (i = 0; i < q / 2; i++)
            {
                int t = gg[i];
                gg[i] = gg[q - i - 1];
                gg[q - i - 1] = t;
            }
            RevStr(ji);
            while (1)
            {
                int zhi = 0;
                while (1)
                {
                    if (zhi == q)
                    {
                        if (ji[zhi - 1] >= 0&&ji[zhi - 1] <= 9){ ji[zhi] = 1; gg[zhi] = gg[zhi - 1]; }
                        else if (ji[zhi - 1] >= a&&ji[zhi - 1] <= z){ ji[zhi] = a; gg[zhi] = gg[zhi - 1]; }
                        else if (ji[zhi - 1] >= A&&ji[zhi - 1] <= Z){ ji[zhi] = A; gg[zhi] = gg[zhi - 1]; }
                        q++;
                        break;
                    }
                    if (ji[zhi] == 9){ ji[zhi] = 0; zhi++; }
                    else if (ji[zhi] == z){ ji[zhi] = a; zhi++; }
                    else if (ji[zhi] == Z){ ji[zhi] = A; zhi++; }
                    else { ji[zhi]++; break; }
                }
                zhi = q - 1;
                for (i = 0; i < y; i++)
                {
                    int flag = 0;
                    while (gg[zhi] == i&&zhi >= 0)
                    {
                        printf("%c", ji[zhi]);
                        zhi--;
                        flag = 1;
                    }
                    if (flag == 0) printf("%c", s[i]);
                }
                printf("\n");
                bu++;
                if (bu >= m) break;
            }
        }
        printf("\n");
    }
    return 0;
}

 

ZOJ 3490 String Successor

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4448744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!