LightOJ 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]里面的数字,让你按照如下的两条规则来排序,
1.如果x的因子的个数大于y的因子的个数,那么y排在x的前面。
2.如果x的因子的个数等于y的因子的个数,且x>y,那么x排在y的前面。
解题思路:
直接打表,然后按照以上两条规则来sort就可以了。
代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<algorithm> 4 5 using namespace std; 6 7 # define MAX 1234 8 9 int a[MAX]; 10 11 struct node 12 { 13 int val; 14 int id; 15 }num[MAX]; 16 17 18 int cmp ( const struct node & x,const struct node & y ) 19 { 20 if ( x.val==y.val ) 21 { 22 return x.id > y.id; 23 } 24 return x.val < y.val; 25 } 26 27 28 void init() 29 { 30 for ( int i = 1;i <= 1000;i++ ) 31 { 32 num[i].id = i; 33 for ( int j = 1;j <= i;j++ ) 34 { 35 if ( i%j == 0 ) 36 { 37 num[i].val++; 38 } 39 } 40 } 41 } 42 43 44 45 int main(void) 46 { 47 init(); 48 sort(num+1,num+1001,cmp); 49 int icase = 1; 50 int t;scanf("%d",&t); 51 while ( t-- ) 52 { 53 int n;scanf("%d",&n); 54 printf("Case %d: ",icase++); 55 printf("%d\n",num[n].id); 56 57 } 58 59 60 return 0; 61 }
代码: