其他分享
首页 > 其他分享> > ACM错题

ACM错题

作者:互联网

1.首先应该对其求各个位数之和

int getSum(int num){
	int sum = 0;
	while (num != 0){
		int i = num % 10;
		sum += i;
		num= 10;
	}
	return sum;
}

2.求前n个数的各个位数之和

#include "stdafx.h"
#include<stdio.h>
int getSum(int num){
	int sum = 0;
	while (num>0){
		int i = num % 10;
		sum =sum+ i;
		num=num/10;
	}
	return sum;
}
int main(){
	int x;
	scanf_s("%d", &x);
	int sum, total = 0;
	for (int i = 0; i <=x; i++){
		sum = getSum(i);
		total =total+ sum;

	}
	printf("%d", total);

	return 0;
}

标签:10,int,sum,ACM,错题,while,num,include
来源: https://blog.csdn.net/m0_62538383/article/details/122535964