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 |
偶数情况枚举,怎么都还还是WA?哪位帮忙看一看, 谢谢了~~~~~~ #include<iostream> #include<stdlib.h> using namespace std; void getmindist(int A[],int n,int& val,int& counts){ //获得数组A相差最小的值 int i; val = 100; counts = 0; for( i=(A[0]==0?1:0);i<n;i++){ if(A[i+1]-A[i]<val){ val = A[i+1]-A[i]; counts=1;} else if(A[i+1]-A[i]==val)counts++; } } int main() { int dist,count,len,t,i,j,k,l,A[15],min,temp; char ch,R1[15],R2[15]; cin>>t; cin.get(); while(t--) { j=0; while((ch=cin.get())!='\n'){ if(ch>='0'&&ch<='9')A[j++]=ch-'0'; } if(j%2==1) { //奇数情况,直接处理 if(A[0]==0) { R1[0]=A[1]+'0'; R1[1]='0'; for(i=2;i<=j/2;i++){ R1[i]=A[i]+'0'; } R1[i]='\0'; for(i=j-1;i>j/2;i--){ R2[j-1-i]=A[i]+'0'; } R2[j-1-i]='\0'; cout<<(atoi(R1)-atoi(R2))<<endl; } else { for(i=0;i<=j/2;i++)R1[i]=A[i]+'0'; R1[i]='\0'; for(i=j-1;i>j/2;i--)R2[j-1-i]=A[i]+'0'; R2[j-1-i]='\0'; cout<<(atoi(R1) - atoi(R2))<<endl; } } else { //偶数情况,贪心枚举相邻之间差为dist if(j==2)cout<<A[1]-A[0]<<endl; else { getmindist(A,j-1,dist,count); min = 100000000; for(i=(A[0]==0?1:0);i<j;i++){ if(A[i+1]-A[i]==dist){ R1[0]=A[i]+'0'; R2[0]=A[i+1]+'0'; k=1; l=j-1; while(k<j/2) { if(l!=i+1 && l!=i) R1[k++]=A[l]+'0'; l--; } R1[k]='\0'; k=1; l=0; //cout<<R1<<endl; while(k<j/2) { if(l!=i && l!=i+1) R2[k++]=A[l]+'0'; l++; } R2[k]='\0'; //cout<<R2<<endl; temp = atoi(R2) - atoi(R1); if(min > temp) min = temp; } } cout<<min<<endl; } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator