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的排序问题,按因子数从小到大排列,如果因子数相同,按数字从大到小排列。
不会看什么时间复杂度,结果试了各种方法都被否定了。看来别人代码才知道,由于数字给的小,完全可以两重for的,暴力一下就ok了。
#include<cstdio>
#include<algorithm>
using namespace std;
struct st
{
int num,div;
}a[1010];
bool cmp(st a,st b)
{
if(a.div==b.div)
return a.num>b.num;
return a.div<b.div;
}
int main()
{
int t,flag=1;
scanf("%d",&t);
a[1].num=1;
a[1].div=1;
for(int i=2;i<=1000;i++)
{
a[i].num=i;
for(int j=1;j<=i;j++)
{
if(i%j==0)
a[i].div++;
}
}
sort(a+1,a+1001,cmp);
while(t--)
{
int n;
scanf("%d",&n);
printf("Case %d: %d\n",flag++,a[n].num);
}
return 0;
}