LIGHT OJ 1109 - False Ordering 【因子个数】

本文介绍了一道编程题目1109-FalseOrdering的解题思路及AC代码。题目要求根据整数因子数量进行排序并输出第n个数。通过预处理1到1000所有整数的因子个数并使用结构体存储,最后根据因子数量进行排序。

1109 - False Ordering

Time Limit: 1 second(s)Memory Limit: 32 MB

We define b is a Divisor of a number a if a is divisible by b. So, the divisors of 12 are 1, 2, 3, 4, 6, 12. So, 12 has 6 divisors.

Now you have to order all the integers from 1 to 1000. x will come before y if

1)                  number of divisors of x is less than number of divisors of y

2)                  number of divisors of x is equal to number of divisors of y and x > y.

Input

Input starts with an integer T (≤ 1005), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 1000).

Output

For each case, print the case number and the nth number after ordering.

Sample Input

Output for Sample Input

5

1

2

3

4

1000

Case 1: 1

Case 2: 997

Case 3: 991

Case 4: 983

Case 5: 840


 

题意:求1-1000内数的因子个数;

思路:1000个数 1000*1000不会超时,直接找因子个数就行(用素数筛的方法和这差不多,优化不了多少),定义结构体,保存数字和该数的因子个数,按照条件排序,预处理一下,直接输出就行;

失误:没有判断i是不是i的因子,虽然没影响,但是还是失误了,没有考虑清楚。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Node{
	int ord;
	int num;
}stu[1111];

bool cmp(Node a,Node b)
{
	if(a.num!=b.num) 
	return a.num<b.num;
	return a.ord>b.ord;
}

int main()
{
	int i,j,T,N,Kase=0;
	memset(stu,0,sizeof(stu));
	for(i=1;i<=1004;++i)
	{
		stu[i].ord=i;
		for(j=1;j<=i;++j)
		{
			if(i%j==0) stu[i].num++;
		}
	}
	sort(stu+1,stu+1000+1,cmp);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&N);
		printf("Case %d: %d\n",++Kase,stu[N].ord);
		
	}
	return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值