例题5-10 UVA 207 PGA Tour Prize Money PGA巡回赛的奖金
解题:
1)fegets()函数
函数原型
char * fgets ( char * str, int num, FILE * stream );
参数
*buf: 字符型指针,指向用来存储所得数据的地址。
bufsize: 整型数据,指明存储数据的大小。
*stream: 文件结构体指针,将要读取的文件流。
含义:
从文件结构体指针stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中,每次最多读取bufsize-1个字符(第bufsize个字符赋' '),如果文件中的该行,不足bufsize个字符,则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行,但是,缓冲区总是以NULL字符结尾,对fgets的下一次调用会继续读该行。函数成功将返回buf,失败或读到文件结尾返回NULL。
用法:
#include<string.h>
#include<stdio.h>
int main ( void )
{
FILE*stream;
char string[]="This is a test";
char msg[20];
stream=fopen("tmp.dat","w+");
fwrite(string,strlen(string),1,stream);
fseek(stream,0,SEEK_SET);
fgets(msg,strlen(string)+1,stream);
printf("%s",msg);
fclose(stream);
return 0;
}
2)strchr
char *strchr(const char *s, int c)
功能: 查找字符串s中首次出现c字符的位置
说明: 返回首次出现c的位置的指针,返回的地址是被查找的字符串指针开始的第一个与c相同字符的指针,若s中不存在c则返回NULL。
返回值: 成功返回要查找的字符第一次出现的位置,否则返回NULL。
下属程序奖金计算错误,重点在于学习输出吧,不改了。。。
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 using namespace std; 6 7 const int maxNum = 144+10;//选手的最大人数 8 const int maxt = 50; 9 const int DQ = 99999; 10 11 struct people{ 12 char name[maxt]; 13 int rd[4]; 14 int total2,total4; 15 int turnNum;//表示参加的轮数,排序时使用 16 bool isOut;//是外行为1 17 bool outLine;//违规为1 18 bool binglie;//并列的话为1 19 int place; 20 int comPlaceNum; 21 double money; 22 }player[maxNum]; 23 24 bool compare1(people a,people b) 25 {///判断那个选手晋级,升序排列 26 if(a.total2 != b.total2) return a.total2 < b.total2; 27 return strcmp(a.name,b.name)<0; 28 } 29 bool compare2(people a,people b) 30 {///晋级后判断前70名 31 if(a.total4 != b.total4) return a.total4 < b.total4; 32 return strcmp(a.name,b.name)<0; 33 } 34 35 bool compare3(people a,people b) 36 {///判断那个选手晋级,升序排列 37 if(a.turnNum != b.turnNum) return a.turnNum > b.turnNum;//并列,先按照轮数排序 38 else if(a.total4 != b.total4) return a.total4 < b.total4; 39 return strcmp(a.name,b.name)<0; 40 } 41 42 int reLen(int a) 43 {///计算整数a的位数 44 int w = 1; 45 while(a/10) {w++;a = a/10;} 46 return w; 47 } 48 49 int main() 50 { 51 int T; 52 cin>>T;//数据组数 53 while(T--){ 54 memset(player,0,sizeof(player)); 55 double totalMoney,moneyRatio[71]; 56 cin>>totalMoney;//总奖金 57 for(int i=1;i<=70;i++) 58 cin>>moneyRatio[i]; 59 int number;//人数,不会超过144 60 cin>>number; 61 getchar(); 62 ///输入选手的信息 63 for(int i=0;i<number;i++) 64 { 65 fgets(player[i].name,20,stdin); 66 if(strchr(player[i].name,'*')) player[i].isOut = 1;//是业余选手 67 for(int j=0;j<4;j++) 68 { 69 player[i].outLine = 0; 70 player[i].turnNum++; 71 if(!(scanf("%d",&player[i].rd[j]))){ 72 player[i].rd[j] = DQ; 73 player[i].outLine = 1;//违规 74 player[i].turnNum --; 75 } 76 if(j<2) player[i].total2 += player[i].rd[j]; 77 player[i].total4 += player[i].rd[j]; 78 if(player[i].outLine == 1) break; 79 } 80 char ch[45]; 81 fgets(ch,40,stdin); 82 } 83 sort(player,player+number,compare1); 84 int win2Num = 0,grade = 0; 85 int num2,num4=0; 86 for(num2 = 0;num2<number;num2++) 87 { 88 if(player[num2].total2 > grade) 89 { 90 win2Num=num2+1;//考虑并列的情况,win2Num表示当前名次 91 grade = player[num2].total2; 92 } 93 if(win2Num>70) break; 94 } 95 ///num2表示晋级的人数 96 97 sort(player,player+num2,compare2); 98 win2Num = 0;grade = 0; 99 for(int temp = 0;temp<num2;temp++) 100 { 101 if(player[temp].total4 > grade) 102 { 103 for(int l = 1;l<=temp+1-win2Num;l++) 104 player[temp-l].comPlaceNum = temp+1-win2Num; 105 win2Num=temp+1;//考虑并列的情况,win2Num表示当前名次 106 player[temp].place = win2Num; 107 grade = player[temp].total4; 108 }else{ 109 player[temp].place = win2Num; 110 player[temp].binglie = 1; 111 player[temp-1].binglie = 1; 112 } 113 /* 114 if((temp+1)>win2Num)//出现并列 115 { 116 player[temp].binglie = 1; 117 player[temp-1].binglie = 1; 118 }*/ 119 if(temp == num2-1) 120 { 121 if(player[temp].place == 0) 122 { 123 for(int l = 0;l<temp+2-win2Num;l++) 124 player[temp-l].comPlaceNum = temp+2-win2Num; 125 } 126 } 127 if(win2Num > 70 && num4 == 0) num4 = temp; 128 } 129 ///num4为前70名的人数 130 if(num4 == 0) num4 = num2; 131 132 ///计算每一名的奖金 133 double later = 0;//前一名的奖金比例 134 int NowP = 0;double M; 135 for(int temp=0;temp<num4;temp++) 136 { 137 if(player[temp].place > NowP)//当前名次的第一个人 138 { 139 double RatioT = 0; 140 for(int k=player[temp].place;k<(player[temp].place+player[temp].comPlaceNum-1);k++) 141 { 142 RatioT += moneyRatio[k]; 143 } 144 M = ((RatioT/100.0)*totalMoney+later)/player[temp].comPlaceNum; 145 later = 0; 146 NowP = player[temp].place; 147 } 148 if(player[temp].isOut) later += M; 149 else player[temp].money = M; 150 } 151 152 153 printf("Player Name Place RD1 RD2 RD3 RD4 TOTAL Money Won "); 154 printf("----------------------------------------------------------------------- "); 155 for(int i = 0;i<num2;i++) 156 { 157 printf("%-21s",player[i].name); 158 int N = 10; 159 if(player[i].outLine != 1) 160 { 161 printf("%d",player[i].place);N-=reLen(player[i].place); 162 if(player[i].binglie == 1){printf("T");N--;} 163 } 164 165 for(int temp = N;temp>0;temp--) 166 cout<<" "; 167 //cout<<endl; 168 ///输出各轮的成绩 169 N = 4; 170 for(int temp = 0;temp<4;temp++) 171 { 172 if(player[i].rd[temp] != DQ) printf("%-5d",player[i].rd[temp]); 173 else {N -= temp;break;} 174 } 175 if(N != 4) 176 { 177 for(int temp=0;temp<N;temp++) printf(" "); 178 } 179 if (N==4)N=0; 180 if(N){printf("DQ ");continue;} 181 if(!player[i].isOut) 182 { 183 printf("%-10d",player[i].total4);printf("$%9.2lf",player[i].money+1e-8); 184 } 185 else printf("%d",player[i].total4); 186 printf(" "); 187 } 188 189 } 190 191 return 0; 192 }
测试样例:
1 1000000.00 18 10.8 6.8 4.8 2.8 1.8 1.7 1.6 1.5 1.49 1.48 1.47 1.46 1.45 1.44 1.43 1.42 1.41 1.4 1.39 1.38 1.37 1.36 1.35 1.34 1.33 1.32 1.31 1.3 1.29 1.28 1.27 1.26 1.25 1.24 1.23 1.22 1.21 1.2 1.19 1.18 1.17 1.16 1.15 1.14 1.13 1.12 1.11 1.1 1.09 1.08 1.07 1.06 1.05 1.04 1.03 1.02 1.01 1 0.99 0.98 0.97 0.96 0.95 0.94 0.93 0.92 0.91 0.202 0.2 140 abcWALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 abbWALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 aabWALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 aaaWALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE* 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ SID SHANKER* 90 99 62 61 SID SHANKER* 90 99 62 62 JIMMY ABLE 69 73 80 DQ WALLY WEDGE 70 70 70 71 WALLY WEDGE 70 70 70 70 SANDY LIE 80 DQ
想看到AC代码的移步=》 例题5-10 UVA 207 PGA Tour Prize Money PGA巡回赛的奖金