HDU ACM【1001~1004】

  其实我有很多同学从大一的时候就开始做ACM题目了,我自己也曾做过几次,但总是会由于某些原因没有坚持下去!【或者是因为感觉题目太难不适合自己;或者是因为感觉题目要求的过于苛刻,不想再上面花费太多的精力和时间;或者是眼前总有一些更重要的事情要去做;又或者是自己的懒惰懒惰懒惰... ...】,这次我想向很多同学学习一下,把自己做的题目贴出来,也算是对自己的一种鞭策吧... ...

View Code
 1 //1001
2 #include <iostream>
3 using namespace std;
4 int main(){
5 int num, sum;
6 while(cin >> num){
7 if(0 == num % 2)
8 sum = num / 2 * (1 + num); //必须这么写,不能写成num*(1+num)/2,因为题目只是说结果在32bit之内,但(1+n)*n很可能超出32bit
9 else
10 sum = (1 + num) / 2 * num;
11 printf("%d\n\n", sum);
12 }
13 return 0;
14 }
15
16 //提高C编程的效率方式其中一个:能用数学公式的要用数学公式!
17 //而不要用for循环进行累加的方法

 

View Code
 1 //1002
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 int main(){
7 int T;
8 char arr1[1000], arr2[1000], out[1000];
9 cin >> T;
10 int i = 0, j, k, l, c;
11 while(i < T){
12 cin >> arr1 >> arr2;
13 j = strlen(arr1);
14 k = strlen(arr2);
15 c = 0;
16 l = 0;
17 while( (j > 0) && (k > 0) ){
18 out[l++] = ( (arr1[--j] - 48 + arr2[--k] - 48 + c) % 10 + 48);
19 c = (arr1[j] - 48 + arr2[k] - 48 + c) / 10;
20 }
21 if( (j <= 0) && (k > 0) ){
22 while(--k >= 0){
23 out[l++] = (arr2[k] - 48 + c) % 10 + 48;
24 c = (arr2[k] - 48 + c) / 10;
25 }
26 }else if( (k <= 0) && (j > 0) ){
27 while(--j >= 0){
28 out[l++] = ( arr1[j] - 48 + c) % 10 + 48;
29 c = (arr1[j] - 48 + c) / 10;
30 }
31 }
32 if(c == 0)
33 l--;
34 else
35 out[l] = '1';
36 cout << "Case " << i + 1 << ":" << endl;
37 cout << arr1 << " + " << arr2 << " = " ;
38 for(c = l; c >= 0; --c)
39 cout << out[c];
40 ++ i;
41 if(i < T) //输出的时候一定要注意:什么时候输出换行!输出几个换行!
42 cout << endl << endl;
43 else
44 cout << endl;
45 }
46 return 0;
47 }

 

 1 //1003
2 //题目中虽然用数组的形式描述了题目,但其实所输入的内容是可以不用存储的,可以不用存储的地方就不要存储!一定要注意!
3 #include <iostream>
4 using namespace std;
5
6 int main(){
7 int T, N, j, index, start, end, i = 0;
8 int thisum, sum, num;
9
10 cin >> T;
11 while(i < T){
12 cin >> N;
13 j = 0;
14 start = end = thisum = 0;
15 sum = -999999999; //初值不能设为,因为如果是负数的话就会求不出最优子序列。在这儿纠结了差不多一个晚上!
16 index = 0;
17 while(j < N){
18 cin >> num;
19 thisum += num;
20 if(thisum > sum){
21 sum = thisum;
22 start = index;
23 end = j;
24 }
25 if(thisum < 0){
26 thisum = 0;
27 index = j + 1;
28 }
29 ++j;
30 }
31 cout << "Case " << i + 1 << ":" << endl;
32 cout << sum << " " << start + 1<< " " << end + 1<< endl;
33 ++i;
34 if(i < T)
35 cout << endl;
36 }
37 return 0;
38 }

 

View Code
 1 //1004
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 int main(){
7 int N, i, j, k;
8 string * arr1;
9 string str;
10 int * arr2;
11 while(cin >> N){
12 if(N == 0)
13 break;
14 k = 0;
15 arr1 = new string[N];
16 arr2 = new int[N];
17 memset(arr1, 0, N * sizeof(string));
18 memset(arr2, 0, N * sizeof(int)); //不能写为sizeof(arr2),因为arr2为整型指针,使用sizeof(arr2)编译器会认为其大小恒为4,记住以后用memset就用这种方法
19 i = 0;
20 while(i < N){
21 cin >> str;
22 for(j = 0; j < N; ++j){
23 if(str == arr1[j]){
24 arr2[j]++;
25 break;
26 }
27 }
28 if(j == N){
29 arr1[k] = str;
30 arr2[k]++;
31 ++k;
32 }
33 ++i;
34 }
35 int max, index;
36 for(i = 1, max = arr2[0],index = 0; i < N; ++i){
37 if(max < arr2[i]){
38 max = arr2[i];
39 index = i;
40 }
41 }
42 cout << arr1[index] << endl;
43 delete[] arr1;
44 delete[] arr2;
45 }
46 return 0;
47 }



posted @ 2012-03-12 19:46  XiaoweiLiu  阅读(1643)  评论(2编辑  收藏  举报