夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

昨日と今日の

パソコンの動作がおもいからブログ書く。←なぞの理由
素数問題ばっか解いたw
あと一問で60問だけど気にしない

3518

素数

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int prime[100000];
    int pnum = 0;
    bool isprime[1299710];
    fill(isprime, isprime+1299710, true);
    isprime[0]=isprime[1]=false;
    for (int i=2; i<1299710; i++) {
        if (isprime[i]) {
            prime[pnum++] = i;
            for (int j=i+i; j<1299710; j+=i) isprime[j]=false;
        }
    }
    int num;
    while (scanf("%d", &num)) {
        if (num == 0) break;
        if (isprime[num]||num<2) {
            printf("0\n");
            continue;
        }
        int res;
        for (int i=0; i<100000; i++) {
            if (prime[i] < num ) {
                res = prime[i+1] - prime[i];
            }
        }
        printf("%d\n", res);
    }
}

2739

素数

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int prime[10000];
    int pnum = 0;
    bool isprime[10001];
    fill(isprime, isprime+10001, true);
    isprime[0]=isprime[1]=false;
    for (int i=2; i<10001; i++) {
        if (isprime[i]) {
            prime[pnum++] = i;
            for (int j=i+i; j<10001; j+=i) isprime[j]=false;
        }
    }
    int num;
    while (scanf("%d", &num)) {
        if (num == 0) break;
        int res = 0;
        for (int i=0; prime[i]<=num; i++) {
            int sum = prime[i];
            for (int j=i+1; j<10000; j++) {
                if (sum == num) {
                    res++;
                    break;
                }
                if (sum > num) break;
                sum += prime[j];
            }
        }
        printf("%d\n", res);
    }
}

3006

素数
問題文が日本語

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int a, d, n;
    bool isprime[1000001];
    fill(isprime, isprime+1000001, true);
    isprime[0]=isprime[1]=false;
    for (int i=2; i<1000001; i++) {
        if (isprime[i]) {
            for (int j=i+i; j<1000001; j+=i) isprime[j]=false;
        }
    }
    while (scanf("%d%d%d", &a, &d, &n)) {
        if (a == 0 && d == 0 && n == 0) break;
        int cnt=0;
        int i;
        for (i=a; cnt!=n; i+=d) if (isprime[i]) cnt++;
        printf("%d\n", i-d);
    }
}

1504

てむや

#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
#define ALL(c) (c).begin(), (c).end()
int toInt(string s)
{
    stringstream ss;
    ss.str(s);
    int res;
    ss >> res;
    return res;
}

string toString(int i)
{
    stringstream ss;
    ss << i;
    return ss.str();
}

int main()
{
    int N; cin >> N;
    string num1, num2, res;
    int n1, n2, sum;
    for (int ix=0; ix<N; ix++) {
        cin >> num1 >> num2;
        reverse(ALL(num1));
        reverse(ALL(num2));
        n1 = toInt(num1);
        n2 = toInt(num2);
        sum = n1 + n2;
        res = toString(sum);
        reverse(ALL(res));
        cout << toInt(res) << endl;
    }
}

1552

ていや

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int in;
    while (scanf("%d",&in)) {
        if (in == -1) break;
        vector<int> num;
        num.push_back(in);
        while (scanf("%d",&in)) {
            if (in == 0) break;
            num.push_back(in);
        }
        sort(num.begin(), num.end());
        int cnt = 0;
        for (int i=0; i<num.size()-1; i++) {
            int temp = num[i];
            for (int j=i+1; j<num.size(); j++) {
                if (temp*2 == num[j]) {
                    cnt++;
                    break;
                }
            }
        }
        printf("%d\n",cnt);
    }
}

ていや

簡単な問題いいよ←

広告を非表示にする