原文:
给定一个整数 n ,任务是打印任意一对 gcd 和 lcm 之和等于 n 的整数。 示例:
输入: n = 14 输出: 1,13 解释 : 对于给定的对,我们有 gcd(1,13) = 1 和 lcm (1,13) = 13。gcd 和 lcm 之和= 1 13 = 14。
输入: 2 输出: 1 1 解释 : 对于给定的对,我们有 gcd(1,1) = 1 和 lcm (1,1) = 1。gcd 和 lcm 之和= 1 1 = 2。
方法: 为了解决上面提到的问题,让我们考虑这一对是(1,n-1)。 gcd 为(1,n-1) = 1,lcm 为(1,n-1)= n–1。因此,gcd 和 lcm 之和= 1 (n–1)= n。因此,对(1,n–1)将是 gcd 和 lcm 之和等于 n 的对。 下面是上述方法的实现:
c
// c implementation to print any pair of integers
// whose summation of gcd and lcm is equal to integer n
#include
using namespace std;
// function to print the required pair
void printpair(int n)
{
// print the pair
cout << 1 << " " << n - 1;
}
// driver code
int main()
{
int n = 14;
printpair(n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation to print any pair of integers
// whose summation of gcd and lcm is equal to integer n
class gfg{
// function to print the required pair
static void printpair(int n)
{
// print the pair
system.out.print(1 " " (n - 1));
}
// driver code
public static void main(string[] args)
{
int n = 14;
printpair(n);
}
}
// this code is contributed by gauravrajput1
python 3
# python3 implementation to print any
# pair of integers whose summation of
# gcd and lcm is equal to integer n
# function to print the required pair
def printpair(n):
# print the pair
print("1", end = " ")
print(n - 1)
# driver code
n = 14
printpair(n)
# this code is contributed by pratikbasu
c
// c# implementation to print any pair
// of integers whose summation of
// gcd and lcm is equal to integer n
using system;
public class gfg{
// function to print the required pair
static void printpair(int n)
{
// print the pair
console.write(1 " " (n - 1));
}
// driver code
public static void main(string[] args)
{
int n = 14;
printpair(n);
}
}
// this code is contributed by princi singh
java 描述语言
output:
1 13
时间复杂度: o(1)
辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处