适用于任何位数的数字求和

大师  •  20天前


include

include

include <math.h>

include <stdlib.h>

using namespace std;

int main() {

int a,num;
cin >>a;
num=a;
unsigned i=0;
for(;num!=0;i++){
	num /= 10;
}
//cout << i;
unsigned sum=0;
for(;i>=1;i--){
	sum += int(a / pow(10,i)) % 10;
}
sum += a % 10;
cout << sum;
return 0;

}


评论:

请先登录,才能进行评论