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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值