在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
以上是 在C ++中从a,b和c中删除所有零后,检查a + b = c是否有效 的全部内容, 来源链接: utcz.com/z/358276.html