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. 自己动手写Redis客户端(C#实现)4 - 整数回复

    整数回复 整数回复就是一个以 ":" 开头, CRLF 结尾的字符串表示的整数. 比如说, ":0\r\n" 和 ":1000\r\n" 都 ...

  2. windows下tensorflow的安装

    一.直接python安装 1.CPU版本: pip3 install --upgrade tensorflow 2.GPU版本:pip3 install --upgrade tensorflow-gp ...

  3. leetcode算法题1: 两个二进制数有多少位不相同?异或、位移、与运算的主场

    /* The Hamming distance between two integers is the number of positions at which the corresponding b ...

  4. Android开发之漫漫长途 番外篇——内存泄漏分析与解决

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  5. pipelineDB里Combine用法

    combine only works on aggregate columns that belong to continuous views. 创建CONTINUOUS CREATE CONTINU ...

  6. sqlserver 全库查询 带架构

    网上现有的全库查询,无法识别自定义架构的数据库结构: ) ) ) declare @counts int )--以上定义变量 declare cur1 cursor for select a.name ...

  7. 初学sheel脚本练习过程

    以下是初学sheel脚本练习过程,涉及到内容的输出.基本的计算.条件判断(if.case).循环控制.数组的定义和使用.函数定义和使用 sheel脚本内容: #! /bin/bashecho &quo ...

  8. 基于Accord.Audio和百度语言识别

    ---恢复内容开始--- 目标需求 使用录音形式,模拟微信语音聊天.按住录音,松开发送语音,并完成语音识别. ps:百度的语言识别有60秒长度限制,需要自己做好控制. 实现方案 采用C# winfor ...

  9. 利用linux shell自己主动顶贴

    在论坛上面发帖问个什么东西的话,一旦不顶.帖子就秒沉了,可是又实在不想每时每刻都去顶,怎么办?以下展示了怎样利用shell 的crontab实现自己主动顶贴. 闲话不多说了,以豆瓣为例-– 1: 用c ...

  10. hdu 5225 Tom and permutation(回溯)

    题目链接:hdu 5225 Tom and permutation #include <cstdio> #include <cstring> #include <algo ...