2015 Fall HIT Weekly Training 2
在错过了第一次之后,终于准时参加了第二次。
18:30-20:30的新手练习题,我居然真的就做了两个小时,差点没AC
A.YY, and YY again
读取string时会停在空格,所以用getline就行。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 6 int main(void) 7 { 8 string st; 9 int x; 10 while(getline(cin, st)){ 11 x = 0; 12 for(int i = 0; i < st.length(); ++i) 13 if(st[i] >= 'A' && st[i] <= 'Z') 14 x += st[i] - 'A' + 1; 15 if(x > 100) cout<<"INVALID"<<endl; 16 else cout<<x<<endl; 17 18 } 19 return 0; 20 }
B.Box of Bricks
这年头,思路比这再简单的数学题真是不多见了。
计算∑(a[i]<p)(p-a[i])就行了。p是最后的高度。
1 #include<cstdio> 2 3 int a[100]; 4 5 int main(void) 6 { 7 int N, k = 1; 8 int p; 9 int ans; 10 scanf("%d", &N); 11 while(N > 0){ 12 if(k > 1) printf("\n"); 13 p = 0; 14 ans = 0; 15 for(int i = 0; i < N; ++i){ 16 scanf("%d", &a[i]); 17 p += a[i]; 18 } 19 p /= N; 20 for(int i = 0; i < N; ++i) 21 if(a[i] < p) 22 ans += p - a[i]; 23 printf("Set #%d\n", k++); 24 printf("The minimum number of moves is %d.\n", ans); 25 scanf("%d", &N); 26 } 27 return 0; 28 }
C.Not A Water Problem
首先是那个诡异Hint:That isn't a real water problem,but I promise that there's only one trick.
一看就知道这道题暗藏杀机,开始时候使用while(scanf()),妥妥的TLE。
奥,原来就trick就是EOF啊,果断改,试了!EOF、getchar() != EOF 、(ch = getchar()) != EOF。果断都不管用,花式WA。。。
然后恍然大悟,signed int,果然是个暗示啊,abs(-2^31)不能用int,然后换LL,还是各种WA。。。
在最后的两分钟,开始各种尝试,终于当我换回了while(cin)的时候,它红了。
。。。。。。
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 #define abs(a) ((a)>0)?(a):(-(a)) 6 7 int main(void) 8 { 9 long long x; 10 while(cin>>x){ 11 cout<<(abs(x))<<endl; 12 } 13 return 0; 14 }
D.整数的排序
果断的sort
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstdlib> 5 6 int a[20]; 7 8 int main(void) 9 { 10 int N; 11 scanf("%d", &N); 12 for(int i = 0; i < N; ++i){ 13 for(int j = 0; j < 10; ++j) scanf("%d", &a[j]); 14 std::sort(a, a+10); 15 for(int j = 0; j < 9; ++j) printf("%d ", a[j]); 16 printf("%d\n", a[9]); 17 } 18 return 0; 19 }
E.Fibonacci Numbers
看见这题瞬间就想起了那年哪月,翔神教我的矩阵快速幂求斐波那契。。。REBMYSMLY
偷偷的百度了lrj的bign,ctrl+c, ctrl+p。
想着有lrj庇佑,这题肯定一次AC,然而。。。
经典坑爹,教科书式的坑爹,测试数据有多组,但他就是不告诉你。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 #define MAXN 3010 7 8 struct bign{ 9 int len, s[MAXN]; 10 11 bign() { 12 memset(s, 0, sizeof(s)); 13 len = 1; 14 } 15 16 bign(int num) { 17 *this = num; 18 } 19 20 bign(const char* num) { 21 *this = num; 22 } 23 24 bign operator = (int num) { 25 char s[MAXN]; 26 sprintf(s, "%d", num); 27 *this = s; 28 return *this; 29 } 30 31 bign operator = (const char* num) { 32 len = strlen(num); 33 for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; 34 return *this; 35 } 36 37 string str() const { 38 string res = ""; 39 for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res; 40 if(res == "") res = "0"; 41 return res; 42 } 43 44 bign operator + (const bign& b) const{ 45 bign c; 46 c.len = 0; 47 for(int i = 0, g = 0; g || i < max(len, b.len); i++) { 48 int x = g; 49 if(i < len) x += s[i]; 50 if(i < b.len) x += b.s[i]; 51 c.s[c.len++] = x % 10; 52 g = x / 10; 53 } 54 return c; 55 } 56 57 void clean() { 58 while(len > 1 && !s[len-1]) len--; 59 } 60 61 bign operator * (const bign& b) { 62 bign c; c.len = len + b.len; 63 for(int i = 0; i < len; i++) 64 for(int j = 0; j < b.len; j++) 65 c.s[i+j] += s[i] * b.s[j]; 66 for(int i = 0; i < c.len-1; i++){ 67 c.s[i+1] += c.s[i] / 10; 68 c.s[i] %= 10; 69 } 70 c.clean(); 71 return c; 72 } 73 74 bign operator - (const bign& b) { 75 bign c; c.len = 0; 76 for(int i = 0, g = 0; i < len; i++) { 77 int x = s[i] - g; 78 if(i < b.len) x -= b.s[i]; 79 if(x >= 0) g = 0; 80 else { 81 g = 1; 82 x += 10; 83 } 84 c.s[c.len++] = x; 85 } 86 c.clean(); 87 return c; 88 } 89 90 bool operator < (const bign& b) const{ 91 if(len != b.len) return len < b.len; 92 for(int i = len-1; i >= 0; i--) 93 if(s[i] != b.s[i]) return s[i] < b.s[i]; 94 return false; 95 } 96 97 bool operator > (const bign& b) const{ 98 return b < *this; 99 } 100 101 bool operator <= (const bign& b) { 102 return !(b > *this); 103 } 104 105 bool operator == (const bign& b) { 106 return !(b < *this) && !(*this < b); 107 } 108 109 bign operator += (const bign& b) { 110 *this = *this + b; 111 return *this; 112 } 113 }; 114 115 istream& operator >> (istream &in, bign& x) { 116 string s; 117 in >> s; 118 x = s.c_str(); 119 return in; 120 } 121 122 ostream& operator << (ostream &out, const bign& x) { 123 out << x.str(); 124 return out; 125 } 126 127 128 int main(void) 129 { 130 int N; 131 bign a1, a2; 132 while(cin>>N){ 133 a1 = 1; 134 a2 = 1; 135 for(int i = 3; i <= N; i += 2){ 136 a1 = a1 + a2; 137 a2 = a1 + a2; 138 } 139 if(N & 1) cout<<a1<<endl; 140 else cout<<a2<<endl; 141 } 142 return 0; 143 }