在C ++中从a,b和c中删除所有零后,检查a + b = c是否有效
假设我们有三个数字a,b,c,则必须从数字中删除所有0后再检查a+b=c。假设数字为a=102,b=130,c=2005,则在删除0之后,数字将为a+b=c:(12+13=25)这是正确的
我们将从数字中删除所有0,然后在删除0(a+b=c与否)之后进行检查。
示例
#include <iostream>
#include <algorithm>
using namespace std;
int deleteZeros(int n) {
int res = 0;
int place = 1;
while (n > 0) {
if (n % 10 != 0) { //if the last digit is not 0
res += (n % 10) * place;
place *= 10;
}
n /= 10;
}
return res;
}
bool isSame(int a, int b, int c){
if(deleteZeros(a) + deleteZeros(b) == deleteZeros(c))
return true;
return false;
}
int main() {
int a = 102, b = 130, c = 2005;
if(isSame(a, b, c))
cout << "a + b = c is maintained";
else
cout << "a + b = c is not maintained";
}输出结果
a + b = c is maintained
热门推荐
10 唯美的祝福语简短的
11 到同事新家简短祝福语
12 祝福语大全简短赞美同学
13 宝子生日祝福语简短
14 学生节祝福语简短大全
15 周一情人祝福语简短
16 团队文案祝福语简短励志
17 对生活祝福语简短精辟
18 教练生日祝福语 简短独特