【作业】两个很简单的题目

计划把一些太简单的题还是放到一起说好了

题目1

边长a, b, c, n 组成一直角三角形,其中他们关系为 (a+b) x (a+b)+ c x c = n x n, 且a+b < c
问有a,b, c, n有多少种组合

结论

input n:55

1, 32, 44
2, 31, 44
3, 30, 44
4, 29, 44
5, 28, 44
6, 27, 44
7, 26, 44
8, 25, 44
9, 24, 44
10, 23, 44
11, 22, 44
12, 21, 44
13, 20, 44
14, 19, 44
15, 18, 44
16, 17, 44
res: 16

代码

#include <iostream>
#include <vector>
#include <array>

int _find_triangle_way(int n) {
	int way_cnt = 0;
	std::vector<std::array<int, 3>> ways;
	
	int n_s = n * n;
	int start = int(std::sqrt((double)n * (double)n) * 0.5);
	for (int c = start; c < n; ++c) {
		int c_s = c * c;
		int ab_s = n_s - c_s;
		if (ab_s < c_s) {
			double dab = std::sqrt((double)ab_s);
			int ab = (int)dab;
			if ((ab) * (ab)+c_s == n_s) {
				int half_ab = ab / 2;
				way_cnt += half_ab;
				for (int a = 1; a <= half_ab; ++a) {
					ways.push_back({ a, ab - a, c });
				}

			}
		}
	}
	if (!ways.empty()) {
		for (std::size_t i = 0; i < ways.size() - 1; ++i) {
			for (std::size_t j = i + 1; j < ways.size(); ++j) {
				if (ways[i] == ways[j]) {
					std::cout << "find the same ways at " << i << ", " << j << ": " << ways[i][0] << ", " << ways[i][1] << ", " << ways[i][2] << std::endl;
				}
			}
		}
	}
	for (auto w : ways) {
		std::cout << std::endl << w[0] << ", " << w[1] << ", " << w[2];
	}
	return way_cnt;
}

void find_triangle_way() {
	std::cout << "input n:";
	int n = 0;
	std::cin >> n;
	std::cout << std::endl << "res: " << _find_triangle_way(n);

}

题目2

两个字符a,b,轮流取他们中的字符,组成新的字符

结论

merge “abcbdefg” & “123456”, Res:a1b2c3b4d5e6fg

代码

#include <iostream>
#include <string>
#include <sstream>

std::string _merge_str(const std::string& a, const std::string& b) {
	std::stringstream strstr;
	std::size_t j = 0;
	std::size_t i = 0;
	while (i < a.size() || j < b.size()) {
		if(i < a.size()) strstr << a[i++];
		if(j < b.size()) strstr << b[j++];
	}
	return strstr.str();
}

void merge_str() {
	const char* a = "abcbdefg";
	const char* b = "123456";
	std::string res = _merge_str(a, b);
	std::cout << "merge \""<< a<< "\" & \""<< b << "\", Res:" << res;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值