首页 > 技术文章 > code vs1706 求合数和(数论 素数的判定)

cangT-Tlan 2016-11-11 14:42 原文

1706 求合数和

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 白银 Silver
 
 
题目描述 Description

用户输入一个数,然后输出从1开始一直到这个数为止(包括这个数)中所有的合数的和。

输入描述 Input Description

一个整数N,0<N<=1000

输出描述 Output Description

一行,一个整数,即从1到N中所有合数的和

样例输入 Sample Input

样例一:100

 

样例二:9

样例输出 Sample Output

样例一:3989

 

样例二:27

数据范围及提示 Data Size & Hint

先找出素数,然后把不是素数的和相加。

分类标签 Tags 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> 
using namespace std;
int n,ans;
bool aa(int x){
    for(int i = 2; i <= sqrt(x); i ++)
        if(x%i==0)    return 1;
    return 0;
}
int main(){
    cin>>n;
    for(int i = 4; i <= n; i ++){
        if(aa(i))
        ans+=i;
    }
    cout<<ans;
}

思路:简单,不解释;

 

推荐阅读