Counting the little sheep again (four brushes) kkmd66

error prone

It can be seen from the question that a may be 0

Input:

There are multiple sets of test cases, input until the end of the file.
Each group of samples occupies one line, ab (0<=a,b<=65536)

Output:

The output of each group of samples occupies one line, and the output content is the sum of all prime numbers between a and b (excluding a and b).

#include <iostream>
#include <valarray>

using namespace std;

/**
 * kkmd66 四刷
 * @return
 */

int main() {
    
    

    int a, b;
    while (cin >> a >> b) {
    
    
        //判断大小,否则无法通过
        if (a > b)
            swap(a, b);

        //找素数,并求和
        long long sum = 0;
        for (int i = a + 1; i < b; ++i) {
    
    
            //判断是否为素数
            bool flag = true;
            for (int j = 2; j <= sqrt(i); ++j) {
    
    
                if (i % j == 0) {
    
    
                    flag = false;
                    break;
                }
            }
            //是素数,累加
            if (flag && i > 1)
                sum += i;
        }
        //输出
        cout << sum << endl;
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324069864&siteId=291194637