T1000
1 #include2 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 #include2 #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 #include2 #include 3 #include 4 #include 5 #define MAXLENGTH 1002 6 #define MAXCARRY 10000 7 #ifdef AUTHOR 8 Code By Hatsuakira Tenan 9 #endif10 struct largeint11 {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 #include2 #include 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 else29 {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 #include2 #include 3 #include