PC/UVa 110705/10168 Summation of Four Primes
来源:互联网 发布:淘宝网儿童玩具飞机 编辑:程序博客网 时间:2024/05/14 18:46
求四个素数之和等于给定的数n ! 题目中描述每个奇数要么是素数要么可以写成三个素数之和,也就是说如果n是奇数,四个数之和当中肯定有一个数是2 ,否则就形不成一个奇数,n-2 扔为奇数,第二个数取3 ,n-2-5 为偶数,因为后面说一个偶数一定能写成两个素数之和,对将n-5分解成两个素数就可以了。如果一个数n是偶数,取前两个数为2 ,n-4仍为偶数,一定能分解成两个素数,题中说可以任意输出一组答案,所以可以固定前两个数
当n小于8 的时候无法写成四个素数之和
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int jugdeprime(int n)
{
int i;
if(n==1||n==0) return 0;
if(n%2==0&&n!=2)
return 0;
for(i=3;i*i<=n;i+=2)
if(n%i==0)
return 0;
return 1;
}
void divice(int n,int &x,int &y)
{
int i;
int t;
t=n-2;
if(jugdeprime(t))
{
x=2;
y=t;
return ;
}
for(i=3;i<=n/2+1;i+=2)
{
t=n-i;
int t1=jugdeprime(i);
int t2=jugdeprime(t);
if(t1!=0&&t2!=0)
{
x=i;
y=t;
return ;
}
}
}
int solve(int n)
{
int x, y;
if(n%2==0)
{
n-=4;
x=0;
y=0;
divice(n,x,y);
if(x&&y)
{
printf("2 2 %d %d\n",x,y);
return 1;
}
}
else
{
n-=5;
x=0;
y=0;
divice(n,x,y);
if(x!=0&&y!=0)
{
printf("2 3 %d %d\n",x,y);
return 1;
}
}
return 0;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n<8||solve(n)==0)
printf("Impossible.\n");
}
return 0;
}
发现人类的语言室多么的奇妙啊,不仔细分析还真会走不少弯路,最后还不对,我是看了高手的分析才搞明白为什么我的老是超时滴!!!以后要好好的额学学语文了
- PC/UVa 110705/10168 Summation of Four Primes
- uva 10168 summation of four primes
- Uva 10168 Summation of Four Primes 素数
- UVA-10168 Summation of Four Primes
- UVa 10168 - Summation of Four Primes
- Uva 10168 - Summation of Four Primes
- UVa 10168 Summation of Four Primes
- 110705 Summation of Four Primes
- 10168 Summation of Four Primes
- UVa Problem Solution: 10168 - Summation of Four Primes
- (数论2.1.2)UVA 10168Summation of Four Primes(欧拉筛法)
- UVA 10168 Summation of Four Primes(数论)
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- 数论刷题-uva【10168】- Summation of Four Primes
- summation of four primes
- Summation of Four Primes - PC110705
- UVa Problem 10168 Summation of Four Primes (四素数之和)
- Web操作
- 判断回文数字
- 【软件测试自动化-QTP系列讲座 52】== VBS类的伪构造器模式设计(一) ==
- /etc/environment 和 /etc/profile
- 我的 html作业
- PC/UVa 110705/10168 Summation of Four Primes
- 使用CppUnit时出现的几个问题
- Java基础第一讲:Java的故事和Java编程环境搭建
- android ListView&&适配器使用
- 单元最短路径
- 不同字符集的介绍
- gcc 安装 之 rpm 篇章
- Fedora配置root用户可登陆
- 《我的2012》----纪念青春