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. azure备份虚拟机

    备份 azure虚拟机 提前创建了一个linux虚拟机,位置是chinaeast. 如果我们需要备份的虚拟机分别在中国东部和北部,那么需要在两个位置都创建备份库. 创建库 登录经典门户,新建-> ...

  2. 爬起点小说 day01

    先介绍下我自己爬起点小说的思路: 1.爬取所有的类型列表 a.链接存redis中 类型表:novel_list 具体每一种类型:bnovel_all_list(把novel_list和bnovel_l ...

  3. memcache的使用、版本使用和相关配置

    首先准备memcached和php_memcache.dll文件.下载网址:链接:http://pan.baidu.com/s/1c1WODji 密码:yzor 将下载好的memcached.exe放 ...

  4. hiho-1015- KMP算法

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  5. 智能合约语言Solidity教程系列2 - 地址类型介绍

    智能合约语言Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你 ...

  6. springBoot系列教程02:mongodb的集成及使用

    1.安装mongodb mongdb的安装很简单,只需要下载解压后运行mongod就好了 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86 ...

  7. Windows下安装solr步骤详解

    Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http Get操 ...

  8. Sql Server 新的日期类型

    新的日期类型:是出现在2008中的在2005中没有的日期类型. 1,Date 数据类型  2,time 数据类型  3, datetime2 数据类型  4, datetimeoffset 数据类型 ...

  9. css3的动画特效--元素旋转

    开发中,视觉要你实现一个元素的旋转问题,比如说如下图所示: 思路:首先动画动效肯定离不开anmimation动画. 和transition动画一样,animation动画也是CSS3动画的一种,这类动 ...

  10. always中的敏感变量

    always语句下如果有判断语句if,那么if语句中的条件必须有always中的敏感变量. 否则错误提示为:Error (10200): Verilog HDL Conditional Stateme ...