在C ++中找到N可以被D整除的数字
假设我们有两个数字N和D。我们必须找到N个数字,该数字可以被D整除。如果N为3,D为5,则数字可以为500。这很容易解决。如果D为10,N为1,那么将是不可能的。我们可以放入D,并假设D有m个数字,然后附加N-m个0,以使其成为N个数字并可以被D整除。
示例
#include<iostream>
using namespace std;
string nDigitDivByD(int n, int d) {
string ans = "";
if (d < 10) {
ans += to_string(d);
for (int i = 1; i < n; i++)
ans += "0";
}
else {
if (n == 1)
return "Cannot find any number";
else {
string temp = to_string(d);
ans += to_string(d);
for (int i = 0; i < n-temp.length(); i++)
ans += "0";
}
}
return ans;
}
int main() {
int n = 5, d = 15;
cout << nDigitDivByD(n, d);
}输出结果
15000
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短