如何找到在C n次贷 [英] How to find the nth prime in C

查看:182
本文介绍了如何找到在C n次贷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个应用程序将收到一个号码 N 。接收到该号码后,该方案具有显示 N -th中素数的列表中素数。例如,如果用户输入 3 ,程序应该显示 5 ,因为 5 是第三个首相起点在 2 。我知道什么是错我的code,但我不知道问题出在哪里,我怎么能解决这个问题。

的#include<&stdio.h中GT;诠释主(){
    诠释N,I,旗,素;
    INT计数器= 1;
    scanf函数(%d个,&安培; N);
    如果(N == 1)黄金= 2;
    其他
        做{
            素= 3;
            对于(i = 2; I<素;我++){
                标志= 1;
                如果(素%我== 0){
                    标志= 0;
                }
            }
            如果(标志== 1)
                反++;
            素++;
        }而(计数器= N!);
    如果(反== N)
    的printf(%d个\\ N,素数);
    返回0;
}


解决方案

你这样的政策的遗体修复示例:

 #包括LT&;&stdio.h中GT;诠释主要(无效){
    诠释N,I,旗,素;
    INT计数器= 1;    scanf函数(%d个,&安培; N);
    如果(N == 1)
        素= 2;
    其他{
        素= 1;
        做{
            黄金+ = 2;
            标志= 1;
            对于(i = 3; I<素,I + = 2){
                如果(素%我== 0){
                    标志= 0;
                    打破;
                }
            }
            如果(标志== 1)
                反++;
        }而(计数器= N!);
    }
    的printf(%d个\\ N,素数);    返回0;
}

This application will receive a number n. After receiving this number, the program has to show the n-th prime in the list of primes. For example, if the user enters 3, the program is supposed to display 5, because 5 is the third prime starting at 2. I know that something is wrong with my code but I don't know where the problem is and how I can fix it.

#include <stdio.h>

int main() {
    int n, i, flag, prime;
    int counter = 1;
    scanf("%d", &n);
    if (n == 1) prime = 2;
    else
        do{
            prime = 3;
            for (i = 2; i < prime; i++) {
                flag = 1;
                if (prime % i == 0) {
                    flag = 0;
                }
            }
            if (flag == 1)
                counter++;
            prime++;
        } while (counter != n);
    if (counter == n)
    printf("%d\n", prime);
    return 0;
}

解决方案

Fix sample of remains of your policy like this :

#include<stdio.h>

int main(void){
    int n, i, flag, prime;
    int counter = 1;

    scanf("%d", &n);
    if (n == 1)
        prime = 2;
    else {
        prime = 1;
        do{
            prime += 2;
            flag = 1;
            for (i = 3; i < prime; i+=2){
                if (prime % i == 0) {
                    flag = 0;
                    break;
                }
            }
            if(flag == 1)
                counter++;
        } while (counter != n);
    }
    printf("%d\n", prime);

    return 0;
}

这篇关于如何找到在C n次贷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆