简答题
偏难
#7293
自整除数 对一个整数n,如果其各个位数的数字相加得到的数m能整除n,则称n为自整除数.例如21,21%(2+1)==0,所以21是自整除数.现求出从10到n(n…
自整除数
对一个整数n,如果其各个位数的数字相加得到的数m能整除n,则称n为自整除数.例如21,21%(2+1)==0,所以21是自整除数.现求出从10到n(n < 100)之间的所有自整除数.
时间限制:1000
内存限制:65536
输入
有一行,整数n,(10 <= n < 100)
输出
有多行.按从小到大的顺序输出所有大于等于10,小于等于n的自整除数,每行一个自整除数.
样例输入
47
样例输出
10 12 18 20 21 24 27 30 36 40 42 45
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">// 参考答案1:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s, g;
cin >> n;
for (int i = 10; i <= n; i++) {
s = i / 10;
g = i % 10;
if (i % (s + g) == 0) {
cout << i << endl;
}
}
}</pre><pre class="brush:cpp;toolbar:false">// 参考答案2:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s = 0;
cin >> n;
for (int i = 10, a, b; i <= n; i++) {
for (a = i, s = 0; a != 0;) {
b = a % 10;
a /= 10;
s += b;
}
if (i % s == 0) {
cout << i << endl;
}
}
return 0;
}</pre>
这道题暂未补解析。