标签:style blog class code c tar
【2014/05/15】今晚做了一下CTU 2008的这套题,最后的rank是3道题。基本是水题啊,我们做的是4个小时,如果完整做,我想应该还会出掉D题,主要是D题很繁琐,weikd写起都说烦。
【题意】纯模拟题,模拟银行存钱,取钱,转钱的操作。注意下细节~:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <map> 5 #include <string> 6 #define EPS 1e-8 7 using namespace std; 8 9 char str[1005]; 10 char cmd[1005]; 11 char name[1005],name1[1005]; 12 map<string,double> ac; 13 14 void create(string id) 15 { 16 printf("create: "); 17 if (ac.find(id)!=ac.end()) 18 { 19 printf("already exists\n"); 20 } 21 else 22 { 23 ac[id] = 0; 24 printf("ok\n"); 25 } 26 } 27 28 void deposit(string id, double x) 29 { 30 printf("deposit %.2f: ",x); 31 if (ac.find(id)==ac.end()) 32 { 33 printf("no such account\n"); 34 } 35 else 36 { 37 ac[id] = ac[id]+x; 38 printf("ok\n"); 39 } 40 } 41 42 void withdraw(string id,double x) 43 { 44 printf("withdraw %.2f: ",x); 45 if (ac.find(id)==ac.end()) 46 { 47 printf("no such account\n"); 48 } 49 else 50 { 51 if (ac[id]+EPS<x) 52 { 53 printf("insufficient funds\n"); 54 } 55 else 56 { 57 ac[id]=ac[id]-x; 58 printf("ok\n"); 59 } 60 } 61 } 62 63 void transfer(string ida,string idb,double x) 64 { 65 printf("transfer %.2f: ",x); 66 if (ac.find(ida)==ac.end()||ac.find(idb)==ac.end()) 67 { 68 printf("no such account\n"); 69 } 70 else 71 { 72 if (ida==idb) 73 { 74 printf("same account\n"); 75 } 76 else 77 { 78 if (ac[ida]+EPS<x) 79 { 80 printf("insufficient funds\n"); 81 } 82 else 83 { 84 ac[ida]=ac[ida]-x; 85 ac[idb]=ac[idb]+x; 86 char ia = *ida.rbegin(); 87 char ib = *idb.rbegin(); 88 if (ia==ib) 89 { 90 printf("ok\n"); 91 } 92 else 93 { 94 printf("interbank\n"); 95 } 96 } 97 } 98 } 99 } 100 101 int main() 102 { 103 #ifdef HotWhite 104 freopen("in.txt", "r", stdin); 105 #endif 106 int n; 107 double mn; 108 while(scanf("%d",&n)) 109 { 110 if (!n) break; 111 ac.clear(); 112 for (int i = 0; i < n; i++) 113 { 114 scanf("%s %lf",str,&mn); 115 ac[str]=mn; 116 } 117 string s1,s2; 118 while(scanf("%s",cmd)) 119 { 120 if (strcmp(cmd,"end")==0) break; 121 if (strcmp(cmd,"create")==0) 122 { 123 scanf("%s",name); 124 s1=name; 125 create(s1); 126 } 127 if (strcmp(cmd,"deposit")==0) 128 { 129 scanf("%s%lf",name,&mn); 130 s1=name; 131 deposit(s1,mn); 132 } 133 if (strcmp(cmd,"withdraw")==0) 134 { 135 scanf("%s%lf",name,&mn); 136 s1=name; 137 withdraw(s1,mn); 138 } 139 if (strcmp(cmd,"transfer")==0) 140 { 141 scanf("%s%s%lf",name,name1,&mn); 142 s1=name; 143 s2=name1; 144 transfer(s1,s2,mn); 145 } 146 } 147 printf("end\n\n"); 148 } 149 printf("goodbye\n"); 150 return 0; 151 }
C - International Collegiate Programming Contest
【题意】每一组case给你一些买家的姓名和最高出价,还有卖家的姓名和最低售价。然后计算出所有成交信息。
【思路】水题,直接暴力模拟,比较价钱即可。代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int MAX = 1010; 6 7 struct BID 8 { 9 char name[25], type[5]; 10 double price; 11 }; 12 BID bid[MAX], buy[MAX], sell[MAX]; 13 14 int main() { 15 16 #ifdef HotWhite 17 freopen("in.txt", "r", stdin); 18 #endif 19 20 int N, flag; 21 char issuer[12]; 22 23 while(scanf("%d%s", &N, issuer) != EOF && N) 24 { 25 int cntb = 0, cnts = 0; 26 for(int i=0; i<N; ++i) 27 { 28 scanf("%s%s%lf", bid[i].name, bid[i].type, &bid[i].price); 29 if(strcmp(bid[i].type, "buy") == 0) 30 { 31 buy[cntb++] = bid[i]; 32 } 33 if(strcmp(bid[i].type, "sell") == 0) 34 { 35 sell[cnts++] = bid[i]; 36 } 37 } 38 printf("%s\n", issuer); 39 for(int i=0; i<N; ++i) 40 { 41 flag = 0; 42 printf("%s:", bid[i].name); 43 if(strcmp(bid[i].type, "buy") == 0) 44 { 45 for(int j=0; j<cnts; ++j) 46 { 47 if(sell[j].price <= bid[i].price) 48 { 49 printf(" %s", sell[j].name); 50 flag = 1; 51 } 52 } 53 } 54 if(strcmp(bid[i].type, "sell") == 0) 55 { 56 for(int j=0; j<cntb; ++j) 57 { 58 if(buy[j].price >= bid[i].price) 59 { 60 printf(" %s", buy[j].name); 61 flag = 1; 62 } 63 } 64 } 65 if(!flag) 66 printf(" NO-ONE"); 67 printf("\n"); 68 } 69 } 70 return 0; 71 }
【题意】经济危机,政府给予bankA和bankB补助,补助是按照一包一包的算。总共有N包,为了公平起见,怎么分配才能使得最后bankA和bankB得到的补助之差最小。
【思路】贪心策略,从小到大排序,先把最小的给A。然后从最大的开始先给B,再给A,依次分配下去,直到分完。代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 int a[50005]; 8 int main() { 9 #ifdef HotWhite 10 // freopen("in.txt", "r", stdin); 11 #endif 12 int n; 13 while(scanf("%d",&n)) 14 { 15 if (!n) break; 16 for (int i = 0; i < n; i++) 17 { 18 scanf("%d",&a[i]); 19 } 20 sort(a,a+n); 21 printf("%d-A",a[0]); 22 int k=0; 23 for (int i = n-1; i >=1; i--,k++) 24 { 25 printf(" %d-%c",a[i],k&1?‘A‘:‘B‘); 26 } 27 printf("\n"); 28 } 29 return 0; 30 }
CTU Open 2008(未完工),布布扣,bubuko.com
标签:style blog class code c tar
原文地址:http://www.cnblogs.com/rayn1027/p/3731958.html