第一次期中考终于结束啦!沃老师是个语文老师,他在评学生的作文成绩时,给每位学生的分数都是一个小于10的非负小数。
沃老师并不会告诉大家分数的排名,但学生间竞争激烈,大家都想知道自己的作文成绩在班上的排名。 但由于作文分数的小数部分可能超级长,难以用肉眼比较两个数的大小关系,请你帮忙写个程序,把所有学生按照作文分数排序吧〜
输入
3
Amy 8.999999999999999999999999999999999999999990000
Bob 8.9999999999999999999999999999999999999999800
Cat 8.9999999999999999999999999999999999999999899999999
Amy 8.999999999999999999999999999999999999999990000
Bob 8.9999999999999999999999999999999999999999800
Cat 8.9999999999999999999999999999999999999999899999999
输出
Amy 8.999999999999999999999999999999999999999990000
Cat 8.9999999999999999999999999999999999999999899999999
Bob 8.9999999999999999999999999999999999999999800
Cat 8.9999999999999999999999999999999999999999899999999
Bob 8.9999999999999999999999999999999999999999800
思路:这题的坑点在于sort,需要一些骚操作;stable_sort,这个函数很骚;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
string mane;
string coer;
string zhen;
} a[2000010];
int cmp(node a,node b)
{
if(a.zhen==b.zhen)
{
return a.mane<b.mane;
}
return a.zhen>b.zhen;
}
int main()
{
int n,j,k,i;
cin>>n;
for(i=0; i<n; i++)
{
cin>>a[i].mane>>a[i].zhen;
a[i].coer=a[i].zhen;
for(j=a[i].zhen.size()-1; j>=0; j--)
{
if(a[i].zhen[j]=='0')
{
a[i].zhen.erase(a[i].zhen.begin()+j);
}
else
break;
}
}
stable_sort(a,a+n,cmp);
for(i=0; i<n; i++)
{
cout<<a[i].mane<<" "<<a[i].coer<<endl;
}
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <math.h>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof a)
#define LL long long
const int inf=0x3f3f3f3f;
const LL mod=1e9+7;
const int N=2e5+100;
using namespace std;
string a[N],b[N],c[N];
bool cmp(int x,int y)
{
if(c[x]==c[y]) return a[x]<a[y];
return c[x]>c[y];
}
int q[N];
int main()
{
int n;
scanf("%d",&n);
int lb;
for(int i=0;i<n;i++)
{
q[i]=i;
cin>>a[i]>>b[i];
c[i]=b[i];
lb=b[i].size();
for(int j=lb-1;j>=0;j--)
{
if(b[i][j]=='0') continue;
c[i].erase(j+1,lb-j+1);
break;
}
}
sort(q,q+n,cmp);
for(int i=0;i<n;i++)
cout<<a[q[i]]<<" "<<b[q[i]]<<endl;
}