Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:错在哪里呢?总是wrong answer.哪位好心指点?In Reply To:错在哪里呢?总是wrong answer.哪位好心指点? Posted by:BABYBLUE at 2005-02-28 21:29:08 abs可以直接取整数的绝对值,不用fabs. 其他看划线部分 > #include<iostream.h> > #include<math.h> > void wang(char a,char b,char c,char d){ > int g; > int i=a-c; > int j=b-d; > > if((int)fabs(i)>(int)fabs(j)) > g=(int)fabs(i); > > else > g=(int)fabs(j); > > cout<<g<<" "; > } > void hou(char a,char b,char c,char d){ > int i=a-c; > int j=b-d; > int k=(int)fabs(i)-(int)fabs(j); > if(k==0||i==0||j==0) > cout<<"1 "; > else > cout<<"2 "; > } > void che(char a,char b,char c,char d){ > int i=a-c; > int j=b-d; > if(i==0||j==0) > cout<<"1 "; > else > cout<<"2 "; > } > void xiang(char a,char b,char c,char d){ > int i=a-c; > int j=b-d; > if((i%2)!=(j%2)) ~~~~~~~~~~~~如i为负数的话,余数也是负的。所以i,j要先取绝对值 > cout<<"Inf\n"; > else if((int)fabs(i)==(int)fabs(j)) > cout<<"1\n"; > else > cout<<"2\n"; > } > void play(){ > char start[2]; > char end[2]; > char a,b,c,d; > cin>>start; > cin>>end; > a=start[0]; > b=start[1]; > c=end[0]; > d=end[1]; > if(a>'g'||a<'a'||c>'g'||c<'a'||b>8||b<1||d>8||d<1) > return; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这里b='1'而不是b=1,因为cin的是char型的,要用b-='0'转化一下,另外这题只关心b、d的差,不用转化的(ps.其实不用判断输入是否合理,测试数据都是合题意的); > if(a==c&&b==d){ > cout<<"0 0 0 0\n"; > return; > } > wang(a,b,c,d); > hou(a,b,c,d); > che(a,b,c,d); > xiang(a,b,c,d); > } > void main(){ > int num,p=1; > > cin>>num; > if(num<0||num>20) > return; ~~~~~~~~~~~~~~~~~~~~~~~~~此处也可删去 > while(p<=num) > { play(); > p++;} > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator