A. Olympic Ranking
题意
n个国家,给出国家的名字和获得的金银铜牌数。要求输出rank1的国家名字。排名规则:先选金牌多的,如果都相等就选银牌多的,否则选铜牌多的。
思路与代码
很简单的一个结构体排序,需要注意的是国家名称的读入具有一定技巧。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2000007;
const int INF = 0x3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1);
inline ll read(){
ll x = 0, f = 1; char ch; ch = getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
string s;
struct node{
int au,ag,cu;
string name;
}q[305];
int cmp(node a,node b)
{
if(a.au!=b.au)
return a.au>b.au;
else
{
if(b.ag!=a.ag)
return a.ag>b.ag;
else
return a.cu>b.cu;
}
}
void solve()
{
n=read();
for(int i=1;i<=n;i++)
{
q[i].au=read(),q[i].ag=read(),q[i].cu=read();
cin>>s;
q[i].name=s;
while(getchar()!='\n')
{
cin>>s;
// if(q[i].name.size()==0)
// q[i].name=s;
// else
q[i].name=q[i].name+" "+s;
}
// cout<<Endl<<"q[i].name="<<q[i].name<<Endl;
}
sort(q+1,q+1+n,cmp);
cout<<q[1].name<<Endl;
}
int main() {
//for(int T = read(); T; T--)
solve();
return 0;
}
B. Aliquot Sum
题意
s(n)=所有除了n以外的的除数之和。比较s(n)与n并按要求输出。
思路和代码
预处理出s(n)即可。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2000007;
const int INF = 0x3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1);
inline ll read(){
ll x = 0, f = 1; char ch; ch = getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll n;
ll ans[1000005];
void init()
{
for(int i=1;i<=1000005;i++)
{
for(int j=i*2;j<=1000005;j+=i)
ans[j]+=i;
}
}
void solve()
{
n=read();
if(ans[n]>n)
{
cout<<"abundant"<<Endl;
}
else if(ans[n]<n)
{
cout<<"deficient"<<Endl;
}
else cout<<"perfect"<<Endl;
}
int main() {
init();
for(int T = read(); T; T--)
solve();
return 0;
}
J. JavaScript
题意
给出string x和y,如果x和y中含有英文字母输出NaN,否则输入x-y的值。
代码与思路
扫描x和y,发现有不是数字的就break掉输出NaN即可。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2000007;
const int INF = 0x3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1);
inline ll read(){
ll x = 0, f = 1; char ch; ch = getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
string x,y;
bool flag;
int get(string q){
int ans=0;
for(int i=0;i<q.length();i++)
ans=ans*10+q[i]-'0';
return ans;
}
void solve()
{
flag=1;
cin>>x>>y;
for(int i=0;i<x.length();i++)
if(!isdigit(x[i]))
{
flag=0;
break;
}
for(int i=0;i<y.length();i++)
if(!isdigit(y[i]))
{
flag=0;
break;
}
if(!flag)
{
cout<<"NaN"<<Endl;
}
else
{
cout<<get(x)-get(y)<<Endl;;
}
}
int main() {
// for(int T = read(); T; T--)
solve();
return 0;
}