第九届蓝桥杯——分数

【问题描述】

1/1 + 1/2 + 1/4 + 1/8 + 1/16 + … 每项是前一项的一半,如果一共有 20 项,求这个和是多少,结果用分数表示出来。
类似:3/2,当然,这只是加了前2项而已。分子分母要求互质。

【答案提交】
需要提交的是已经约分过的分数,中间任何位置不能含有空格。
请不要填写任何多余的文字或符号。


解题思路:

前一项和 1/1,前两项和 3/2,前三项和 7/4,前四项和 15/8,这样的话应该可以得出规律了吧

题解:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int a = 1, b = 1;
	for (int i = 1; i < 20; i ++)
	{
		a = 2*a + 1;
		b *= 2;
	}
	cout << a << '/' << b << endl;
	
	return 0;
}

程序运行结果:1048575/524288,如何判断是不是最简式呢,程序如下

题解:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

// 求最大公约数 
int gcd(int a, int b)
{
	if(b == 0) return a;
	else return gcd(b, a % b);
}

int main()
{
	int a = 1, b = 1;
	for (int i = 1; i < 20; i ++)
	{
		a = 2*a + 1;
		b *= 2;
	}
	
	int t = gcd(a, b);
	cout << a/t << '/' << b/t << endl;
	
	return 0;
}

答案:1048575/524288

卑微求赞↓↓↓

发布了63 篇原创文章 · 获赞 5 · 访问量 828

猜你喜欢

转载自blog.csdn.net/weixin_46239370/article/details/105452987