标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3333 Accepted Submission(s): 1489
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> using namespace std; typedef long long LL; LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b); } LL lcm(LL a,LL b){ return a/gcd(a,b)*b; } int getLen(LL num){ int ans = 0; while(num){ ans++; num/=10; } return ans; } int main() { int n; while(scanf("%d",&n)!=EOF){ LL a=1,b=1; ///a为分子,b为分母 for(int i=2;i<=n;i++){ a = a*i+b; b = b*i; LL d = gcd(a,b); a/=d; b/=d; } a=a*n; LL d = gcd(a,b); a/=d,b/=d; LL res = a/b; if(a%b==0){ printf("%lld\n",a/b); }else { LL len = getLen(res); LL len1 = getLen(b); LL yushu = a%b; for(int i=0;i<=len;i++){ printf(" "); } printf("%lld\n%lld ",yushu,res); for(int i=0;i<len1;i++) printf("-"); printf("\n"); for(int i=0;i<=len;i++){ printf(" "); } printf("%lld\n",b); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5571437.html