Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

c語言作業

請設計一個猜謎遊戲,首先程式會先產生1~10之間的亂數當作被猜的數字,執行過程中會提示猜謎者所猜的數字應該再大一點或小一點,以縮小猜的範圍,若猜到正確的數字會印出“You Got it!”,並印出相關的程式需求(1)奇數數值總合(2)累加總合(3)質數判斷(4)階乘運算,此猜謎遊戲最多讓使用者猜謎5次,若還是錯誤請印出”You Lose”。

Update:

要用while迴圈和

include

srand(time(NULL));

變數=rand()%數值+1;

請輸入第一次猜謎數值(1~10之間):10

請再猜低一點

請輸入第二次猜謎數值(1~10之間):4

(1)此數值4以內(包含此數值)的奇數數值總和為:1+3=4

(2)從1累加到4的總和為:10

(3)數值4以內的質數數值有哪些:2、3

(4)4!( n 階乘)的數值:24

今天就要

請大家幫我

我真的想不出來

3 Answers

Rating
  • 1 decade ago
    Favorite Answer

    #include <stdio.h>

    #define COMPUTER_RAND() rand()%10+1

    int get_prime(int num)//質數查詢,為質數傳回1,非質數傳回0

    {

    int i;

    int flag=1;

    for(i=2;i<num;i++)

    {

    if(num%i == 0)//如果此數可以從2~(num-1)被整除

    flag=0;//旗標設為0,代表不為質數

    }

    return flag;

    }

    int main(void)

    {

    int computer;

    int num;

    int i;

    int odd=0;

    int j;

    srand(time(NULL));

    //printf("%d",COMPUTER_RAND());

    computer = COMPUTER_RAND();printf("%d\n",computer);

    printf("請輸入第一次猜謎數值(1~10之間):");

    scanf("%d",&num);

    fflush(stdin);

    if(computer > num)

    printf("請再猜高一點\n");

    if(computer < num)

    printf("請再猜低一點\n");

    if(computer == num)

    {

    printf("You Got it\n");

    }

    else

    {

    printf("請輸入第二次猜謎數值(1~10之間):");

    scanf("%d",&num);

    fflush(stdin);

    if(computer > num)

    printf("請再猜高一點\n");

    if(computer < num)

    printf("請再猜低一點\n");

    if(computer == num)

    {

    printf("You Got it\n");

    }

    }

    //printf("%d\n",num);

    for(i=1;i<=num;i++)

    {

    if( (i%2) !=0)

    odd = odd + i;

    }

    printf("(1)此數值%d以內(包含此數值)的奇數數值總和為:%d\n",num,odd);

    odd = 0;

    for(i=1;i<=num;i++)

    odd = odd +i;

    printf("(2)從1累加到%d的總和為:%d\n",num,odd);

    printf("(3)數值%d以內的質數數值有哪些:",num);

    for(i=2;i<num;i++)

    {

    if(get_prime(i) == 1)//如果為質數

    printf("%d ",i);

    }

    printf("\n");

    odd = 1;

    for(i=1;i<=num;i++)

    {

    odd =odd*i;

    }

    printf("(4)%d!( n 階乘)的數值:%d\n",num,odd);

    system("pause");

    return 0;

    }

    Source(s): 自己,C語言答案,有問題再問
  • 1 decade ago

    你的題目和我們老師給的題目好像阿 |||

  • MrAYD
    Lv 5
    1 decade ago

    #include "stdafx.h"

    #include <iostream>

    #include <stdlib.h>

    #include <time.h>

    using namespace std;

    bool prime(int value)

    {

    int i=2;

    while(i<value)

    {

    if(!(value%i))return false;

    i++;

    }

    return true;

    }

    int fact(int n)

    {

    return (n>=1)?n*fact(n-1):1;

    }

    int _tmain(int argc, char * argv[])

    {

    int i=0,j,value,sum=0,sumA=0;

    srand(time(NULL));

    int Num=rand()%10+1;

    do

    {

    cin>>value;

    if(Num==value)

    {

    cout<<"Prime:";

    for(j=1;j<=value;j+=1)

    {

    sum+=j;

    if(prime(j)) cout<<j<<"\t";

    if(j%2) sumA+=j;

    else continue;

    }

    cout<<endl;

    cout<<sum<<endl;

    cout<<sumA<<endl;

    cout<<fact(value)<<endl;

    }

    else if(value>Num) cout<<"too Big!!"<<endl;

    else cout<<"too small!!"<<endl;

    i++;

    }while(i<5);

    cout<<"You Loss!!"<<endl;

    cin.ignore();

    cin.ignore();

    return 0;

    }

    2007-12-09 20:42:23 補充:

    如何改成C自己想想吧!!

    Source(s): ME, ME
Still have questions? Get your answers by asking now.