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 包源码阅读》10 线程池系列之AbstractExecutorService

    AbstractExecutorService对ExecutorService的执行任务类型的方法提供了一个默认实现.这些方法包括submit,invokeAny和InvokeAll. 注意的是来自E ...

  2. cmd markdown 使用教程

    cmd markdown 使用教程 tags: 自制教程 李卓伦 目录: [TOC] 一.简介与安装 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd M ...

  3. CS窗体程序数据列表分页

    以前,觉得winform程序分页很无趣,也没实际意义,直到近期的项目实践中让我认识到原来winform数据列表分页也是非常有必要的,因为由于数据量过大的情况,当窗体在初始加载数据的时候如果不做条件的限 ...

  4. display:box和display:flex填坑之路

    背景分析:最近做移动端项目时,遇到一个常见的需求: 可以滑动的导航,如下图 虽然是很常见的一个布局,但在移动端没有做过,想当然的写下以下的样式,简单描述下: 父元素 width:100%: overf ...

  5. 友元函数 C++

    #include<iostream> #include<vector> using namespace std; class Text{ public: Text():a(){ ...

  6. tomcat警告setting property 'debug' to '0' did not find a matching property

    在使用tomcat6.0版本结合myeclipse进行java web项目,运行程序显示setting property 'debug' to '0' did not find a matching ...

  7. 如何利用panel在一个窗口中实现诸多页面的显示

    Book_register form11 = new Book_register();//new一个对象 form11.TopLevel = false;//去除子窗体的顶级窗体设置 form11.P ...

  8. 在Linux中使用线程

    我并不假定你会使用Linux的线程,所以在这里就简单的介绍一下.如果你之前有过多线程方面的编程经验,完全可以忽略本文的内容,因为它非常的初级. 首先说明一下,在Linux编写多线程程序需要包含头文件p ...

  9. iOS 处理socket粘包问题

    1.什么是粘包? 粘包通常出现在TCP的协议里面,对于UDP来说是不会出现粘包状况的,之所以出现这种状况的原因,涉及到一种名为Nagle的算法. Nagle算法通过减少必须发送的封包的数量,提高网络应 ...

  10. OAuth 2.0(网转)

    (一)背景知识 OAuth 2.0很可能是下一代的"用户验证和授权"标准,目前在国内还没有很靠谱的技术资料.为了弘扬"开放精神",让业内的人更容易理解" ...