A. Equation--------思维/枚举+两种做法

Let’s call a positive integer composite if it has at least one divisor other than 1 and itself. For example:

the following numbers are composite: 1024, 4, 6, 9;
the following numbers are not composite: 13, 1, 2, 3, 37.
You are given a positive integer n. Find two composite integers a,b such that a−b=n.

It can be proven that solution always exists.

Input
The input contains one integer n (1≤n≤107): the given integer.

Output
Print two composite integers a,b (2≤a,b≤109,a−b=n).

It can be proven, that solution always exists.

If there are several possible solutions, you can print any.

Examples
inputCopy

1
outputCopy
9 8
inputCopy
512
outputCopy
4608 4096

题意:找出两个合数a,b使得a-b=n,给出n

解析:
第一种做法:a-b=n,那么a=b+n,我们枚举b check(b)&&check(b+n),循环次数的一定很少因为素数很少。

第二种做法:a-b=n,那么a=3n,b=2n==3n-2n=n。对于1直接输出样例,对于其他的输出a3,b2;

第一种:

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int check(int x)
{
	int i;
	for( i=2;i<=sqrt(x)&&x%i;i++);
	if(i>sqrt(x)) return 0;
	return 1;
}
int main()
{
	cin>>n;
	for(int i=4;;i++)
	{
		if(check(i)&&check(i+n))
		{
			b=i;
			a=i+n;
			break;
		}
 
	}
	cout<<a<<" "<<b<<endl;
 } 

第二种:


#include<bits/stdc++.h>
using namespace std;
int n,a,b;
 
int main()
{
	cin>>n;
	if(n==1)
	{
		cout<<9<<" "<<8<<endl;
	}
	else cout<<n*3<<" "<<n*2<<endl;
 } 
发布了309 篇原创文章 · 获赞 6 · 访问量 5266

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104074719
今日推荐