HDOJ 1850 Being a Good Boy in Spring Festival

  各种问题啊:字符数组以\0结尾,知道是一回事,使用的时候能避免错误又是一回事。数组越界,永远的痛!!!不查永远不知道,原来整数转位串是有现成的函数的——__itoa。__itoa_s你永远伤不起的__itoa的安全版本,OJ版本不兼容啊!!!如果能及时意识到“^”这个东东叫抑或,又何必有第一个版本呢?基础知识丢到了爪哇国……

最后想说:做个好孩子真不容易啊~

版本一:

View Code
 1 //#include <fstream>   
2 #include <iostream>
3 #include<math.h>
4 using namespace std;
5
6 int main()
7 {
8 //ifstream cin("Being a Good Boy in Spring Festival.txt");
9 int num;
10 while(cin>>num,num)
11 {
12 char result[22];
13 int value[102];
14 cin>>value[0];
15 _itoa(value[0],result,2);
16
17 int record;
18 for(int i=1;i<num;i++)
19 {
20 char assist[22];
21 cin>>value[i];
22 _itoa(value[i],assist,2);
23 int assistLen=strlen(assist)-1;
24 int resultLen=strlen(result)-1;
25 int max=resultLen;
26 if(assistLen>resultLen)
27 max=assistLen;
28 record=max;
29 result[max+1]='\0';//数组越界,你永远的痛!!!
30 while(max>=0)
31 {
32 if(resultLen<0)
33 result[max]=assist[max];
34 if(resultLen>=0&&assistLen>=0)
35 if(result[resultLen]==assist[assistLen])
36 result[max]='0';
37 else
38 result[max]='1';
39 max--;
40 assistLen--;
41 resultLen--;
42 }
43 }
44 int intResult=0;
45 int i=0;
46 while (record>=0)
47 {
48 if(result[record]=='1')
49 intResult+=pow(2.0,i);
50 record--;
51 i++;
52 }
53 record=0;
54 for(i=0;i<num;i++)
55 {
56 intResult^=value[i];
57 if(intResult<value[i])
58 record++;
59 intResult^=value[i];
60 }
61 cout<<record<<endl;
62 }
63 return 0;
64 }

版本二:

View Code
 1 //#include <fstream>   
2 #include <iostream>
3 using namespace std;
4 int value[101];
5 int main ()
6 {
7 //ifstream cin("Being a Good Boy in Spring Festival.txt");
8 int n,sum,count,i;
9 while (cin>>n)
10 {
11 if (n==0) return 0;
12 sum=0;
13 for (i=0;i<n;i++)
14 {
15 cin>>value[i];
16 sum^=value[i];
17 }
18 count=0;
19 for (i=0;i<n;i++)
20 {
21 sum^=value[i];
22 if (value[i]>sum) count++;
23 sum^=value[i];
24 }
25 cout<<count<<endl;
26 }
27 return 0;
28 }



posted on 2011-07-31 22:30  AdaByron  阅读(244)  评论(0编辑  收藏  举报

导航