您的位置:首页 > 其它

HDU 2012 FZU 1756关于素数的一些水题

2014-07-23 20:55 267 查看


HDU 2012 素数判定

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 72727 Accepted Submission(s): 25323



Problem Description

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input

输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output

对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Sample Input

0 1
0 0


Sample Output

OK


思路:筛选法求素数

#include<iostream>
using namespace std;
bool bf[2600];
void getss(){
for(int i=2;i<2600;i++)
{
if(!bf[i]){
for(int j=2;j*i<2600;j++){
bf[i*j]=true;
}
}

}
}
int main(){
memset(bf,false,sizeof(bf));
getss();
int x,y;
bool is;
while(cin>>x>>y){
if(x==0&&y==0)break;
is=true;
for(int k=x;k<=y;k++){
if(bf[k*k+k+41]==true){
is=false;break;
}
}
if(is==true) cout<<"OK"<<endl;
else cout<<"Sorry"<<endl;
}
return 0;

}


FZU Problem 1756 FactorSum

Accept: 429 Submit: 1134

Time Limit: 1000 mSec Memory Limit : 32768 KB



Problem Description

Give you a signed integer n (n<=10000), please tell me the sum of its all non-negative factors, and all its factors must larger than zero. Say, give you 12, you know, 1,2,3,4,6 are its factors. So the answer would be 1+2+3+4+6=16. if n < 0 , just output
"0"

There are several test cases, for evey case, there is exactly one line containing an integer N (N<=10000).



Sample Input

1234



Sample Output

0113

思路:对于每个数字,他的倍数都要加上他;比如 我们一共要求到6;那么从一开始是一的倍数(不超过6)的都要加1;直到6;然后在从2开始 以此类推;

#include<iostream>
using namespace std;
int bf[10001];
int main(){
memset(bf,0,sizeof(bf));
for(int i=1;i<=10000;i++)
for(int j=2;i*j<=10000;j++){
bf[i*j]+=i;
}
int n;
while(cin>>n){
if(n<0)cout<<0<<endl;

else cout<<bf
<<endl;
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: