SRM 389(1-250pt)

题意:按一定方法生成n个分数,求他们的和。n <= 20

解法:暴力。我只是没想到,10000^20用double算也能被接受0 0

tag:brute-force

  1 // BEGIN CUT HERE
  2 /*
  3  * Author:  plum rain
  4  * score :
  5  */
  6 /*
  7 
  8  */
  9 // END CUT HERE
 10 #line 11 "ApproximateDivision.cpp"
 11 #include <sstream>
 12 #include <stdexcept>
 13 #include <functional>
 14 #include <iomanip>
 15 #include <numeric>
 16 #include <fstream>
 17 #include <cctype>
 18 #include <iostream>
 19 #include <cstdio>
 20 #include <vector>
 21 #include <cstring>
 22 #include <cmath>
 23 #include <algorithm>
 24 #include <cstdlib>
 25 #include <set>
 26 #include <queue>
 27 #include <bitset>
 28 #include <list>
 29 #include <string>
 30 #include <utility>
 31 #include <map>
 32 #include <ctime>
 33 #include <stack>
 34 
 35 using namespace std;
 36 
 37 #define clr0(x) memset(x, 0, sizeof(x))
 38 #define clr1(x) memset(x, -1, sizeof(x))
 39 #define pb push_back
 40 #define sz(v) ((int)(v).size())
 41 #define all(t) t.begin(),t.end()
 42 #define zero(x) (((x)>0?(x):-(x))<eps)
 43 #define out(x) cout<<#x<<":"<<(x)<<endl
 44 #define tst(a) cout<<a<<" "
 45 #define tst1(a) cout<<#a<<endl
 46 #define CINBEQUICKER std::ios::sync_with_stdio(false)
 47 
 48 typedef vector<int> vi;
 49 typedef vector<string> vs;
 50 typedef vector<double> vd;
 51 typedef pair<int, int> pii;
 52 typedef long long int64;
 53 
 54 const double eps = 1e-8;
 55 const double PI = atan(1.0)*4;
 56 const int inf = 2139062143 / 2;
 57 
 58 class ApproximateDivision
 59 {
 60     public:
 61         double quotient(int a, int b, int terms){
 62             int t = 1;
 63             while (t <= b){
 64                 if (t == b) return (double)a / b;
 65                 t *= 2;
 66             }
 67             int c = t - b, cnt = 0;
 68             double t1 = 1.0, t2 = t; 
 69             double sum = 0;
 70             while (cnt < terms){
 71                 sum += a * t1 / t2;
 72                 t1 *= c; t2 *= t;
 73                 ++ cnt;
 74             }
 75             return sum;
 76         }
 77         
 78 // BEGIN CUT HERE
 79     public:
 80     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }
 81     private:
 82     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
 83     void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
 84     void test_case_0() { int Arg0 = 2; int Arg1 = 5; int Arg2 = 2; double Arg3 = 0.34375; verify_case(0, Arg3, quotient(Arg0, Arg1, Arg2)); }
 85     void test_case_1() { int Arg0 = 7; int Arg1 = 8; int Arg2 = 5; double Arg3 = 0.875; verify_case(1, Arg3, quotient(Arg0, Arg1, Arg2)); }
 86     void test_case_2() { int Arg0 = 1; int Arg1 = 3; int Arg2 = 10; double Arg3 = 0.33333301544189453; verify_case(2, Arg3, quotient(Arg0, Arg1, Arg2)); }
 87     void test_case_3() { int Arg0 = 1; int Arg1 = 10000; int Arg2 = 2; double Arg3 = 8.481740951538086E-5; verify_case(3, Arg3, quotient(Arg0, Arg1, Arg2)); }
 88     void test_case_4() { int Arg0 = 1; int Arg1 = 7; int Arg2 = 20; double Arg3 = 0.14285714285714285; verify_case(4, Arg3, quotient(Arg0, Arg1, Arg2)); }
 89     void test_case_5() { int Arg0 = 0; int Arg1 = 4; int Arg2 = 3; double Arg3 = 0.0; verify_case(5, Arg3, quotient(Arg0, Arg1, Arg2)); }
 90     void test_case_6() { int Arg0 = 50; int Arg1 = 50; int Arg2 = 1; double Arg3 = 0.78125; verify_case(6, Arg3, quotient(Arg0, Arg1, Arg2)); }
 91 
 92 // END CUT HERE
 93 
 94 };
 95 
 96 // BEGIN CUT HERE
 97 int main()
 98 {
 99 //    freopen( "a.out" , "w" , stdout );    
100     ApproximateDivision ___test;
101     ___test.run_test(-1);
102        return 0;
103 }
104 // END CUT HERE
View Code

 

 

posted @ 2013-12-29 16:04  Plumrain  阅读(196)  评论(0编辑  收藏  举报