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:数据错了,真的错了In Reply To:Re:数据错了 Posted by:wpolly at 2007-03-26 11:11:01 > 只能说明数据弱了。。 但是9确实在输入数据里面 下面是测出输入数据有9的代码 #include <iostream> #include <list> using namespace std; int eva() { int num,i; cin>>num;//num中保存的是天平两边的重量差 if(num==9) //如果有9的话就TLE,实践也证实了这一点 while(1); //注释掉这两行的话就AC,但这代码得出的是错误答案 int side=1;//left=-1,right =1; //side用来标记天平那一侧更轻 int ruler[30];//保存每个砝码的重量 int sum[30];//保存砝码重量的求和 sum[1]=1; ruler[1]=1; for (i=2;i<=20;i++) ruler[i]=ruler[i-1]*3; for (i=2;i<=20;i++) sum[i]=sum[i-1]+ruler[i]; //初始化砝码重量 list<int> left,right; //保存结果 for (i=20;i>0;i--)//从重到轻枚举每一个砝码 { if (num>=ruler[i]) //如果重量差大于当前砝码,则将它加到较轻的一边 { //big poise if (side==1) right.push_front(i); else left.push_front(i); num-=ruler[i]; } else if (num>sum[i-1]) //如果重量差大于所有轻于当前砝码的砝码的质量和,则将当前砝码加到较轻的一边 { //big poise-r if (side==1) right.push_front(i); else left.push_front(i); num=ruler[i]-num; side*=-1;//加砝码后,轻重两边将会对调 } } list<int>::iterator pList; char buf[1000]; //显示结果 if (left.empty()) { cout<<"empty"; } else { pList=left.begin(); sprintf(buf,"%d",ruler[*pList]); cout<<buf; pList++; for (;pList!=left.end();pList++) { sprintf(buf,",%d",ruler[*pList]); cout<<buf; } } cout<<' '; if (right.empty()) { cout<<"empty"; } else { pList=right.begin(); sprintf(buf,"%d",ruler[*pList]); cout<<buf; pList++; for (;pList!=right.end();pList++) { sprintf(buf,",%d",ruler[*pList]); cout<<buf; } } cout<<endl; return 0; } int main() { int t; cin>>t; while(t--) eva(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator