Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103498
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1011
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-15 13:58
个人简介

普普通通一个人

文章分类

全部博文(102)

文章存档

2018年(1)

2015年(13)

2014年(88)

我的朋友

分类: C/C++

2014-02-05 07:44:32


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #define SIZE 4


  3. /* function prototype */
  4. void introduction(void); /* game introduction */
  5. void judgement(int[], int[]);

  6. int main(void){
  7.     int goal[SIZE] = { 1, 2, 3, 4 };
  8.     int guess[SIZE];

  9.     introduction(); /* title: illustrate how to play the game */

  10.     
  11.     if ( getchar() == 'y' ){
  12.         printf("Okey! Let`s start\n");
  13.         
  14.         int input_num;
  15.         while (scanf_s("%d", &input_num) != EOF){
  16.             /* Use the every bits of four digits, assigned
  17.             to the array guess`s element, respectively */
  18.             guess[0] = input_num / 1000;
  19.             guess[1] = (input_num - guess[0] * 1000) / 100;
  20.             guess[2] = (input_num - (guess[0] * 1000 + guess[1] * 100)) / 10;
  21.             guess[3] = input_num - (guess[0] * 1000 + guess[1] * 100 + guess[2] * 10);
  22.             /* print the number just input */
  23.             printf("%d%d%d%d", guess[0], guess[1], guess[2], guess[3]);
  24.             /* call the judgement function */
  25.             judgement(goal, guess);
  26.         }
  27.     }
  28.     else{
  29.         printf("Okey! See you next time, Bye!\n");
  30.     }
  31.     
  32.     return 0;
  33. }

  34. /* only print the illustrate of the game on screen */
  35. void introduction(void){
  36.      printf("********************* GAME INTRODUCTION *********************\n\n"
  37.          "There is a number composed by 4 figures(0~9), and without repetition.\n"
  38.          "Guess the number. If the figure and the position all correct, then\n"
  39.          "output A, if the figure is correct but the positon is incorrect,\n"
  40.          "then output B, the goal is 4A0B. OKey!! Come on baby!!!\n"
  41.          "Are you ready?(y/n):");
  42. }

  43. /* judge the goal number and the guess number matching or not */
  44. void judgement(int judge_goal[SIZE], int judge_guess[SIZE]){
  45.     int a = 0, b = 0;/* counter */
  46.     /* count the number and the position all correct */
  47.     for (int i = 0; i < SIZE; i++){
  48.         if (judge_goal[i] == judge_guess[i]){
  49.             a++;
  50.         }
  51.         /* count the number correct (if the number matching, then b+1)*/
  52.         for (int j = 0; j < SIZE; j++){
  53.             if (judge_goal[i] == judge_guess[j])
  54.                 b++;
  55.         }
  56.     }
  57.     /* b include a, so b-a */
  58.     b = b - a;
  59.     printf("------%d数字、位置都正确; %d数字正确、但位置不正确\n", a, b);
  60. }

阅读(203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~