一开始没有找到合适的出发点,结果TL 了;看了网上的代码,知道了
startNum = (testNum - 9 * bit < 0)?0:testNum - 9 * bit; //此处减去各位上的(最大可能的)数字之和
// Problem#: 1325
// Submission#: 1301505
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
/**********准确度没有把握*************/
//int split(int n)
//{
// int splitSum = 0;
// int remainder = 0;
//
// if(n < 10)
// {
// return n;
// }
//
// while(n >= 10)
// {
// remainder = n % 10;
// splitSum = splitSum + remainder;
// n = (n - remainder)/10;
// }
// splitSum = splitSum + n;
// return splitSum;
//}
//
//int main()
//{
// int testNum;
// scanf("%d",&testNum);
// while(testNum-- > 0)
// {
// int n;
// scanf("%d",&n);
// int startNum = (n%2 == 0)?(n/2):(n+1)/2;
// int Sum = 0;
// for(int i = startNum; i < n; i++)
// {
// /*Sum = i + split(i);*/
// int splitSum = 0;
// if(i < 10)
// {
// splitSum = i;
// }
// else
// {
// int copy = i;
// int remainder = 0;
// while(copy >= 10)
// {
// remainder = copy % 10;
// splitSum = splitSum + remainder;
// copy = (copy - remainder)/10;
// }
// splitSum = splitSum + copy;
//
// }
//
// Sum = i + splitSum;
// if(Sum == n)
// {
// cout << i << endl;
// break;
// }
//
// if(Sum > n)
// {
// cout << "0" << endl;
// break;
// }
// }
// }
//
// return 0;
//}
/**********准确度没有把握*************/
int main()
{
int testCase;
scanf("%d",&testCase);
while(testCase-- > 0)
{
int startNum;
int bit = 0;
int testNum;
scanf("%d",&testNum);
for(int i = testNum; i != 0; i = i / 10 )
{
bit++;
}
startNum = (testNum - 9 * bit < 0)?0:testNum - 9 * bit; //此处减去各位上的(最大可能的)数字之和
int Sum = 0;
int j;
for(j = startNum; j < testNum; j++)
{
int splitSum = 0;
for(int k = j; k != 0; k = k/10)
{
splitSum = splitSum + k%10;
}
Sum = j + splitSum;
if(Sum == testNum)
{
/*cout << j << endl;*/
printf("%d\n",j);
break;
}
}
if(j == testNum) //不能在for循环的中途判断,例如10->11 ... 19->29;但是从20开始有 20->22,此时22比29小了
{
/*cout << "0" << endl;*/
printf("0\n");
}
}
return 0;
}