With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.
Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.
For example, 3 games' odds are given as the following:
W T L 1.1 2.5 1.7 1.2 3.0 1.6 4.1 1.2 1.1
To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).
Input
Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.
Output
For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.
Sample Input1.1 2.5 1.7 1.2 3.0 1.6 4.1 1.2 1.1Sample Output
T T W 37.98分析:题目挺简单的,看代码就好,注意一点,在VisualStudio上调试出来,比如说用上面的输入例子,输出的结果为“T T W 37.97”,这个是因为输出的时候没有四舍五入
代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> double max3(double a[], int n, char *index) { double maxNum; maxNum = (a[0] > a[1])?a[0]:a[1]; maxNum = (maxNum > a[2])?maxNum:a[2]; if(maxNum == a[0]){ *index = 'W'; }else if(maxNum == a[1]){ *index = 'T'; }else if(maxNum == a[2]){ *index = 'L'; } return maxNum; } int main() { double input[3]; double maxOdd[3]; char index[3]; double tmpMax; double result; int i, j; for(i = 0; i < 3; i++){ scanf("%lf%lf%lf", &input[0], &input[1], &input[2]); maxOdd[i] = max3(input, 3, &index[i]); } result = (maxOdd[0]*maxOdd[1]*maxOdd[2]*0.65 - 1)*2; printf("%c %c %c %.2lf\n", index[0], index[1], index[2], result); return 0; }
以上为第1011题!
=============================================================
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A 310101 98 85 88 90 310102 70 95 88 84 310103 82 87 94 88 310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
Input
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.
Output
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output "N/A".
Sample Input5 6 310101 98 85 88 310102 70 95 88 310103 82 87 94 310104 91 91 91 310105 85 90 90 310101 310102 310103 310104 310105 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A分析:给定学生的学号以及三个科目的成绩,查询对应学生单科成绩排名与总分(平均分)排名中最高的那一个。
注意:
1.若一个学生的成绩排名有两个相同,则输出对应优先级的对应科目时按照A > C > M > E的优先级来;
2.此外,若几个学生同一门科目中,有多个学生的成绩相同,比如:88 88 89 排名应如下:1 1 3
思路:设计一个Student结构体,先输入所有的数据,再对每一门科目按照降序排列,如此,每一门科目对应的排名就出来了,再将此排名取代该可目录的成绩,最后处理完,Student中所有信息都是其在各科目中的排名。最后输出时只需对每个学生各自的不同的科目排名取最高(数字最小)的那个,并输出对应的科目代号,记住要考虑“注意事项2”。
代码:注意一下,在qsort之后需要对rank进行处理,这里简化为一个函数了,逻辑上更清晰。
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define min(a, b) ((a) < (b))?(a):(b) #define MaxNum 2000 typedef struct _Student{ char id[7]; int clang; //c programming language int math; //math int eng; //english int sum; //sum of 3 courses, can represent average //int rank; //best rank }Student; int minRank(int clang, int math, int eng, int sum, char *index) { int minR1 = 0; int minR2 = 0; int minR3 = 0; minR1 = min(clang, math); minR2 = min(eng, sum); minR3 = min(minR1, minR2); if(minR3 == sum){ *index = 'A'; }else if(minR3 == clang){ *index = 'C'; }else if(minR3 == math){ *index = 'M'; }else if(minR3 == eng){ *index = 'E'; } return minR3; } //*a > *b, return 1 //*a < *b, return -1 //*a == *b, return 0 int compareClang(const void *a, const void *b) { Student *x = (Student *)a; Student *y = (Student *)b; if(x->clang > y->clang){ return -1; }else if(x->clang < y->clang){ return 1; }else{ return 0; } } int compareMath(const void *a, const void *b) { Student *x = (Student *)a; Student *y = (Student *)b; if(x->math > y->math){ return -1; }else if(x->math < y->math){ return 1; }else{ return 0; } } int compareEng(const void *a, const void *b) { Student *x = (Student *)a; Student *y = (Student *)b; if(x->eng > y->eng){ return -1; }else if(x->eng < y->eng){ return 1; }else{ return 0; } } int compareSum(const void *a, const void *b) { Student *x = (Student *)a; Student *y = (Student *)b; if(x->sum > y->sum){ return -1; }else if(x->sum < y->sum){ return 1; }else{ return 0; } } int rankProcess(Student stu[], int n, char index) { int tmpRank = 0; int tmpScore = 0; int oldScore = 0; int i; int *tmpStu; for(i = 0; i < n; i++){ switch (index){ case 's': tmpStu = &stu[i].sum; break; case 'c': tmpStu = &stu[i].clang; break; case 'm': tmpStu = &stu[i].math; break; case 'e': tmpStu = &stu[i].eng; break; default: break; } tmpScore = *tmpStu; if(oldScore == tmpScore){ *tmpStu = tmpRank; }else{ tmpRank = i + 1; *tmpStu = tmpRank; } oldScore = tmpScore; } } int main() { int n, m; Student stu[MaxNum]; int i, j; int maxFinalRank[MaxNum]; int maxFinalRankTmp; char index[MaxNum]; char indexTmp; int tmpScore, oldScore; int tmpRank, highRank; int result, flag; char tmpStudent[7]; scanf("%d%d", &n, &m); for(i = 0; i < n; i++){ scanf("%s%d%d%d", (stu[i].id), &(stu[i].clang), &(stu[i].math), &(stu[i].eng)); stu[i].sum = stu[i].clang + stu[i].math + stu[i].eng; } qsort(stu, n, sizeof(Student), compareClang); rankProcess(stu, n, 'c'); //replace code below //tmpRank = 0; //tmpScore = 0; //oldScore = 0; //for(i = 0; i < n; i++){ // tmpScore = stu[i].clang; // if(oldScore == stu[i].clang){ // stu[i].clang = tmpRank; // }else{ // tmpRank = i + 1; // stu[i].clang = tmpRank; // } // oldScore = tmpScore; //} qsort(stu, n, sizeof(Student), compareMath); rankProcess(stu, n, 'm'); qsort(stu, n, sizeof(Student), compareEng); rankProcess(stu, n, 'e'); qsort(stu, n, sizeof(Student), compareSum); rankProcess(stu, n, 's'); result = 0; for(i = 0; i < m; i++){ flag = 0; scanf("%s", tmpStudent); for(j = 0; j < n; j++){ result = strncmp(tmpStudent, stu[j].id, 7); if(result == 0){ //found flag = 1; maxFinalRankTmp = minRank(stu[j].clang, stu[j].math, stu[j].eng, stu[j].sum, &indexTmp); break; }else{ continue; } } if(flag == 0){ maxFinalRank[i] = 0; }else{ maxFinalRank[i] = maxFinalRankTmp; index[i] = indexTmp; } } for(i = 0; i < m; i++){ if(maxFinalRank[i] != 0){ printf("%d %c\n", maxFinalRank[i], index[i]); }else{ printf("N/A\n"); } } return 0; }
以上为第1012题!
=============================================================
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input3 2 3 1 2 1 3 1 2 3Sample Output
1 0 0
分析:给定一张图,上面有城市以及连接城市的公路,要求计算,当某个城市被占领了之后,与其相连的公路全部不能用了,即相当于从图中去掉了这个节点以及他所连接的线。之后,计算去掉上面的节点之后,需要添加多少条线才可以保证每个整个图是联通的,可以转化为,去掉上面的节点之后,有多少个“连通分量”,其个数再-1就是所谓的需要添加的线的条数。
用DFS的思路来做。
代码中有一段有点问题,提交的时候case3一直不能通过,逻辑不成立的地方在于,我们无法确定nodes[tmpcity].next是存在的。没必要以tmpcity作为出发点的,直接遍历就可以。
//for w adjacent to v, dfs w // edge = nodes[tmpcity].next; // while(edge != NULL){ // if(!visited[edge->adjvex]){ // dfs(edge->adjvex); // components[i]++; // } // edge = edge->next; // } for (j = 1; j <= n; ++ j) { if (!visited[j]) { dfs(j); components[i] ++; } }
代码:
#include <stdio.h> #include <stdlib.h> #define MaxN 1001 typedef struct _Node{ int adjvex; struct _Node *next; }Node; int visited[MaxN]; Node nodes[MaxN]; void InsertEdge(int head, int tail) { Node *hEdge; Node *tEdge; hEdge = malloc(sizeof(Node)); if(hEdge == NULL){ perror("Our of space!"); exit(1); } hEdge->adjvex = tail; hEdge->next = nodes[head].next; nodes[head].next = hEdge; tEdge = malloc(sizeof(Node)); if(tEdge == NULL){ perror("Out of space!"); exit(1); } tEdge->adjvex = head; tEdge->next = nodes[tail].next; nodes[tail].next = tEdge; return; } void dfs(int index) { Node *tmpEdge; visited[index] = 1; tmpEdge = nodes[index].next; while(tmpEdge != NULL){ if(!visited[tmpEdge->adjvex]){ dfs(tmpEdge->adjvex); } tmpEdge = tmpEdge->next; } } int main() { int n, m, k; int i, j; int city1, city2; int tmpcity; Node *edge; int components[MaxN]; scanf("%d%d%d", &n, &m, &k); for(i = 1; i <= n; i++){ nodes[i].adjvex = i; nodes[i].next = NULL; } for(i = 0; i < m; i++){ scanf("%d%d", &city1, &city2); InsertEdge(city1, city2); } for(i = 1; i <= k; i++){ if(k != 1){ components[i] = 0; scanf("%d", &tmpcity); for(j = 0; j <= n; j++){ visited[j] = 0; } visited[tmpcity] = 1; ////for w adjacent to v, dfs w //edge = nodes[tmpcity].next; //while(edge != NULL){ // if(!visited[edge->adjvex]){ // dfs(edge->adjvex); // components[i]++; // } // edge = edge->next; //} for (j = 1; j <= n; ++ j) { if (!visited[j]) { dfs(j); components[i] ++; } } }else{ components[i] = 1; scanf("%d", &tmpcity); } } for(i = 1; i <= k; i++){ printf("%d\n", components[i] - 1); } return 0; }
以上为第1013题!
=============================================================
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.
At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.
Input
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).
The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
Output
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.
Sample Input2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7Sample Output
08:07 08:06 08:10 17:00 Sorry分析:分析起来其实挺简单,银行有 N 个窗口,每个窗口允许排队 M 个人,如果 N 个窗口占满了排队的人,更多的人需要在黄线外等待。 每个人都会选择尽可能人少的窗口排队,如果有多个窗口等待队列一样长,则选择编号小的窗口。 前 N 个用户设定在 8:00 开始接受服务。给定了 K 个用户所需要接受服务的时间长度,要求在这个排队模型下,每个人的完成处理的时间点。
注意:17:00前排上队的用户可以得到服务,而17:00之后的用户就不能进入队列了。
弄死不少脑细胞啊。
开始代码有两个case不能通过,发现第113行代码(之前116行代码&&之后那部分在113行),这样的话在后面的for循环中customerLeft就可能变成<0,故注释掉113,改成现在的116就ok了!
代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MaxCustomers 1000 #define TimeLimit 60*9 int time[MaxCustomers + 1]; //processing time of customer use int result[MaxCustomers + 1]; typedef struct _Person{ int id; int timeLeft; }Person; typedef struct _Queue{ int capacity; int size; Person p[11]; //use a[1]~a[10] as the queue }Queue; Queue *initQueue(int n) { Queue *q; q = malloc(sizeof(Queue)); if(q == NULL){ perror("Out of space!"); exit(1); } memset(q, 0, sizeof(Queue)); q->capacity = n; return q; } //return 0 if queue is not full and enqueue //return 1 if queue is full int enQueue(Person *p, Queue *q) { if(q->size != q->capacity){ //not full q->size++; q->p[q->size].id = p->id; q->p[q->size].timeLeft = p->timeLeft; return 0; }else{ return -1; } } //return the element when queue is not empty //return -1 when queue is empty void deQueue(Queue *q) { int i; int tmp; if(q->size == 0){ //empty return; }else{ for(i = 1; i <= q->size; i++){ q->p[i - 1].id = q->p[i].id; q->p[i - 1].timeLeft = q->p[i].timeLeft; } q->p[0].id = 0; q->p[0].timeLeft = 0; q->p[q->size].id = 0; q->p[q->size].timeLeft = 0; q->size--; return; } } int main() { int n, m, k, q; int i, j; int customerLeft; Queue *windows[21]; //use windows[1]~windows[20] Queue *tmpQueue; Person *tmpPerson; int minTime; int timer = 0; int query = 0; tmpPerson = malloc(sizeof(Person)); if(tmpPerson == NULL){ perror("Out of space!."); exit(1); } scanf("%d%d%d%d", &n, &m, &k, &q); for(i = 1; i <= n; i++){ //init n queues windows[i] = initQueue(m); } for(i = 1; i <= k; i++){ //input processing time of k customers scanf("%d", &time[i]); } customerLeft = k; while(1){ /************************************************************************/ /* 17:00之前进入的人,就算结束时间晚于17:00可以得到服务 */ /* 而晚于17:00之后的就算想进入即不再加入queue中 */ /* 故一开始就要判断时间是否超出界限 */ /************************************************************************/ if(timer < TimeLimit){ //若没有剩下的客户需要得到服务,就直接继续,否则按规则插入到队列中 //两种情况:开始全部为空时的插入,以及满的时候选择最短的插入 //规则:找最短队列,若有多条队伍一样长,找下标最小的那条 //if(customerLeft > 0){ for(i = 0; i < m; i++){ for(j = 1; j <= n; j++){ if(windows[j]->size == i && customerLeft > 0){ //队伍都满的时候选择插入需要判断的次数较多 tmpPerson->id = k - (customerLeft--) + 1; //ID:1~n tmpPerson->timeLeft = time[tmpPerson->id]; enQueue(tmpPerson, windows[j]); //插入队列中 } } } //} } //找到所有队伍最开头那个人需要服务的时间的最小值 minTime = 0; for(i = 1; i <= n; i++){ if(windows[i]->size != 0){ if(minTime == 0 || minTime > windows[i]->p[1].timeLeft){ minTime = windows[i]->p[1].timeLeft; } } } timer += minTime; //如果没有下一个离开窗口的时间,则表明所有人都服务完毕 if(minTime == 0){ break; } for(i = 1; i <= n; i++){ if(windows[i]->size != 0){ //队首每个人都消耗相同的最小时间,故每个人所剩时间都需减去minTime windows[i]->p[1].timeLeft -= minTime; //与之前找到minTime的相同所有队列首元素就是要同时离开的人 if(windows[i]->p[1].timeLeft == 0){ result[windows[i]->p[1].id] = timer; //保存对应人所离开的时间 deQueue(windows[i]); //17:00之前加入的,就算结束时间17:00之后也可以得到服务,故 //先出队,再将队列中剩余元素全部清空,可看case分析,第6次循环的分析 if(timer >= TimeLimit){ while(windows[i]->size != 0){ deQueue(windows[i]); } } } } } } for(i = 0; i < q; i++){ scanf("%d", &query); if(result[query] == 0){ printf("Sorry\n"); }else{ printf("%02d:%02d\n", 8 + result[query]/60, result[query]%60); } } return 0; }
以上为第1014题!
=============================================================
还有两天就PAT了,暂时代码先放放,主要看思路,弄懂题目以及解题方法先。
=============================================================
A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (< 105) and D (1 < D <= 10), you are supposed to tell if N is a reversible prime with radix D.
Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.
Output Specification:
For each test case, print in one line "Yes" if N is a reversible prime with radix D, or "No" if not.
Sample Input:73 10 23 2 23 10 -2Sample Output:
Yes Yes No分析:原数字与其翻转之后的数字,按照相同的radix计算得到的整数值,判断是否都为Prime。
代码:以后再写
以上为第1015题!
=============================================================
A long-distance telephone company charges its customers by the following rules:
Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.
Input Specification:
Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.
The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.
The next line contains a positive number N (<= 1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word "on-line" or "off-line".
For each test case, all dates will be within a single month. Each "on-line" record is paired with the chronologically next record for the same customer provided it is an "off-line" record. Any "on-line" records that are not paired with an "off-line" record are ignored, as are "off-line" records not paired with an "on-line" record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.
Output Specification:
For each test case, you must print a phone bill for each customer.
Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.
Sample Input:10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10 10 CYLL 01:01:06:01 on-line CYLL 01:28:16:05 off-line CYJJ 01:01:07:00 off-line CYLL 01:01:08:03 off-line CYJJ 01:01:05:59 on-line aaa 01:01:01:03 on-line aaa 01:02:00:01 on-line CYLL 01:28:15:41 on-line aaa 01:05:02:24 on-line aaa 01:04:23:59 off-lineSample Output:
CYJJ 01 01:05:59 01:07:00 61 $12.10 Total amount: $12.10 CYLL 01 01:06:01 01:08:03 122 $24.40 28:15:41 28:16:05 24 $3.85 Total amount: $28.25 aaa 01 02:00:01 04:23:59 4318 $638.80 Total amount: $638.80
分析:长话公司收费问题。逻辑不能搞乱了。
流程图先画着,回头贴。
代码:
以上为第1016题!
=============================================================
=============================================================
=============================================================
=============================================================
=============================================================
=============================================================