hdu5920 Ugly Problem 回文串+高精度

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 189    Accepted Submission(s): 73
Special Judge


Problem Description
Everyone hates ugly problems.

You are given a positive integer. You must represent that number by sum of palindromic numbers.

A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.
 

Input
In the first line of input, there is an integer T denoting the number of test cases.

For each test case, there is only one line describing the given integer s ( 1s101000 ).
 

Output
For each test case, output “Case #x:” on the first line where x is the number of that test case starting from 1. Then output the number of palindromic numbers you used, n, on one line. n must be no more than 50. en output n lines, each containing one of your palindromic numbers. Their sum must be exactly s.
 

Sample Input
  
  
2 18 1000000000000
 

Sample Output
  
  
Case #1: 2 9 9 Case #2: 2 999999999999 1

题意:给一个数字,把这个数字拆分成若干个回文数字的和,数字长度1000,最多能拆成50个数字。

我的做法:开个while(1)循环,首先取原字符串s的前半段出来(长度上取整(len+1)/2),然后把这个字符串翻折一下,构造一个与原字符串长度一样的字符串。看构造的串是否与s相等,如果相等就存下来跳出循环。如果这个串的值小于s,存下这个串后更新s的值,就是用s减去这个串。如果这个串的值大于s的话,再用之前s的一半减1,再进行相同的构造操作,减1之后就保证这个串的值小于s,存下来,s再减。

可以发现这种方法每次可以减去一半的长度,一共1000位,10个串就构成了,有一些情况可能增加几个(比如10000),不过无伤大雅,远达不到50个。

CODE

#include <bits/stdc++.h>
using namespace std;
const int N = 1000+10;
char s[N];

void jf(char *s,char *s1)  ///高精度减法 s = s-s1; 只用于s>s1,且都为正
{
    int num1[N] = {0},num2[N] = {0};
    int lens = strlen(s),lens1 = strlen(s1);
    for(int i = 0;i < lens;i++)
        num1[i] = s[i]-'0';
    for(int i = 0;i < lens1;i++)
        num2[i] = s1[i]-'0';
    for(int i = lens-1,j = lens1-1;i >= 0 && j >= 0;--i,--j)
        num1[i] -= num2[j];
    for(int i = lens-1;i >= 0;i--){
        if(num1[i] < 0){
            num1[i] += 10;
            num1[i-1]--;
        }
    }
    bool flag = true;
    for(int i = 0;i < lens && flag;i++){
        if(num1[i]){
            for(int j = 0;i+j <= lens;j++){
                s[j] = num1[i+j]+'0';
                if(i+j == lens) s[j] = '\0';
            }
            flag = false;
        }
    }
    if(flag)
        strcpy(s,"0");
}
vector<string> vt;  ///存答案
int main(void)
{
    int T;
    scanf("%d",&T);
    int cas = 1;
    while(T--){
        vt.clear();
        scanf("%s",s);
        printf("Case #%d:\n",cas++);
        while(1){
            int len = strlen(s);
            char s1[N] = {0};
            for(int i = 0;i < (len+1)/2;i++)
                s1[i] = s[i];
            for(int i = 0,j = len-1;i < len/2;i++,j--)
                s1[j] = s1[i];
            s1[len] = '\0';
            int t = strcmp(s1,s);
            if(t == 0){      ///s1 == s 保存然后跳出去
                vt.push_back(s1);
                break;
            }
            else if(t < 0){  ///s1<s s减掉s1然后把s1存进去
                jf(s,s1);
                vt.push_back(s1);
            }
            else{            ///s1>s 把s的一半取出来-1构造回文串
                char s2[5] = "1";
                for(int i = 0;i < (len+1)/2;i++)
                    s1[i] = s[i];
                s1[(len+1)/2] = '\0';
                jf(s1,s2);
                int lens1 = strlen(s1);
                int tmp;
                if(len%2 == 0) tmp = lens1-1; ///s串长度为偶数就构造一个偶数长度的串,奇数就构造奇数长度的串
                else tmp = lens1-2;
                for(int i = tmp,j = lens1;i >= 0;i--,j++)
                    s1[j] = s1[i],s1[j+1] = '\0';
                if(strcmp(s1,"00") == 0) strcpy(s1,"1");
                vt.push_back(s1);
                jf(s,s1);
            }
        }
        printf("%d\n",vt.size());
        for(int i = 0;i < (int)vt.size();i++)
            cout<<vt[i]<<endl;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值