題目地址:http://acm.hdu.edu.cn/showPRoblem.php?pid=2093
/************************************************************************
這題廢了一番功夫。。
首先題目沒給出人數,乍一看懵了。參考了網上的代碼,用 while(scanf("%s",name)!=EOF)來結束人的輸入,鍵盤同時按住ctrl+z,輸入文件結束符,再回車,計算排名。同時把存儲人的數組定義的大一點(10000為例);
其次是數據的輸入竟然還有括號,原想用getchar()來接收并檢測是不是括號,但容易出錯,干脆全部用%s接收成字符串,再寫個函數轉化為純數字。
/****************************************************************************************
代碼如下:
/*************************
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>using namespace std;struct person{ char name[11]; int accepted; int punish;}acmer[10000];int n,m;//n題目數,m懲罰分 int cmp(const void *q,const void *w){ person *a=(person*)q,*b=(person*)w; if(a->accepted!=b->accepted) return b->accepted-a->accepted; if(a->punish!=b->punish) return a->punish-b->punish; return strcmp(a->name,b->name);}int to_int(char *p)//此函數將字符串中的數字轉化為int型數據 { if(*p=='-'||*p=='0') return 0;//題目沒做對,不做統計,直接0 int punish=0,temp=0; while(*p) { if(*p=='(') { while(*(++p)!=')') { temp=temp*10+*p-'0'; } break; } punish=punish*10+*p-'0'; p++; } return punish+temp*m;}int main(){ char a[12]; int score,i=0; scanf("%d%d",&n,&m); while(scanf("%s",acmer[i].name)!=EOF) { acmer[i].accepted=0; acmer[i].punish=0; for(int j=0;j<n;j++) { scanf("%s",a); score=to_int(a); if(score>0) { acmer[i].accepted++; acmer[i].punish+=score; } } i++; } qsort(acmer,i,sizeof(acmer[0]),cmp); for(int j=0;j<i;j++) { printf("%-10s %2d %4d/n",acmer[j].name,acmer[j].accepted,acmer[j].punish); } return 0;}
新聞熱點
疑難解答