核算机考研复试_牛客_KY2~KY3-简书(考研复试机试考什么)

ky2——成果排序
标题描绘
查找和排序
标题:输入任意(用户,成果)序列,可以获得成果从高到低或从低到高的摆放,相同成果
都按先录入摆放在前的规则处置。
示例:
jack 70
peter 96
tom 70
smith 67
从高到低 成果
peter 96
jack 70
tom 70
smith 67
从低到高
smith 67
jack 70
tom 70
peter 96

输入描绘:

留心一个case里边有多组样例,请用循环处置输入
输入多行,先输入要排序的人的个数,然后输入排序办法0(降序)或许1(升序)再别离输入他们的名字和成果,以一个空格离隔。

输出描绘:

依照指定方法输知名字和成果,名字和成果之间以一个空格离隔

示例1

输入

3
0
fang 90
yang 50
ning 70

输出

fang 90
ning 70
yang 50

题解
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct student {
int id;
string name;
int score;
student(int id, string name, int score):id(id),name(name),score(score){}
};

bool less_fun(student a, student b){
return a.score == b.score ? a.id < b.id : a.score < b.score;
}
bool greater_fun(student a, student b){
return a.score == b.score ? a.id < b.id : a.score > b.score;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
vector<student> arr;
while(cin>>n){
arr.clear();
int mode;
cin>>mode;
for(int i=0;i<n;i++){
string name;
int score;
cin>>name>>score;
student temp = student(i, name, score);
arr.push_back(temp);
}
if(mode)
sort(arr.begin(), arr.end(), less_fun);
else
sort(arr.begin(), arr.end(), greater_fun);
for(int i=0;i<n;i++){
cout<<arr[i].name<<' '<<arr[i].score<<endl;
}
}
return 0;
}

ky3——约数的个数
标题描绘
输入n个整数,顺次输出每个数的约数的个数

输入描绘:

输入的第一行为n,即数组的个数(n<=1000)
接下来的1行包括n个整数,其间每个数的规模为(1<=num<=1000000000)

输出描绘:

可以有多组输入数据,关于每组输入数据,
输出n行,其间每一行对应上面的一个数的约数的个数。

示例1

输入

5
1 3 4 6 12

输出

1
2
3
4
6

思路
素数打表

题解
#include <stdio.h>
#include <string.h>

bool isprime[100007];
int prime[50007];

void initprime()
核算机考研复试_牛客_KY2~KY3-简书(考研复试机试考什么)插图
{
int cnt = 0;
memset(isprime, 1, 50001);
isprime[1] = false;
for(int i=2;i<=50000;i++){
if(isprime[i] == true) {
for(int j=2;i*j<50000;j++){
isprime[i*j] = false;
}
prime[cnt++] = i;
}
}
}

int main(){
initprime();
int cnt;
scanf("%d", &cnt);
while(cnt--){
int num, ans=1;
scanf("%d", &num);
for(int i=0;prime[i]*prime[i]<=num;i++) {
int k = 1;
while(num%prime[i]==0){
// printf("%d %d\n", cnt, num);
k++;
num /= prime[i];
}
ans *= k;
}
if(num != 1) ans *= 2;
printf("%d\n", ans);
}
return 0;
}

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注