#5067. 函数求数字之和
函数求数字之和
题目描述
求出1~n范围内的整数,使其数字之和为13,请问这样的数有多少个?
例如:数85,其数字之和为8+5=13;数373,其数字之和为3+7+3=13。
#include <bits/stdc++.h>
using namespace std;
int he(int x){
int s = 0;
while(x){
s += x%10;
x /= 10;
}
return s;
}
int main(){
int n, cnt = 0;
cin >> n;
for(int i = 1; i <= n; i++){
if( he(i) == 13 ) cnt++;
}
cout << cnt;
return 0;
}
以上代码是否能够实现对应效果 {{ select(1) }}
- 可以
- 不可以