zoukankan      html  css  js  c++  java
  • 素数之和

    题目来自:网络

    Description

    对于任意给定的两个整型数N和M,你需要做的事情是求出[N,M]之间的所有素数之和。我们确保0 <= N < M <= 100。

    所谓素数即:只能被1及其自身整除的数。注:1不是素数。

    Input

    输入的数据仅包含两个整型数N和M,N和M之间用空格间隔

    Output

    输出只包含一个数,即[N, M]之间的素数之和

    Sample Input

    1 10

    Sample Output

    17

    //-----------------------------------------------------------------------

    //这个还是比较简单的

    #include <iostream>
    #include <cmath>
    using namespace std;
    void sum_sushu(int a,int b);
    bool Is_sushu(int n);
    int main()
    {
        int a,b;
        cin >> a >> b;
        sum_sushu(a,b);
        return 0;
    }
    bool Is_sushu(int n)
    {
        if (n < 2)
        {
            return false;
        }
        int i,j;
        for (i = 2,j = int(sqrt(n));i<=j;i++)
        {
           // cout << j<< endl;    // Using cout to check how 'j' change
            if ((n % i) == 0)
            {
                return false;
            }
            //cout << n % i << endl;  // Using cout to check how ' n%i ' change
        }
        return true;
    }
    void sum_sushu(int a,int b)
    {
        int sum = 0;
        for (int i = a;i <= b;i++)
        {
            if (Is_sushu(i))
            {
                sum += i;
                //cout << i << " ";    //Using cout to check how ' i ' change
            }
        }
        cout << sum << endl;
    }

    //I like to cout some variables to check if they are right, and how they change

    //Could you please send your codes to my e-mail ediszhao@sina.com If you have a better way to deal with it

  • 相关阅读:
    Linux安装nginx
    Linux安装vsftp服务
    maven的Tomcat插件使用
    Mybatis逆向工程生成代码
    千里之行,始于足下
    java 通过反射获取注解
    天气预报需要用到的jar包
    JDBC 利用反射 配置文件
    从网页下载图片的代码
    装箱/拆箱 对象排序
  • 原文地址:https://www.cnblogs.com/ediszhao/p/3481491.html
Copyright © 2011-2022 走看看