HDU 5104-Primes Problem
来源:互联网 发布:类似于记事本的软件 编辑:程序博客网 时间:2024/06/05 10:23
Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3611 Accepted Submission(s): 1626
题目链接:点击打开链接
Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000) .
Output
For each test case, print the number of ways.
Sample Input
3
9
Sample Output
0
2
3
9
Sample Output
0
2
题意:
给你一个数 n ,让你找一组数 p1,p2,p3,满足 p1<=p2<=p3,并且这三个数都是质数,让你求能找几组满足条件的数。
分析:
第一遍做超时了,因为我先将10000内的质数找出来,存在一个数组 s 里,等输入 n 的时候,在遍历一下 s 数组,找到第一个大于 n 的数,
记住下标,再用三重 for 循环开始遍历,找符合条件的情况,完美的超时了,最后我想到了二分,让二分代替
最后一个for循环,但是队友给我提供了一个更简单的方法,就是首先完全没必要找到那个大于 n 的数,其次在开
一个数组,将所求的质数以数组下标的方式标记一下,用查数组下标的方式代替第三重 for 循环,哈哈哈,
无懈可击。
#include<iostream>#include<stdio.h>#include<string>#include<string.h>#include<math.h>using namespace std;int main(){ int n,k=0,sum,i,j; int s[10000],h[10000]={0}; for(i=2;i<=10000;i++) { for(j=2;j<=sqrt(i);j++) { if(i%j==0) break; } if(j>sqrt(i)) { s[k++]=i; h[i]=1;///用下标将其标记 } } while(~scanf("%d",&n)) { sum=0; for(i=0;;i++) { if(s[i]>=n) break;///如果大于n,则不用往后遍历了 for(j=i;s[j]<n;j++)///注意这里要有s[j]<n的控制条件 { if(h[n-s[i]-s[j]]==1&&(n-s[i]-s[j])>=s[j]) ///差数组下标,并且该数不能小于第二个数,如果不控制,会有重复的情况出现 sum++; } } printf("%d\n",sum); } return 0;}
阅读全文
0 0
- HDU 5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU-#5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU 5104 Primes Problem
- hdu 5104 Primes Problem
- HDU 5104 Primes Problem
- HDU 5104-Primes Problem
- hdu 5104 Primes Problem(数学)
- HDU 5104 - Primes Problem (枚举)
- HDU 5104 Primes Problem(数学)
- hdu 5104 Primes Problem(素数判定)
- HDU 5104 Primes Problem(打表)
- HDU--Primes Problem
- HDOJ 5104 Primes Problem
- HDU 5104 Bestcoder #18 1001 Primes Problem 水题
- CSS-相关练习1-表格实现奇数行和偶数行自行判断设置不同的背景颜色
- 求循环节
- Linux简介
- 1041. 考试座位号(15)--Python
- 装系统体验②
- HDU 5104-Primes Problem
- B
- MYSQL中group_concat有长度限制!默认1024
- 将 Centos 的 yum 源更换为国内的阿里云源
- localhost和127.0.0.1的概念
- CentOS 7.4系统-Apache-PHP安装-开发环境配置
- Android 7.1 GUI系统-BufferQueue 的管理(三)
- 强化学习相关资料
- Educational Codeforces Round 33 (Rated for Div. 2) 题解