hdu5920(字符串模拟)

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1159    Accepted Submission(s): 409
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


题意:给定一个数字,找出一个或几个回文数字,使得这几个回文数字之和刚好等于给出的数字。回文数字的数量不大于50!!!

字符串长度len无论奇偶,i与len-1-i都是左右对称的这点很受用


/*
思路:从中间开始向两边遍历并逐位赋值给b,找到不对称的地方时取较小的数,
每次找到的b都是小于a的较大回文数字,然后更新a,直到a的长度为1 
*/
#include
#include
#include
using namespace std;
const int N = 1010;  
char a[N],b[N],c[N],ans[N][N]; 
int len,num;
int main()
{
	int t;
	cin>>t;
	for(int k=1;k<=t;k++)
	{
		cin>>a;     //条件数字输入 
		len=strlen(a);
		num=0;
		while(len>=1)
		{
			bool flag=0;
			b[len>>1]=a[len>>1];     //为奇数长度服务,因为在循环中奇数长度的中间字符没有包含 
			for(int i=len/2-1;i>=0;i--)               //用这种方法可以很好的一起处理len为奇数或偶数的情况 
			{
				if(a[i]!=a[len-1-i]&&!flag)    //找到首个两边不相等的字符,并取小的 
				{
					flag=1;
					b[i]=b[len-1-i]=a[i]=0;i--)
			{
				ta=a[i]-'0';
				tb=b[i]-'0';
				n=(10+a[i]-b[i]-jw)%10;
				c[i]=n+'0';
				jw=(a[i]-b[i]-jw)<0;
			}
			c[len]=0;
			int tc=0;
			while(c[tc]=='0')tc++;
			strcpy(a,c+tc);
			len=strlen(a);
		}
		printf("Case #%d:\n%d\n",k,num);
		for(int i=0;i







你可能感兴趣的:(模拟)