2017年10月8日 星期日

[C] 猜數字遊戲

/*
 
說明
 
猜數字遊戲
讓系統隨機生成一數字介於1到5之間
使用者輸入一數字猜此數字是什麼
猜對的話輸出”猜對了”
猜錯則反之輸出”猜錯了”
 
Input Format
 
猜的數字1-5
 
Output Format
 
猜對的話輸出”你猜對了 答案正是 x”
猜錯則反之輸出”猜錯了了喔 其實是 x”
 
Sample Input
 
3
 
Sample Output
 
bingo answer is 3
 
Sample Input
 
3
 
Sample Output
 
wrong answer, answer is 5
 
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main()
{
    int guess, keyin;
    srand((unsigned)time(NULL));
    guess = rand()%5+1;
   
    scanf("%d", &keyin);
   
    if(guess == keyin)
        printf("bingo answer is %d\n", guess);
       
    else
        printf("wrong answer, answer is %d\n", guess);
       
    return 0;
}

沒有留言:

張貼留言