Lightoj1109——False Ordering(简单数学)
2016-04-16 11:22
183 查看
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.
Each case contains an integer n (1 ≤ n ≤ 1000).
找1-1000的数的因子个数,然后按照因子个数从大到小排序,如果因子个数相等,按照数最大的排在前面。还好数据规模不大,二重循环找因子个数也能过。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cmath>
#define MAXN 10000
using namespace std;
struct Number
{
int num,cnt;
};
Number numb[1000];
void init()
{
numb[0].num=1;
numb[0].cnt=1;
int i,j;
for(i=2;i<=1000;++i)
{
numb[i-1].num=i;
numb[i-1].cnt=0;
for(j=1;j<=i;++j)
{
if(i%j==0)
numb[i-1].cnt++;
}
}
}
bool cmp(Number a,Number b)
{
if(a.cnt==b.cnt)
return a.num>b.num;
else
return a.cnt<b.cnt;
}
int main()
{
init();
sort(numb,numb+1000,cmp);
int t,cnt=1,i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("Case %d: %d\n",cnt++,numb[n-1].num);
}
return 0;
}
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 |
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cmath>
#define MAXN 10000
using namespace std;
struct Number
{
int num,cnt;
};
Number numb[1000];
void init()
{
numb[0].num=1;
numb[0].cnt=1;
int i,j;
for(i=2;i<=1000;++i)
{
numb[i-1].num=i;
numb[i-1].cnt=0;
for(j=1;j<=i;++j)
{
if(i%j==0)
numb[i-1].cnt++;
}
}
}
bool cmp(Number a,Number b)
{
if(a.cnt==b.cnt)
return a.num>b.num;
else
return a.cnt<b.cnt;
}
int main()
{
init();
sort(numb,numb+1000,cmp);
int t,cnt=1,i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("Case %d: %d\n",cnt++,numb[n-1].num);
}
return 0;
}
相关文章推荐
- wifi增强
- 网页前端的异常处理方法、异常处理系统及异常处理服务器
- 手动崩溃统计
- IOS开发 - Info.plist跟pch文件的作用
- TCP/IP三次握手与四次挥手
- 学习的
- jQuery遍历json的方法分析
- JavaScript中的继承之借用构造函数
- [转载]java正则表达式
- Activiti 流程部署方式 activi 动态部署(高级源码篇)
- C++英文面试常见问题
- Activiti 流程部署方式 activi 动态部署(高级源码篇)
- 完美解决MySQL安装后无法远程连接的问题
- const用法
- 浙大 PAT Advanced level 1002. A+B for Polynomials
- visio二次开发——如何配置环境?
- Code Forces 21C Stripe 2
- FZU 2150 Fire Game (BFS_好题)
- OpenCV学习资料收集!
- LeetCode 107. Binary Tree Level Order Traversal II