[sicily]1325. Digit Generator

1325. Digit Generator

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M .

For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.

Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.

You are to write a program to find the smallest generator of the given integer.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N , 1$ \le$N$ \le$100, 000 .

Output

Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does not have any generators, print 0.

The following shows sample input and output for three test cases.

Sample Input

3 
216 
121 
2005

Sample Output

198 
0 
1979
简单的数字处理题。大意是给出一个数N,要我们找到一个最小的小于N的整数 K,满足 K与K的各个位上的数之和为 N。直接一个for 循环,从较小的一个数 i 开始,除10取余得到各位上数字之和,加上 i 判断是否等于 N 即可 。需要注意的是,这个寻找范围需要控制压缩,不然超时。假设每个数字都是9,减去即可判断出最小下界。代码如下:


#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int count=0;
        int tmp = n;
        while(tmp>0)
        {
            tmp = tmp/10;
            count++;
        }           
        int i = 0;
        int flag = 1;
        if( i < 9*count)
            i = 9*count;
        for(i=n-i; i<n; i++) //注意 i 的起点,缩小寻找范围 
        {
            tmp = i;
            int sum = 0;
            while(tmp>0)
            {
                sum += tmp%10;
                tmp = tmp/10;
            }
            if( i+sum == n)
            {
                cout<<i<<endl;
                flag = 0;
                break;
            }
        }
        if(flag)//没有找到解的情况
            cout<<0<<endl;
    } 
    //system("pause");
    return 0;   
}                                                             


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值