您的位置:首页 > 其它

第四届吉林省大学生程序设计大赛个人解题报告 Problem A: Harshad Number

2010-10-30 14:15 441 查看
Problem A: Harshad Number

Welcome to 2010 Jilin Province Collegiate Programming Contest. Do you know 2010 is a magic number? If you add up all the digits of 2010, that is 3, it is just one factor of 2010. The number who has this character is called Harshad number. A Harshad number, or Niven number in a given number base, is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers were defined by D. R. Kaprekar, a mathematician from India. The word "Harshad" comes from the Sanskrit "harṣa", meaning "great joy". The Niven numbers take their name from Ivan M. Niven from a paper delivered at a conference on number theory in 1997. All integers between zero and n are Harshad numbers in base n. The Harshad number is rare in all natural numbers. The ratio is about 10% decreasing with the number value. Your task is to list all the Harshad number between 1000 and 9999 orderly. We just consider the normal decimal base.

Input
This problem has no input

Output
You should output each Harshad number in one line orderly.

Sample Output
1000
……
2010
……
9990

Not all the numbers are listed in the sample. There are just the first, the last, and 2010 itself. The ellipsis expresses what you should calculate.

这道题是一道十足的水题,也就要求你打印1000 - 9999中,能除于自己每一位数加和的数。代码实现如下:

#include<iostream>
using namespace std;

int main()
{
for(int i = 1000 ; i <=9999 ; i++)
{
int sum = 0;
for(int tmp = i ; tmp ; sum += tmp % 10 , tmp /= 10);
if(i % sum == 0) printf("%d/n",i);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐