Light OJ 1109 - False Ordering

                                                           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

 


題解:

題意是找出1000以內所有整數的因子數,並按以下規則排列,並輸出排列後的第n項。

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.



其中找因子的方法我用了類似於找素數的方法,類似於篩法,然後再利用快排,寫比較函數時按題目給出的規則來寫,比較簡單。


代碼:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct f
{
  int number;
  int value;
}a[1100];
int cmp(f a,f b)
{
  if(a.value<b.value)return 1;
  else if(a.value>b.value)return 0;
  else if(a.value==b.value)
  {
    if(a.number>b.number)return 1;
    else return 0;
  }
}
int main()
{
  for(int i=1;i<=1010;i++)
  {
    a[i].number=i;
    a[i].value=0;
  }
  for(int i=1;i<=1010;i++)
  {
    for(int j=i;j<=1010;j+=i)
    {
      a[j].value++;
    }
  }
  sort(a+1,a+1000+1,cmp);
  /*for(int i=1;i<=10;i++)
  {
    printf("i=%d %d\n",i,a[i].number);
  }*/
  int t,x=1;
  scanf("%d",&t);
  while(t--)
  {
    int n;
    scanf("%d",&n);
    printf("Case %d: %d\n",x++,a[n].number);
  }
  return 0;
}


發佈了41 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章