0

宿題のために C で関数を書かなければなりません。関数 is_prime(n) と nth_prime(n) を考えると、n が素数の場合は 1 を返し (そうでない場合は 0)、nth_prime は n 番目の素数を返すため、時間をカウントする関数 next_prime(count) を作成する必要があります。呼び出されてから、「カウント番目」の素数を返します。count は静的な unsigned int 変数でなければなりません。n=0 (n は scanf で指定) の場合、カウント値を 0 にリセットする必要があり、関数は最初の素数 2 を返します。
構造体、配列、または再帰を使用できません。私はコーディングが初めてで、何をすべきかわかりません。Visual Studio 2010 を使用しており、ISO C89 (ANSI C) としてコンパイルする必要があります。関数は、評価される唯一のものであるライブラリ ファイルに記述する必要があるため、main () 関数で count++ を使用することはできません。ここ'

unsigned int next_prime( unsigned int count ) {
    if( count == 0 ) {
        if ( n=!0 ) {                       
            return nth_prime( count );
            count++;
        } else {
            count = 0;
            return 2;
        }      
    } else {    
        if ( n=!0 ) {                       
            return nth_prime( count );
        } else {
            count = 0;
            return 2; 
        }       
    }
}   
4

1 に答える 1

0

Here is a function that will meet your question:

/* the function next_prime(count) */
unsigned int next_prime(unsigned int count) {
    /* avoid "unused parameter" warning */
    (void)count;
    /* introduce this block because defining the variable count here will read to an redeclaring error */
    {
        static unsigned int count = 0;
        int n = -1;
        /* n is given with a scanf */
        scanf("%d", &n);
        /* if n=0 */
        if (n == 0) {
            /* count value must be reset to 0 */
            count = 0;
            /* return the first prime number, 2 */
            return 2;
        } else {
            /* count the time it is called */
            count++;
            /* return the "count-th" prime number */
            return nth_prime(count);
        }
    }
}
于 2015-10-25T11:47:22.653 に答える