HDU 5920 Ugly Problem(大数减法 模拟)

版权声明:From: http://blog.csdn.net/tju_tahara https://blog.csdn.net/TJU_Tahara/article/details/52740294

Ugly Problem

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

Source
 


题意:
把一个长度小于1000的字符串拆分成n个回文数相加的数字。保证n < 50。

思路:
基本上算是一个模拟的水题。写好大数减法之后构造比原来数字小的回文数列即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

string operator -(string a, string b){
	int lena = a.length();
	int lenb = b.length();
	int len = max(lena, lenb) + 5;
	char res[len];
	memset(res, 0, sizeof(res));
	int flag, reslen = len;
	len--;
	res[len] = 0;
	len--;
	lena--; lenb--;
	flag = 0;
	while(lenb >= 0){
		res[len] = a[lena--] - b[lenb--] + '0' - flag;
		flag = 0;
		if(res[len] < '0'){
			flag = 1;
			res[len] = res[len] + 10;
		}
		len--;
	}
	while(lena >= 0){
		res[len] = a[lena--] - flag;
		flag = 0;
		if(res[len] < '0'){
			flag = 1;
			res[len] = res[len] + 10;
		}
		len--;
	}
	while((res[flag] == 0 || res[flag] == '0') && flag < reslen) flag++;
	if(flag == reslen) return "";
	return res + flag;
}

bool cmp(string a, string b){
	if(a.length() > b.length()) return true;
	if(a.length() < b.length()) return false;
	return a > b;
}

string get(int len){
	char res[len + 1];
	res[len] = 0;
	for(int i = 0; i < len; i++)
		res[i] = '9';
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	int tmp, len, casen, k = 1, top, i, hlen;
	string wanna, nxtstr;
	char nxt[1100];
	cin >> casen;
	queue<string> res;
	while(casen--){
		cin >> wanna;
		top = 0;
		while(wanna.length() != 0){

			len = wanna.length();
			hlen = len / 2;
			memset(nxt, 0, sizeof(nxt));
			for(i = 0; i < hlen; i++)
				nxt[i] = wanna[i];
			if(len % 2){
				nxt[i] = wanna[i];
				i++;
			}
			for(; i < len; i++){
				nxt[i] = nxt[len - i - 1];
			}
			if((len % 2) && nxt[hlen] != '0' && cmp(nxt, wanna)) nxt[hlen]--;
			while(cmp(nxt, wanna) && hlen >= 0){
				if(nxt[hlen] == '0'){
					hlen--;
					continue;
				}
				else{
					nxt[hlen]--;
					nxt[len - 1 - hlen]--;
				}
			}

			if(cmp(nxt, wanna) || nxt[0] == '0')
				nxtstr = get(len - 1);
			else
				nxtstr = nxt;

			wanna = wanna - nxtstr;
			top++;
			res.push(nxtstr);
		}
		cout << "Case #"<< k++ << ":" << endl;
		cout << top << endl;
		while(!res.empty()){
			nxtstr = res.front();
			res.pop();
			cout << nxtstr << endl;
		}
		
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/TJU_Tahara/article/details/52740294