Ask a question about the c language source code.

Asked 2 years ago, Updated 2 years ago, 104 views

There are three conditions that I want to fill out.

The first line has written the code.
Second, res was used. By the way, where should I put the res++; code?
Please tell me the location. And How do I make the code for the third condition?

<Code Created>

#include <stdio.h>
#include <math.h>

#define size 1000001

int num[size] = { 0, };

int main() {
    int M, N, i, j, res = 0;
    int tmp = 0;

    for (i = 2; i < size; i++)
        num[i] = i;

    scanf_s("%d %d", &M, &N);
    tmp = (int)sqrt(N);

    for (i = 2; i <= tmp; i++) {
        if (num[i] == 0) 
            continue;
        else {
            for (j = i + 1; j <= N; j++) {
                if (num[j] == 0)
                    continue;

                if (num[j] % i == 0)
                    num[j] = 0; 
            }
        }
    }
    for (i = M; i <= N; i++) {
        if (num[i] != 0) 
            printf("%d", i); 
    }

    printf("\n%d", res);


    return 0;
}

c c++ source coding code

2022-09-20 16:37

1 Answers

I think it would be better to change the variable name. For example:

int N, M; 
scanf_s("%d %d", &N, &M); 
printf("%d", N + M);

When writing a short code on your own like "?You can see that the variable means ~," but as a group (especially when asking questions), it is not readable and it is annoying to answer. Please refer to the code below.

#include <stdio.h>
int main() {
    int n, prime_count = 0, index = 0, arr[100];
    scanf_s("%d", &n);
    for (int i=2; i <= n; i++) {
        int count = 0;
        for (int j=1; j <= i; j++) {
            if (i % j == 0) {
                count++;
            }
        }
        if (count == 2) {
            arr[prime_count] = i;
            prime_count++;
            printf("%d ", i); //1
        }
    }
    printf("\n"); //2
    printf("%d\n", prime_count); //2

    int min = arr[0], max = arr[0]; 
    for (int i=0; i < prime_count; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    printf("%d %d %d", min, max, min + max); //3
    return 0;
}


2022-09-20 16:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.