2016中国大学生程序设计竞赛(长春)-重现赛 题解

J. Ugly problem

题意:把大数字拆成不超过50个的回文数

题解:每次用不超过大数字的回文数去减,得到的结果要再这样执行,注意"10"这个数的坑点

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
using namespace std;
const double eps=1e-8;
int len;
void subtract(char *a,char *b,char *c)
{
    for(int i=len-1;i>=0;i--)
    {
        c[i]=a[i]-b[i]+‘0‘;
        if(c[i]<‘0‘)c[i]+=10,a[i-1]--;
    }
}

char str[2][1005],sub[50][1005];
int main()
{
    int i,j,k,n;
    scanf("%d",&n);getchar();
    for(int h=1;h<=n;h++)
    {
        memset(sub,‘0‘,sizeof(sub));
        gets(str[0]);
        len=strlen(str[0]);
        int old=1,now=0;
        int cnt=0;
        for(i=0;str[now][i];)
        {
            old^=1,now^=1;
            if(!strcmp(str[old]+i,"10"))
            {
                sub[cnt][0]=‘9‘;sub[cnt][1]=0;cnt++;
                sub[cnt][0]=‘1‘;sub[cnt][1]=0;cnt++;
                break;
            }
            for(j=i;j<=len-1-j+i && str[old][j]<=str[old][len-1-j+i];j++);
            strcpy(sub[cnt],str[old]);
            if(j<len-1-j+i)
            {
                sub[cnt][(i+len-1)/2]--;
                for(j=(i+len-1)/2;j>=0 && sub[cnt][j]<‘0‘;j--)sub[cnt][j]+=10,sub[cnt][j-1]--;
            }
            for(j=0;sub[cnt][j]==‘0‘;j++);
            for(k=j;k<=len-1-k+j;k++)sub[cnt][len-1-k+j]=sub[cnt][k];
            sub[cnt][len]=0;
            subtract(str[old],sub[cnt],str[now]);
            for(;str[now][i]==‘0‘;i++);
            cnt++;
        }
        printf("Case #%d:\n%d\n",h,cnt);
        for(i=0;i<cnt;i++)
        {
            char *s;
            for(s=sub[i];*s==‘0‘;s++);
            puts(s);
        }
    }
    return 0;
}

J ugly problem

时间: 2024-10-15 13:50:04

2016中国大学生程序设计竞赛(长春)-重现赛 题解的相关文章

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))

Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Mr. Frog has n sticks, whose lengths are 1,2, 3?n respectively. Wallice is a bad man,

HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Mr. Frog recently studied how to add two fractions up, and he came up with an evil ide

HDU 5916 Harmonic Value Description 【构造】(2016中国大学生程序设计竞赛(长春))

Harmonic Value Description Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description The harmonic value of the permutation p1,p2,?pn is ∑i=1

2016中国大学生程序设计竞赛 - 网络选拔赛

solved 4/11 2016中国大学生程序设计竞赛 - 网络选拔赛

HDU 5833 Zhu and 772002(高斯消元)——2016中国大学生程序设计竞赛 - 网络选拔赛

传送门 Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 48    Accepted Submission(s): 16 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test the abili

HDU 5832 A water problem(取模~)—— 2016中国大学生程序设计竞赛 - 网络选拔赛

传送门 A water problem Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 60    Accepted Submission(s): 37 Problem Description Two planets named Haha and Xixi in the universe and they were created wit

2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means every time

2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m