HDOJ(~1004)
T1000
1 #include <stdio.h> 2 int main() 3 { 4 int a, b; 5 while (scanf("%d %d", &a, &b) != EOF) 6 printf("%d\n", a + b); 7 return 0; 8 }
T1001
1 #include <stdio.h> 2 #ifdef AUTHOR 3 Code By Hatsuakira Tenan 4 #endif 5 int f(int n) 6 { 7 int js = 0; 8 for (; n > 0; --n) 9 js += n; 10 return js; 11 } 12 int main() 13 { 14 int n; 15 while (scanf("%d", &n) != EOF) 16 printf("%d\n\n", f(n)); 17 return 0; 18 }
T1002
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <algorithm> 5 #define MAXLENGTH 1002 6 #define MAXCARRY 10000 7 #ifdef AUTHOR 8 Code By Hatsuakira Tenan 9 #endif 10 struct largeint 11 { 12 int num[MAXLENGTH], len; 13 largeint() 14 { 15 len = 1; 16 memset(num, 0, sizeof(num)); 17 } 18 }; 19 void scanf_override(largeint &input) 20 { 21 int tmplen; 22 char tmp[MAXLENGTH]; 23 scanf("%s", tmp); 24 std::reverse(tmp, tmp + (tmplen = strlen(tmp))); 25 input.len = tmplen / 4 + (tmplen % 4 > 0); 26 for (int i = 0, j = 0; i < tmplen; ++i, ++j) 27 { 28 input.num[j] = tmp[i] - '0'; 29 if (tmp[++i] == '\0') 30 break; 31 input.num[j] += 10 * (tmp[i] - '0'); 32 if (tmp[++i] == '\0') 33 break; 34 input.num[j] += 100 * (tmp[i] - '0'); 35 if (tmp[++i] == '\0') 36 break; 37 input.num[j] += 1000 * (tmp[i] - '0'); 38 } 39 } 40 inline largeint operator +(largeint a, largeint b) 41 { 42 int tmp = 0; 43 a.len = std::max(a.len, b.len); 44 for (int i = 0; i < a.len; ++i) 45 { 46 a.num[i] += b.num[i] + tmp; 47 tmp = a.num[i] / MAXCARRY; 48 a.num[i] %= MAXCARRY; 49 } 50 if (tmp > 0) 51 a.num[a.len++] = 1; 52 return a; 53 } 54 void printf_override(largeint output) 55 { 56 printf("%d", output.num[--output.len]); 57 for (int i = output.len - 1; i > -1; --i) 58 printf("%04d", output.num[i]); 59 } 60 void work(int k) 61 { 62 largeint a = largeint(), b = largeint(); 63 scanf_override(a); 64 scanf_override(b); 65 printf("Case %d:\n", k + 1); 66 printf_override(a); 67 printf(" + "); 68 printf_override(b); 69 printf(" = "); 70 a = a + b; 71 printf_override(a); 72 printf("\n"); 73 } 74 int main() 75 { 76 int T; 77 scanf("%d", &T); 78 work(0); 79 for (int i = 1; i < T; ++i) 80 { printf("\n"); work(i); } 81 return 0; 82 }
T1003
最长连续子序列和, 根据数据范围(N<=100, 000 & T<=20)选择使用O(n)的算法, 是较为基础的动态规划.
1 #include <stdio.h> 2 #include <limits.h> 3 #define MAXLENGTH 100005 4 #ifdef AUTHOR 5 Code By Hatsuakira Tenan 6 #endif 7 int number[MAXLENGTH], start, end, max; 8 void dp(int length) 9 { 10 int max_tmp = 0, start_tmp = 0, end_tmp = 0; 11 max_tmp = max = INT_MIN; 12 for (int i = 0; i < length; ++i) 13 { 14 if (max_tmp < 0) 15 { 16 if (number[i] > max_tmp) 17 { 18 max_tmp = number[i]; 19 start_tmp = end_tmp = i; 20 if (max < max_tmp) 21 { 22 max = max_tmp; 23 start = start_tmp; 24 end = end_tmp; 25 } 26 } 27 } 28 else 29 { 30 max_tmp += number[i]; 31 end_tmp = i; 32 if(max_tmp > max) 33 { 34 max = max_tmp; 35 start = start_tmp; 36 end = end_tmp; 37 } 38 } 39 } 40 } 41 void work(int R) 42 { 43 printf("Case %d:\n", R); 44 int n; 45 scanf("%d", &n); 46 for (int i = 0; i < n; ++i) 47 scanf("%d", &number[i]); 48 dp(n); 49 printf("%d %d %d\n", max, start + 1, end + 1); 50 } 51 int main() 52 { 53 int T; 54 scanf("%d", &T); 55 for (int i = 1; i < T; printf("\n"), ++i) 56 work(i); 57 work(T); 58 return 0; 59 }
T1004 用STL 做这个题很耍赖, 但是鉴于数据量不大所以直接暴力遍历也是可以的.
1 #include <stdio.h> 2 #include <string.h> 3 #include <map> 4 #include <string> 5 using std::map; 6 using std::string; 7 map<string, int> set; 8 void work(int n) 9 { 10 set.clear(); 11 int maxn = -1; 12 string tmp_stl_string; 13 char tmp_string[21]; 14 for (int i = 0; i < n; tmp_stl_string = tmp_string, ++set[tmp_stl_string], ++i) 15 scanf("%s", tmp_string); 16 for (map<string, int>::iterator it = set.begin(); it != set.end(); ++it) 17 if (maxn < it -> second) 18 { 19 maxn = it -> second; 20 tmp_stl_string = it -> first; 21 } 22 printf("%s\n", tmp_stl_string.c_str()); 23 } 24 int main() 25 { 26 int n; 27 scanf("%d", &n); 28 while (n > 0) 29 { 30 work(n); 31 scanf("%d", &n); 32 } 33 return 0; 34 }
posted on 2013-10-11 11:11 初昱天殷HatsuakiraTenan 阅读(247) 评论(0) 编辑 收藏 举报