zoukankan      html  css  js  c++  java
  • 杭电 Problem 2008 分拆素数和 【打表】

    分拆素数和

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 33196    Accepted Submission(s): 14406


    Problem Description
    把一个偶数拆成两个不同素数的和,有几种拆法呢?
     

    Input
    输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
     

    Output
    对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
     

    Sample Input
    30 26 0
     

    Sample Output
    3 2
     
     首先,先打一个素数表,然后用一个ans数组标记状态。两个不相等的素数和为下标,数组每被调用一次,就加一,这样所求偶数对应数组下表下的值就是答案。

    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <cstdlib>
    #include <cstdio>
    #define MAX_N   10005
    #define TY  int
    #define MAX(a, b)   ((a > b)? a: b)
    #define MIN(a, b)   ((a < b)? a: b)
    using namespace std;
    
    bool s[MAX_N];
    int ans[MAX_N];
    void init() {
        memset(s, false, sizeof(s));
        s[0] = s[1] = true;
        for (int i = 0; i < MAX_N; i++) {
            if (s[i])   continue;
            for (int j = 2*i;  j < MAX_N; j += i) {
                s[j] = true;
            }
        }
    }
    void solve() {
        for (int i = 2; i < MAX_N - 2; i++) {
            if (s[i]) continue;
            for (int j = i + 1; j < MAX_N - 1; j++) {
                if (s[j] || i + j > MAX_N) continue;
                    ans[i + j]++;
            }
        }
    }
    int main() {
        int n;
        init();
        solve();
        while (scanf("%d", &n), n) {
            printf("%d
    ", ans[n]);
        }
        return 0;
    }
    


  • 相关阅读:
    9ch
    thymeleaf 的使用(三)--举例使用
    thymeleaf 的使用(二)--基本语法
    thymeleaf 的使用(一)--导入和基本操作
    SpringBoot对静态资源的映射规则
    第一个Spring Boot项目
    HTML文本换行问题
    判断for(var i=0;i<=3;i++){ setTimeout(function() { console.log(i) }, 10) }
    js的执行机制——宏任务和微任务
    v-if 和 v-show 的区别
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770930.html
Copyright © 2011-2022 走看看