http://acm.hdu.edu.cn/showproblem.php?pid=1406
Problem Description
完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3;28=1+2+4+7+14。
本题的任务是判断两个正整数之间完数的个数。
Input
输入数据包含多行,第一行是一个正整数n,表示测试实例的个数,然后就是n个测试实例,每个实例占一行,由两个正整数num1和num2组成,(1<num1,num2<10000) 。
Output
对于每组测试数据,请输出num1和num2之间(包括num1和num2)存在的完数个数。
Sample Input
2
2 5
5 7
Sample Output
0
1
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;
int ans[10005];
bool iswan (int n)
{
int sum = 0, i, x = n;
for (i = 1; i < x && n >= i; i++)
{
if (n % i == 0)
sum += i;
if (sum > x)
break;
}
//cout << x << "---" << sum << endl;
if (sum == n)
return true;
return false;
}
int main()
{
int i;
memset (ans, 0, sizeof(ans));
for (i = 2; i < 10000; i++) //ans[i]表示累计到i【包括i】一共有多少个完数
{
if (iswan (i))
ans[i] = ans[i-1] + 1;
else ans[i] = ans[i-1];
}
/*for (i = 2; i < 100; i++)
cout << ans[i] <<' ';
cout << endl;*/
int t, a, b, res;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d", &a, &b);
if (a > b)
a ^= b, b ^= a, a ^= b; //忘记这里WA了一次,a和b实现交换
//cout << a << ' ' << b << endl;
res = ans[b]-ans[a];
if (iswan(a))
res++;
printf ("%d\n", res);
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
HDU的一题........HDU DP动态规
hdu1001解题报告
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
ACM HDU题目分类,我自己总结的大概只有十来个吧
搜索 dfs 解题代码 hdu1241
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
hdu1290 解题报告 献给杭电五十周年校庆的礼物 (切西瓜问题,即平面分割空间)
HDU最全ac代码
hdu动态规划算法集锦
hdu 1166线段树代码
hdu题目分类
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU图论题目分类