ccpc 长春站 J 题 Ugly Problem

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
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
Hint
9 + 9 = 18 999999999999 + 1 = 1000000000000
题意很简单,就是给一个数,输出这个数可以由多少个回文串加和得到,输出个数以及每个回文串,回文串个
数不得高于50
解:我们取当前串的前面一半,例如 93840 ,取938 ,然后另这串减1 ,(减1是为了对称后比原来串小)
得937,然后再对称得到 93739 , 用原串减去当前串得到一个位数减少一半的新串 111,然后再采取这样的操作
,直到当前串变为一位时,退出循环,每一次的对称得到的回 文串就是一个答案,记录回文串个数及回文串输
出即可
特殊判断 当前串的奇偶,奇数取前面一半加中间位,偶数位取前面一半位;当取得的数为两位数且十位为1时
也要特殊判断,因为取得的前面一 半串为1,减1 以后的0,对称以后为00 ,一个串减去00 永远不变,会造成
无限循环,所以这时我们特殊处理一下,当当前串十位比个位大时 取得的前面一半数不减1,直接对称过来,
比如 当前串为15 那么 取 1 对称得11,做差得4 ,变为1位数退出。当当前串十位小于等于个位 就两种情况
10 11 ,那么不再对称,直接减去9,剩余1 2 ,变为1位数退出循环。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1e6+10;

bool comp(string num1,string num2)
{
    int leng=num1.length(),i;
    for(i=0; i<leng; i++)
    {
        if(num1[i]!='0')break;
    }
    num1=num1.substr(i,leng);
    if(num1.length()==0)num1="0";

    leng=num2.length();
    for(i=0; i<leng; i++)
    {
        if(num2[i]!='0')break;
    }
    num2=num2.substr(i,leng);
    if(num2.length()==0)num2="0";

    if(num1.length()>num2.length())return true;
    else if(num1.length()==num2.length())
    {
        if(num1>=num2)return true;
        else return false;
    }
    else
        return false;
}
string _minus(string num1,string num2)
{
    if(comp(num2,num1))
    {
        string ss=num1;
        num1=num2;
        num2=ss;
    }
    reverse(num1.begin(),num1.end());
    reverse(num2.begin(),num2.end());
    string result="";

    int i;
    for(i=0; i<num1.length()&&i<num2.length(); i++)
    {
        char c=num1[i]-num2[i]+48;
        result=result+c;
    }
    if(i<num1.length())for(; i<num1.length(); i++)result=result+num1[i];

    int jiewei=0;
    for(i=0; i<result.length(); i++)
    {
        int zhi=result[i]-48+jiewei;
        if(zhi<0)
        {
            zhi=zhi+10;
            jiewei=-1;
        }
        else jiewei=0;
        result[i]=(char)(zhi+48);
    }

    for(i=result.length()-1; i>=0; i--)
    {
        if(result[i]!='0')break;
    }

    result=result.substr(0,i+1);
    reverse(result.begin(),result.end());
    if(result.length()==0)result="0";
    return result;
}
vector<string>ans;
string s1;
string fen(string str)
{
    s1="";
    string s2;
    int len=str.length();
    if(len&1)
    {
        for(int i=0; i<=len/2; i++)
        {
            s1+=str[i];
        }
        string temp="1";
        s1=_minus(s1,temp);
        for(int j=len/2-1, i=len/2+1; i<len; i++,j--)
        {
            s1+=s1[j];
        }
        s2=_minus(str,s1);
    }
    else
    {
        if(str=="11"||str=="10")/// 十位数字小于等于个位
        {
          s1="9";
          return _minus(str,s1);
        }
        for(int i=0; i<len/2; i++)
        s1+=str[i];
         if(!(len==2&&str[0]=='1'))
         {                         
              string temp="1";
            s1=_minus(s1,temp);
         }
         ///两位数字,十位数字为1,且十位数字小于个位,不再减一,直接对称过去
        for(int j=len/2-1, i=len/2; i<len; i++,j--)
        s1+=s1[j];

        s2=_minus(str,s1);
    }
    return s2;
}
bool cmp(string a,string b)
{
    return a>b;
}
int main()
{
    int T;
    string str,ss,strs;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        ans.clear();
        cin>>str;
        printf("Case #%d:\n",cas);
        int tot=0;
        while(1)
        {
            string temp="";
            if(str.length()==1)
            {
                ans.push_back(str);
                str="";
                break;
            }
            temp+=fen(str);
            str="";
            str+=temp;
            if(str.length()==0)
                break;
            ans.push_back(s1);
        }
        tot=ans.size();
        cout<<tot<<endl;
        for(int i=0; i<tot; i++)
        {
           cout<<ans[i]<<endl;
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值