#include<iostream>
#include<algorithm>
using namespace std;
struct stu
{
int num;//编号
int c,m,e;
int sum;
}student[310];
bool cmp(stu a,stu b)
{
if(a.sum>b.sum) return 1;
else if(a.sum<b.sum) return 0;
else
{
if(a.c>b.c) return 1;
else if(a.c<b.c) return 0;
else
{
if(a.num>b.num) return 0;
else return 1;
}
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
student[i].num=i;//录入编号
cin>>student[i].c>>student[i].m>>student[i].e;//输入
student[i].sum=student[i].c+student[i].m+student[i].e;//计算总分
}
sort(student+1,student+1+n,cmp);
for(int i=1;i<=5;i++)
cout<<student[i].num<<' '<<student[i].sum<<endl;
return 0;
}
在sort中给出cmp 可以得出从大到小的排序;
bool cmp{
if(ture) return 1;
else if(false){
return 0;
}
}
题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前 55 名学生发奖学金。期末,每个学生都有 33 门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的 33 门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前 55 名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
77 279279
55 279279
这两行数据的含义是:总分最高的两个同学的学号依次是 77 号、55 号。这两名同学的总分都是 279279 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为 77 的学生语文成绩更高一些。如果你的前两名的输出数据是:
55 279279
77 279279
则按输出错误处理,不能得分。
输入格式
共 �+1n+1行。
第 11 行为一个正整数�(≤300)n(≤300),表示该校参加评选的学生人数。
第 22 到 �+1n+1 行,每行有 33 个用空格隔开的数字,每个数字都在 00 到 100100 之间。第 �j 行的 33 个数字依次表示学号为 �−1j−1 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 1∼�1∼n(恰好是输入数据的行号减 11)。
所给的数据都是正确的,不必检验。
//感谢 黄小U饮品 修正输入格式
输出格式
共 55 行,每行是两个用空格隔开的正整数,依次表示前 55 名学生的学号和总分。
输入输出样例
输入 #1复制
6 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
输出 #1复制
6 265 4 264 3 258 2 244 1 237
输入 #2复制
8 80 89 89 88 98 78 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
输出 #2复制
8 265 2 264 6 264 1 258 5 258
玩了这么多年的扫雷,今天终于玩明白了
#include<bits/stdc++.h>
using namespace std;
char a[101][101];//地图
int b[101][101]={0};//答案
int n,m,i,j;
int dx[8]={1,0,-1,0,-1,1,1,-1};//方向
int dy[8]={0,1,0,-1,-1,-1,1,1};//方向
void dfs(int x,int y)//调用函数将地雷周围的数加起来
{
int nx,ny,k;
for (k=0;k<8;k++)//循环,将8个方向加起来。(不需要判断地雷,因为输出有判断)
{
nx=x+dx[k];
ny=y+dy[k];
if (nx>=1&&nx<=n&&ny>=1&&ny<=m)//判断有没有越界
b[nx][ny]++;//加一
}
}
int main()
{
cin>>n>>m;//输入
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
cin>>a[i][j];
if (a[i][j]=='*')
dfs(i,j);//如果是地雷就调用函数
}
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
if (a[i][j]=='*')//判断是地雷就直接输出
cout<<a[i][j];
else
cout<<b[i][j];//不是地雷输出答案
cout<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
/*struct node{
int num,c,m,e,sum;
}p[301];*/
char ch[1001][1001];
void solve(){
}
int cnt;
int dir(int x,int y){
cnt=0;
if(ch[x-1][y]=='*'){
cnt++;
}
if(ch[x+1][y]=='*'){
cnt++;
}
if(ch[x][y-1]=='*'){
cnt++;
}
if(ch[x][y+1]=='*'){
cnt++;
}
if(ch[x-1][y-1]=='*'){
cnt++;
}
if(ch[x-1][y+1]=='*'){
cnt++;
}
if(ch[x+1][y-1]=='*'){
cnt++;
}
if(ch[x+1][y+1]=='*'){
cnt++;
}
ch[x][y]=cnt;
}
//int cnt,cntl,total;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;
cin>>c;
ch[i][j]=c;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(ch[i][j]=='*')
cout<<ch[i][j];
else {
int t=dir(i,j);
cout<<t;
}
// if(ch[i][j]!='*'&&ch[i-1][j])
}
printf("\n");
}
}