题意
求所有n位数中各个位之和等于m的数的和
题解
n不大可以直接从枚举到
,然后对其中的每一位数进行判断是否各个位之和为m,是的话答案就加上该数。
复杂度
时间复杂度为
空间复杂度为
代码
class Solution { public: /** * * @param n int整型 * @param m int整型 * @return long长整型 */ long long sum(int n, int m) { // write code here long long ans=0; int l=1; while(--n) l*=10; for(int i=l;i<l*10;i++) { int cnt=0; int j=i; while(j) { cnt+=j%10; j/=10; } if(cnt==m) ans+=i; } return ans; } };