码迷,mamicode.com
首页 > 其他好文 > 详细

[Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]

时间:2017-10-17 17:28:46      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:实现   task   stdio.h   提交   技术分享   strong   test   sum   traints   

Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + sum(M) + sum (sum(M)) = N, where sum(M) denotes the sum of digits in M.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a number N as input.

Output:
For each test case, print the count of numbers in new line.

Constraints:
1<=T<=500
1<=N<=109

Example:
Input:
2
5
9

Output:

0
1

Explanation:

Input: 9
Output: 1
Explanation: 
Only 1 positive integer satisfies the condition that is 3, 
3 + sum(3) + sum(sum(3)) = 3 + 3 + 3  = 9
#include <stdio.h>
#include <stdlib.h>
int sum(int n)
    {
        int sum=0;
        while(n)
        {
            sum=sum+n%10;
            n=n/10;
        }
        return sum;
    }
int main()
{
    int num,i;
    scanf("%d",&num);
    int *Arr=(int *)malloc(sizeof(int)*num);
    int *Brr=(int *)malloc(sizeof(int)*num);
    for(i=0;i<num;i++)
    {
        scanf("%d",&Arr[i]);
        Brr[i]=0;
    }

    for(i=0;i<num;i++)
    {
        int j=0;
        for(j=0;j<Arr[i];j++)
        {
            if(j+sum(j)+sum(sum(j))==Arr[i])
                Brr[i]++;
        }
    }
    for(i=0;i<num;i++)
    {
        printf("%d\n",Brr[i]);
    }

    return 0;
}

  看似完美的实现了要求,提交代码显示:

技术分享





[Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]

标签:实现   task   stdio.h   提交   技术分享   strong   test   sum   traints   

原文地址:http://www.cnblogs.com/wongyi/p/7682524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!