hdu 5920 Ugly Problem 2016ACM/CCPC长春赛区现场赛J

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

每次减去可以减的最大的,可以使得数位减半

因为只有1000位长度,因此该方法可以在50次之内构造出答案

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
string xt[101];
int a[10001],b[10001];
int main()
{
	int T,k=0;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		k++;
		string x;
		cin>>x;
		int i,j;
		int len=x.size();
		for(i=0;i<len;i++)
			a[i+1]=x[i]-'0';
		int ss=0,d=1;
		while(d<=len)
		{
			for(i=d;i<=(len+d)/2;i++)
			{
				b[i]=a[i];
				b[len+d-i]=a[i];
			}
			bool flag=false;
			for(i=(len+d)/2+1;i<=len;i++)
			{
				if(b[i]>a[i])
				{
					flag=true;
					break;
				}
				else if(b[i]<a[i])
					break;
			}
			if(flag)
			{
				j=(len+d)/2;
				while(b[j]==0)
				{
					b[j]=9;
					b[len+d-j]=9;
					j--;
				}
				b[j]--;
				b[len+d-j]--;
				if(b[j]==0&&j==d)
				{
					for(i=d+1;i<=len;i++)
						b[i]=9;
				}
			}
			ss++;
			xt[ss]="";
			flag=false;
			for(i=d;i<=len;i++)
			{
				if(!flag&&b[i]==0)
					continue;
				flag=true;
				xt[ss]+=(b[i]+'0');
			}
			for(i=len;i>=d;i--)
			{
				a[i]=a[i]-b[i];
				if(a[i]<0)
				{
					a[i]+=10;
					a[i-1]--;
				}
			}
			while(a[d]==0)
				d++;
		}
		printf("Case #%d:\n%d\n",k,ss);
		for(i=1;i<=ss;i++)
			cout<<xt[i]<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值