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;
}

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

********************************************************************************************************************************

***********************************************问题解决************************************************************************

********************************************************************************************************************************

根据题意,我们知道1<=N<=109

sum(m)的范围是[ 1,81],其中m取999999999时候取得最大值。

sum(sum(m))的范围是[1,16] 其中sum(m)=79时候取得最大值。

故sum(m)+sum(sum(m))的范围是[2,95]

所以实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int sum(int n)
{
	int s=0;
	while(n)
	{
	   s=s+n%10;
	   n=n/10;
    }
    return s;
}
int main()
{
	int num;
	cin>>num;
	while(num--)
     {
		int n,sum1=0,c=0;
		cin>>n;
		if(n<100)
        {
            for(int j=1;j<n;j++)
            {
                if(j+sum(j)+sum(sum(j))==n)
                    c++;
            }
        }
		else
        {
		    for(int j=n-95;j<n;j++)
            {
                if(j+sum(j)+sum(sum(j))==n)
                    c++;
            }
		}
        cout<<c<<"\n";
     }
     return 0;
}

  

如有疑问,请留言。

[Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]的更多相关文章

  1. uva 10712 - Count the Numbers(数位dp)

    题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...

  2. c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

    第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...

  3. UVALive8518 Sum of xor sum

    题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...

  4. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  5. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

  8. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  9. Python神坑:sum和numpy.sum

    同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...

随机推荐

  1. 《java.util.concurrent 包源码阅读》06 ArrayBlockingQueue

    对于BlockingQueue的具体实现,主要关注的有两点:线程安全的实现和阻塞操作的实现.所以分析ArrayBlockingQueue也是基于这两点. 对于线程安全来说,所有的添加元素的方法和拿走元 ...

  2. 九、Hadoop学习笔记————Hive简介

    G级别或者T级别都只能用hadoop

  3. 三种方法运行python

    注:本文基于windows 1.交互式解释器 配置好环境变量后,命令行中打开,输入python即可,Ctrl+Z退出 命令行选项 当从命令行启动Python时,可以给解释器一些选项,如下: -d   ...

  4. leecode -- 3sum Closet

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. LINQ学习系列-----3.1 查询非泛型集合

    一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...

  6. HDU2191--多重背包(二进制分解+01背包)

    悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. 在CentOS上为Docker开启SELinux

    a { color: #4183C4; text-decoration: none } a:hover { text-decoration: underline } ul,ol { padding-l ...

  8. python中namedtuple介绍

    namedtuple:namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护.namedtuple能够用来创建类似于元祖的数 ...

  9. 》》canvas时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. quzrtz的使用

    Quartz是一个大名鼎鼎的Java版开源定时调度器,功能强悍,使用方便. 一.核心概念 1.Job 表示一个工作,要执行的具体内容,此接口只有一个方法 void execute(JobExecuti ...