세 개의 숫자 a, b, c가 있다고 가정하고 숫자에서 0을 모두 제거한 후 a + b =c인지 확인해야 합니다. 숫자가 a =102, b =130, c =2005라고 가정하고 0을 제거한 후 숫자는 a + b =c가 됩니다. (12 + 13 =25) 이것은 true입니다.
숫자에서 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