链接:https://ac.nowcoder.com/acm/contest/1083/C来源:牛客网
题目描述
给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角三角形。
输入描述:
一个整数n。
输出描述:
另外两条边b,c。答案不唯一,只要输出任意一组即为合理,如果无法构造请输出-1。
示例1
输入
[复制](javascript:void(0)😉
3
输出
[复制](javascript:void(0)😉
4 5
示例2
输入
[复制](javascript:void(0)😉
4
输出
[复制](javascript:void(0)😉
3 5
备注:
0<=n<=1e91<=b,c<=1e18n,b,c均为整数
思路:
通过一下程序打表,可以发现规律:
当\(n>=3\) 时,一定有答案。
当n为奇数时,一定有a,b 满足 a=b-1 ,\(a*a+n*n=b*b\)
当n为偶数时,一定有a,b 满足 a=b-2 ,\(a*a+n*n=b*b\)
把b用a表示就是一个一元一次方程,求解即可。
打表程序:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string S(ll n) {stringstream ss; string s; ss << n; ss >> s; return s;}
ll N(string s) {stringstream ss; ll n; ss << s; ss >> n; return n;}
string rm0(string s) { // 去除前导0函数
int i;
for (i = 0; i < s.size() - 1; i++)
if (s[i] != '0')
break;
return s.substr(i);
}
string ADD(string s, string t) { // 字符串整数相加,返回一个字符串
if (s.size() < t.size())swap(s, t); s = '0' + s;
reverse(t.begin(), t.end()); while (s.size() > t.size())t += '0'; reverse(t.begin(), t.end()); int c = 0;
for (int i = s.size(); i >= 0; i--)
{
if (c) {
if (s[i] == '9') {
s[i] = '0'; c = 1;
} else {
s[i] = (char)(s[i] + 1); c = 0;
}
}
int sum = (int)s[i] + (int)t[i] - '0' * 2;
if (sum >= 10) {
s[i] = (char)(sum - 10 + '0'); c = 1;
}
else
s[i] = (char)(sum + '0');
}
return rm0(s);
}
bool cmp(string s, string t) {
// s>=t 返回1
// s<t 返回0
if (s.size() != t.size())
return s.size() > t.size();
for (int i = 0; i < s.size(); i++)
if (s[i] != t[i])
return s[i] > t[i];
return 1;
}
// 字符串减法,不带负号
// int x[MAXN], y[MAXN];
// string minuss(string a, string b)
// {
// int la = a.length(), lb = b.length(), lc, i;
// memset(y, 0, sizeof(y)); lc = max(la, lb); string ans;
// for (i = 0; i < la; i++)x[la - i - 1] = a[i] - 48;
// for (i = 0; i < lb; i++)y[lb - i - 1] = b[i] - 48;
// for (i = 0; i < lc; i++)
// {
// x[i] -= y[i];
// if (x[i] < 0)x[i + 1]--, x[i] += 10;
// }
// while (!x[lc - 1] && lc > 1)lc--;
// for (i = lc - 1; i + 1; i--)ans.push_back(x[i] + 48);
// return ans;
// }
string add(string a, string b)
{
// 二进制带补位加法
int len1 = a.size(), len2 = b.size();
if (len1 > len2)swap(a, b), swap(len1, len2);
reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
for (int i = 0; i < len2 - len1; i++)a += "0";
int f = 0;
string ans = "";
for (int i = 0; i < len2; i++) {
int k = a[i] - '0' + b[i] - '0' + f; f = 0;
if (k >= 2)k %= 2, f = 1;
if (k == 0)ans += "0";
else ans += "1";
}
if (f)ans += "1";
reverse(ans.begin(), ans.end());
return ans;
}
int a[maxn];
set<string> st;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int n;
cin >> n;
repd(i, 1, n)
{
repd(j, 1, n)
{
repd(k, 1, n)
{
if (i * i + j * j == k * k || (j * j + k * k) == i * i)
{
int g = 1;
a[0] = i / g;
a[1] = j / g;
a[2] = k / g;
sort(a, a + 3);
string s = S(a[0]) + S(a[1]) + S(a[2]);
if (st.count(s) == 0)
{
st.insert(s);
cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
}
}
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
ll n;
cin >> n;
if (n <= 2)
{
cout << -1 << endl;
} else
{
if (n & 1)
{
cout << ((n * n - 1) >> 1) << " " << (n * n - 1ll >> 1) + 1 << endl;
} else
{
cout << (n * n - 4ll) / 4ll << " " << (n * n - 4ll) / 4ll + 2 << endl;
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}