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 |
时间超出 ,太多循环for 请高手帮我改一下#include <iostream> #include<time.h> using namespace std; int f(int a[], int b[]) { int n=1; double s=a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3]+a[4]*b[4]+a[5]*b[5]; s=s/2; for (int i0=0;i0<=b[0];i0++) for (int i1=0;i1<=b[1];i1++) for (int i2=0;i2<=b[2];i2++) for (int i3=0;i3<=b[3];i3++) for (int i4=0;i4<=b[4];i4++) for (int i5=0;i5<=b[5];i5++) { double t=i0*a[0]+i1*a[1]+i2*a[2]+i3*a[3]+i4*a[4]+i5*a[5]; if (t==s) {n=2; break;} } return n; } void main () { int start ,end; start=clock(); int a[6]={1,2,3,4,5,6}, b[6]={1}, c[50]={0}; int j=0; while (b[0]!=0 || b[1]!=0 || b[2]!=0 || b[3]!=0 || b[4]!=0 || b[5]!=0 ) { c[j]=f(a,b); for (int i=0; i<6;i++) cin>>b[i]; j++; } for (j=1;c[j]!=0;j++) { if (c[j]==1) cout<<"Collection #"<<j<<":"<<endl<<"Can't be divided."<<endl; else cout<<"Collection #"<<j<<":"<<endl<<"Can be divided."<<endl; cout<<endl; } end=clock(); cout<<(start-end)/18.2; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator