Title description:
Input a number n such that a+b=n, and b is the result of setting a single digit to 0 in a, and output each case of a and b
Algorithm idea:
- General idea: enumerate a, calculate b through the formula, and then determine whether b is the result of setting a digit to 0 in a
- Set a position to 0: a/(10*t)+a%t, where t is 1,10,100,1000 to indicate one, ten, hundred, thousand, and set it to 0
long long t=1;
while(a>=t){
long long b = a/(t*10)*t+a%t;
if((a+b)==n){
cout<<"a = "<<a<<' '<<"b = "<<b<<endl;
k++;
break ; // 当a确定时候b也唯一确定,所以直接break
}
t*=10;
}
All codes:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
long long n,a;
cin>>n;
int k=0;
for(a=n/2;a<n;a++){
long long t=1;
while(a>=t){
long long b = a/(t*10)*t+a%t;
if((a+b)==n){
cout<<"a = "<<a<<' '<<"b = "<<b<<endl;
k++;
break ; // 当a确定时候b也唯一确定,所以直接break
}
t*=10;
}
}
cout<<"以上"<<k<<"个解"<<endl;
return 0;
}