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:终于ac了,double和long long区别啊,后面的人别走弯路了,两种方法(代码)In Reply To:终于ac了,double和long long区别啊,后面的人别走弯路了(代码) Posted by:1008061011 at 2011-07-30 15:25:26 >解法一 //题目注意问题:输出用double会出现精度问题,用long long输出 > #include "stdio.h" > #include "string.h" > #include "math.h" > main() > { > int n,i,x[1000100],y[1000100],count=0; > long long s,j; > char a[1000100]; > scanf("%d",&n); > while(n--) > { > count=0;s=0; > x[0]=0;y[0]=0; > scanf("%s",&a); > for(i=0;a[i]!='5';i++) //这个循环用来找每个点的坐标,并放在x[],y[]里面 > { > count++; > if(a[i]-'1'==0) > { > x[i+1]=x[i]+1; > y[i+1]=y[i]-1; > continue; > } > if(a[i]-'2'==0) > { > x[i+1]=x[i]+1; //每次比较字符以后,continue就进入下一个循环,节约时间 > y[i+1]=y[i];continue; > } > if(a[i]-'3'==0) > { > x[i+1]=x[i]+1; > y[i+1]=y[i]+1;continue; > } > if(a[i]-'4'==0) > { > x[i+1]=x[i]; > y[i+1]=y[i]-1;continue; > } > if(a[i]-'6'==0) > { > x[i+1]=x[i]; > y[i+1]=y[i]+1;continue; > } > if(a[i]-'7'==0) > { > x[i+1]=x[i]-1; > y[i+1]=y[i]-1;continue; > } > if(a[i]-'8'==0) > { > x[i+1]=x[i]-1; > y[i+1]=y[i];continue; > } > if(a[i]-'9'==0) > { > x[i+1]=x[i]-1; > y[i+1]=y[i]+1;continue; > } > } > if(count==1) > printf("0\n"); //如果输入只有一个字符5,那么结果为0,但用下面的公式只能算输入两个以上字符的所以在这里用一个条件判断 > > else > { > for(i=1;i<=count;i++) > { > s+=x[i]*y[i+1]-x[i+1]*y[i]; //用叉积公式:分母的和=三角形总的面积的两倍,因此最后要结果要除以2 > } > j=(long long)fabs(s); //最后分母的和求绝对值 > > if(s%2==0) > printf("%lld\n",j/2); //如果为偶素直接除以2 > else > printf("%lld.5\n",j/2); //奇数除以2后,后面的0.5会丢失,在%lld后加.5即可 > > } > } > > } > > 改进方法; > //题目注意问题:输出用double会出现精度问题,用long long输出 > #include "stdio.h" > #include "string.h" > #include "math.h" > main() > { > int n,i,x1,x2,y1,y2,len,p,dic[10][2]={{0,0},{1,-1},{1,0},{1,1},{0,-1},{0,0},{0,1},{-1,-1},{-1,0},{-1,1}}; //对各个字符对应的坐标值用数组先保存下来 > long long s,j; > char a[1000100]; > scanf("%d",&n); > while(n--) > { > s=0; > > scanf("%s",&a); > len=strlen(a); > x1=0;y1=0; > for (i=0;i<len-1;i++) //找出每个字符坐标对应的点,并根据叉积法,求每个三角形对应的面积 > { > p=a[i]-'0'; > x2=x1+dic[p][0]; > y2=y1+dic[p][1]; > s+=x1*y2-y1*x2; //每个小三角形的分母求和,最后除2就为总面积 > x1=x2; > y1=y2; > } > > > j=(long long)fabs(s); > if(j==0) > printf("0\n"); > else > { > if(j%2==0) > printf("%lld\n",j/2); > else > printf("%lld.5\n",j/2); //奇数除以2后,后面的0.5会丢失,在%lld后加.5即可 > } > > } > > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator