lightoj1109 - False Ordering

  lightoj1109 - False Ordering

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

5

1

2

3

4

1000

   

Output for Sample Input

Case 1: 1

Case 2: 997

Case 3: 991

Case 4: 983

Case 5: 840


Beginner  的题目。主要是考查 sort 的结构体二级排序。

大概题意是说给你1000个数字,1--1000,分别计算出各个数的 约数 总个数,比如说 1 有多少个约数, 10 有多少个约数, eg:12有6个约数,分别为1,2,3,4,6,12,

然后按照约数的个数给这一千个数字排列,且由小到大。

排列规则如下 :

用div[ i ] 表示 i 的约数个数;

1):如果  div [ x ]  != div [ y ],  则按照div [ x ]  和 div [ y ] 的大小排序,如果div [ x ] < div [ y ], 则 x 排在y前面;

2):如果 div [ x ]  == div [ y ], 则按照 x 和 y 的大小排序,并且大的排在前面,如果 x  >  y , 则 x 排在y前面

这个过程要用到 C++ 的 sort 和结构体二级排序;
然后题目给出 n,就是求排在第 n 位的是哪个数字。


具体代码如下 :

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

struct order
{
    int num;
    int divisor;
}a[1010];
int cmp(order x, order y)
{
    if(x.divisor != y.divisor) return x.divisor < y.divisor;       //  < 表示升序
    else return x.num > y.num;             //   > 表示降序
}
int main()
{
    int Case, n, i, j;
    int num = 1;
    freopen("in.txt", "r",stdin);
    for(i = 1; i <= 1000; i++)
    {
        a[i].num = i;
        a[i].divisor = 0;             //  每个数的约数初始化为 0
        for(j = 1; j <= i; j++)
        if(i%j==0) a[i].divisor++;      //计算每个数的约数个数
    }
    sort(a+1, a+1001, cmp);   //  sort 排序,注意,a数组是从 1 开始排序的
    scanf("%d", &Case);
    while(Case--)
    {
        scanf("%d", &n);
        printf("Case %d: %d\n", num++, a[n].num);
    }
    return 0;
}

关于结构体二级排序不懂得自己去百度,网上有很多资料。

这个排序很好用,经常用到,还可以三级排序,甚至多级排序。

以上思路及代码希望可以帮到有需要的人。

如有出错,还望指出。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值