标签:style blog http color os io strong ar for
1 /* 2 * Main.c 3 * 1020. 月饼 4 * Created on: 2014年8月31日 5 * Author: Boomkeeper 6 **********部分通过******** 7 */ 8 9 #include <stdio.h> 10 11 struct moonCake { 12 int restore; //库存量 13 int totalPrice; //总售价 14 float price; //单价,利润率 15 }; 16 17 /* 18 * 以单价 降序 排序每种月饼 19 */ 20 void sort(const int type, struct moonCake moonCakes[]) { 21 22 int i, j; 23 float temp_restore, temp_totalPrice, temp_price; 24 25 for (i = 0; i < type; i++) { 26 for (j = 0; j < type - i - 1; j++) { 27 if (moonCakes[j].price < moonCakes[j + 1].price) { 28 temp_restore = moonCakes[j].restore; 29 temp_totalPrice = moonCakes[j].totalPrice; 30 temp_price = moonCakes[j].price; 31 32 moonCakes[j].restore = moonCakes[j + 1].restore; 33 moonCakes[j].totalPrice = moonCakes[j + 1].totalPrice; 34 moonCakes[j].price = moonCakes[j + 1].price; 35 36 moonCakes[j + 1].restore = temp_restore; 37 moonCakes[j + 1].totalPrice = temp_totalPrice; 38 moonCakes[j + 1].price = temp_price; 39 } 40 } 41 } 42 43 // for(i=0;i<type;i++) 44 // printf("\n排序后:price : %f\n",moonCakes[i].price); 45 46 } 47 48 /* 49 * 比较市场需求量,计算最大利润 50 */ 51 void cal(const int type, const int amount, struct moonCake moonCakes[]) { 52 53 int i; 54 float bufferRestore = 0; //月饼存量缓冲池,用于与市场需求量amount做比较 55 float income = 0; 56 57 i=0; 58 while(bufferRestore<amount && i<type){ 59 bufferRestore += moonCakes[i].restore; 60 i++; 61 } 62 63 i--; 64 bufferRestore -= moonCakes[i].restore; 65 income = (float)(amount - bufferRestore) * moonCakes[i].price; 66 67 i--; 68 while (i >= 0) { 69 income += moonCakes[i].totalPrice; 70 i--; 71 } 72 73 printf("%.2f\n", income); 74 } 75 76 /* 77 * 计算每种月饼的利润率 78 */ 79 void calPrice(const int type, struct moonCake moonCakes[]) { 80 81 int i; 82 83 for (i = 0; i < type; i++) { 84 moonCakes[i].price = (double) moonCakes[i].totalPrice 85 / moonCakes[i].restore; 86 // printf("price = %f\n", moonCakes[i].price); 87 } 88 } 89 90 int main(void) { 91 92 struct moonCake moonCakes[1000]; //各种月饼 93 int amount, type; //市场最大需求量,月饼种类 94 int i; 95 96 //读取 97 scanf("%d %d", &type, &amount); 98 getchar(); 99 // printf("type = %d\n",type); 100 // printf("amount = %d\n",amount); 101 102 103 for (i = 0; i < type; i++) 104 scanf("%d", &(moonCakes[i].restore)); 105 getchar(); 106 for (i = 0; i < type; i++) 107 scanf("%d", &(moonCakes[i].totalPrice)); 108 109 // for(i=0;i<type;i++){ 110 // printf("restore : %d ; ",moonCakes[i].restore); 111 // printf("totalPrice : %d\n",moonCakes[i].totalPrice); 112 // } 113 114 //计算每种月饼的利润 115 calPrice(type, moonCakes); 116 117 //以单价 降序 排序 118 sort(type, moonCakes); 119 120 //比较市场需求量,计算最大利润 121 cal(type, amount, moonCakes); 122 123 return 0; 124 }
做这道题目过程中,发现很好的给变量或函数起名字也不是很容易。
至此,我把我能做出来的PAT(B)题目都写出来了,剩下的一些题目,要么是干脆没思路或参看别人的也看不懂的,要么就是有思路,但特别繁杂,自己也搞不清楚的。考试之前我尽量刷完,但还是要总结一下做过的题目,希望顺利。
题目链接:
http://pat.zju.edu.cn/contests/pat-b-practise/1020
.
标签:style blog http color os io strong ar for
原文地址:http://www.cnblogs.com/boomkeeper/p/1020b.html