标签:style blog color os io ar for div sp
这个最简递增分数题关键在于最简,如果知道这个方法,此题易解。呵呵,一次性通过。
/* ID: qq104801 LANG: C++ TASK: frac1 */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> using namespace std; int n; struct frac { int up; int down; bool operator < (const frac &b)const{ return up*b.down < down*b.up; } }; bool cmp(const frac &a,const frac &b) { return a.up*b.down < b.down*b.up; } int isstupid(int up,int down) { int t; if (up==1)return 1; if(down%up==0)return 0; while(down%up!=0) { t=up;up=down%up;down=t; } if(up>1)return 0; else return 1; } void test() { freopen("frac1.in","r",stdin); freopen("frac1.out","w",stdout); cin >> n; //cout<<n<<endl; cout<<"0/1"<<endl; vector<frac> v; int i,j,k; frac f; for(i=2;i<=n;i++) for(j=1;j<=i;j++) { if(isstupid(j,i)) { f.up=j;f.down=i; v.push_back(f); //cout<<f.up<<"/"<<f.down<<endl; } } sort(v.begin(),v.end()); //cout<<endl; vector<frac>::iterator it; for(it=v.begin();it!=v.end();it++) cout<<it->up<<"/"<<it->down<<endl; cout<<"1/1"<<endl; } int main () { test(); return 0; }
测试结果:
USER: cn tom [qq104801] TASK: frac1 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.008 secs, 3376 KB] Test 2: TEST OK [0.011 secs, 3508 KB] Test 3: TEST OK [0.008 secs, 3508 KB] Test 4: TEST OK [0.008 secs, 3508 KB] Test 5: TEST OK [0.008 secs, 3508 KB] Test 6: TEST OK [0.011 secs, 3508 KB] Test 7: TEST OK [0.014 secs, 3508 KB] Test 8: TEST OK [0.038 secs, 3508 KB] Test 9: TEST OK [0.070 secs, 3508 KB] Test 10: TEST OK [0.097 secs, 3508 KB] Test 11: TEST OK [0.329 secs, 3508 KB] All tests OK. YOUR PROGRAM (‘frac1‘) WORKED FIRST TIME! That‘s fantastic -- and a rare thing. Please accept these special automated congratulations. Here are the test data inputs: ------- test 1 ---- 1 ------- test 2 ---- 2 ------- test 3 ---- 4 ------- test 4 ---- 7 ------- test 5 ---- 10 ------- test 6 ---- 15 ------- test 7 ---- 24 ------- test 8 ---- 50 ------- test 9 ---- 75 ------- test 10 ---- 100 ------- test 11 ---- 160 Keep up the good work! Thanks for your submission!
标签:style blog color os io ar for div sp
原文地址:http://www.cnblogs.com/dpblue/p/3952331.html