选修课网址 1088: The Owl and the Fox

狐狸Mithra需要帮助重新排列数字序列,根据数字之和的大小进行调整。每给出一个数字N,需要找到比N小且数字之和少一的最接近数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1088: The Owl and the Fox
Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 44  Solved: 14
[Submit][Status][Web Board]
Description
Fox Mithra has finally learned the numbers and he is now familiar with the concept of ‘one’, ’two’, ‘three’ and also even ‘zero’, ‘minus one’, ‘minus two’ and so on. Really, an achievement for such a small fox. He took the textbook and copied the integers from the book one by one from the smallest to the biggest on the wall of his enclosure in the ZOO.
“Look, there is something wrong with your sequence on the wall”, said the owl who just landed on the branch above Mithra’s head. “You should put 30 between 20 and 22, there.”
“Why?”
“Because the importance of a number is judged by the sum of its digits. 30 is therefore less important than 22 and it is more important than 20. And obviously, 30 should be equally close to 20 and 22 because its sum of digits differs only by one from both 20 and 22.”
“I see,” replied Mithra, “you are really clever. Can you help me please to rearrange the sequence correctly? Each time I tell you a number N you will tell me the closest smaller number with sum of digits less by one than the sum of digits of N.”
“With pleasure,” nodded the owl majestically.
Your task is to imitate the owl’s task. Given an integer N you have to find the biggest integer
which is smaller than N with sum of digits less by one than the sum of digits of N. 
Input
There are more test cases. Each case consists of a line containing a single integer N (1 ≤ N ≤ 100 000). The input is terminated by a line containing string “END” and no other symbols. 
Output
For each test case print on a separate line the number demanded by fox Mithra. 
Sample Input
30
199
1000
1520
END
Sample Output
20
198
0
1510
HINT

Source


容易超时  用字符char数组会快一点


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
	char a[1000010];
	long long n,m,i,k;
	ios::sync_with_stdio(false);
	while(scanf("%s",a)&&strcmp(a,"END"))
	{
		n=strlen(a);
		n=n-1;
		k=5;
		for(i=n;i>=0;i--)
		{
			if(a[i]!='0')
			{
				a[i]=a[i]-1;
				if(i!=0)
				{
					break;
				}
			}
			if(a[0]=='0')
			{
				k=0;	
			}
		}
		if(k==0)printf("0\n");
		else printf("%s\n",a);
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值