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;
}

上一篇:我的python学习(二)条件控制、循环语句


下一篇:省选模拟11