藍橋杯每日一題1.13 2018省賽A組1.分數[等比數列求和][約分]
阿新 • • 發佈:2021-01-26
題目描述
1/1 + 1/2 + 1/4 + 1/8 + 1/16 + ....
每項是前一項的一半,如果一共有20項,求這個和是多少,結果用分數表示出來。
類似:3/2當然,這只是加了前2項而已。分子分母要求互質。
輸出
按格式輸出答案
題解
https://blog.csdn.net/weixin_43914593/article/details/112517638
等比數列求和
等差數列求和
手算2**20-1/2**19,其實可以直接寫程式碼打印出來答案了
約分
int t = __gcd(a, b); //除去公約數
cout << a/t << "/" << b/t;
#include<bits/stdc++.h>
using namespace std;
int main(){
int a = (1 << 20) - 1; //分子
int b = (1 << 19); //分母
int t = __gcd(a, b); //除去公約數
cout << a/t << "/" << b/t;
return 0;
}
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<set> #include<queue> #include<stack> #include<map> #include<cmath> using namespace std; typedef long long ll; const int maxn = 10100; ll gcd(ll a,ll b) { ll m; m=a%b; while(m!=0) { a=b; b=m; m=a%b; } return b; } int main() { ll a=pow(2,20)-1; ll b=pow(2,19); //ll g=gcd(a,b); //cout<<(a/g)<<"/"<<(b/g)<<endl; cout<<a<<"/"<<b; return 0; }