UVA

来源:互联网 发布:游戏运营数据分析报告 编辑:程序博客网 时间:2024/05/26 09:56
/*代码似有点细节问题,但解析挺细致的bloghttp://www.cnblogs.com/xcw0754/p/4754015.htmlhttp://blog.163.com/xifan_jiang/blog/static/251140042201671887586/我模仿借鉴了的2个blog,其中有些可写可不写的,或者可以合并的代码,在我自己重写的时候,我都做了一定的简化http://blog.csdn.net/a197p/article/details/45577499http://blog.csdn.net/u011345136/article/details/39058429这题主要的问题出在,并没有理解清楚题意,后来看了几种题解的思路解析,觉得其实这道题我还是有些似懂非懂...按照blog作者的说法,已经取得k次以后,取得下一个新的号码的概率为 p = (n-k)/n,而对应的平均次数就是 1/p = n/(n-k),那么总共的次数就是 1/p的那个式子求和,其中k从0到(n-1)想说有一种很诡异的感觉,分明觉得他的思路挺有道理,可是又隐隐觉得哪里好像不对劲,现在想不太明白,到底是哪里怪怪的,那就先搁置吧以及,我自己改写的代码,删去了我觉得不必要的地方,例如,就本题而言,即便是中间过程,分数也不可能出现负数,所以,如果符号在分母的处理,就被我删掉了...此外,如果构造中已经用了reduction()简化,而重载加减乘除时,又是返回一个新构造的值,那么重载函数中,就没有调用reduction()的必要了...等等简化*//*  另外一说的是,有些事情果真只是看起来简单,真要做的时候,许多问题就会涌现出来  比如我自己敲的时候,一开始一直答案有错,后来发现,重载加号时,我忘了加 fraction(),导致,后面的,就被当成了括号表达式,返回一个long long型的数,总之就是,连构造函数都忘了调用,怪不得出错T^T    一定要细心再细心啊!!!*/


#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int N = 40;struct fraction{ll num, den;fraction(){num = 0; den = 1;}fraction(ll n, ll d){num = n; den = d;this->reduction();}fraction(ll n){num = n; den = 1;}void operator = (ll n){*this = fraction(n);}void operator = (const fraction a){num = a.num;den = a.den;//cout << "used" << endl;}fraction operator +(const fraction a){fraction tp = fraction(num * a.den + a.num * den, den * a.den);//cout << "test:"; tp.print();return tp;}fraction operator -(const fraction a){fraction tp = fraction(num * a.den - a.num * den, den * a.den);return tp;}fraction operator *(const fraction a){return fraction(num * a.num, den * a.den);}fraction operator /(const fraction a){return fraction(num * a.den, den * a.num);}//fraction operator += (const fraction a)//{//return fraction(*this) + fraction(a);//}void reduction(){ll gcd = __gcd(num, den);num /= gcd;den /= gcd;}void print(){ll a = num, b = den, c = num / den;//c为整数部分 if (!a){cout << 0 << endl; return;}a %= b;// 化为真分数后的分子 if (!a){cout << c << endl; return;}ll t = c; int lenc = 0, lenb = 0;while (t){lenc++;t /= 10;}t = b;while (t){lenb++;t /= 10;}if (c){for (int i = 0; i <= lenc; i++) cout << " "; cout << a << endl;cout << c << " "; for (int i = 0; i < lenb; i++) cout << "-"; cout << endl;for (int i = 0; i <= lenc; i++) cout << " "; cout << b << endl;}else{cout << a << endl;for (int i = 0; i < lenb; i++) cout << "-"; cout << endl;cout << b << endl;}}};int main(){cin.sync_with_stdio(false);cin.tie(0);int n;while (cin >> n){fraction fra = 0;for (int i = 1; i <= n; i++){//cout << n << " " << i << endl;//cout << "this is fra: " << endl; fra.print();//cout << "add this: " << endl; fraction(n, i).print();fra = fra + fraction(n, i);}fra.print();}return 0;}


原创粉丝点击