【问题标题】:C lottery programC彩票程序
【发布时间】:2014-01-31 04:37:21
【问题描述】:

这是我的任务。

  1. 如果随机 3 位数字与用户 3 位数字完全匹配 然后是一些奖项。
  2. 如果用户匹配 3 位数字但不按顺序匹配,则其他一些 奖项。
  3. 如果用户匹配 2 位数字,则有其他奖品
  4. 如果用户输入仅匹配 1 位数字,则有其他奖励

这是我想出的:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
    int r,i,num[0];
  srand(time(NULL));
  r = rand()%(999-100+1)+100;

  printf("Here is the wininng number%d\n",r);

  printf("Enter three digit number to win lottery:\n");
  scanf("%d",num);

  for(i=0;i<3;i++){

    if(r==num[0]){
        printf("For three exact match you get $100,000\n");
    }else if((r/10)==(num[1]/10)){
        printf("For two number match you get $50,000\n");
    }else if((r%10)==(num[]%10)){
        printf("For one number match you get $10,000\n");
    }else{
        printf("You get nothing!\n");
    }}

}

我得到三位数的匹配,有时编译后三位数和两位数匹配。告诉我出了什么问题。提前谢谢你们。

【问题讨论】:

  • 我可以在逻辑和代码中看到很多问题。例如 num 被声明为 1 个元素的数组,但您仍然在访问数组的第 2 个元素

标签: c logical-operators


【解决方案1】:

你的逻辑错了。

通过将他们输入的内容与您生成的内容(中奖号码)进行比较,可以找到 3 位数的完全匹配。

3 位排列比较棘手,但您还需要考虑 2 位和 1 位排列。我可能会将生成的(中奖)号码转换为字符串,以及用户的号码。然后,您可以逐步查看中奖号码,计算中奖号码中与用户号码中未使用的数字匹配的位数。通过计数,您知道用户赢了什么(如果有的话)。注意,当一个数字已经匹配时,需要删除它,这样当中奖号码为666,用户输入456时,与6不匹配3次。

像这样:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int win;
    int num;

    srand(time(NULL));
    win = rand() % (999 - 100 + 1) + 100;

    printf("Here is the winning number: %d\n", win);

    printf("Enter three digit number to win lottery:\n");
    if (scanf("%d", &num) != 1)
        return 1;

    if (num == win)
        printf("For exact match you get $100,000\n");
    else if (num < 0 || num > 999)
        printf("Your number is out of range - you win nothing\n");
    else
    {
        char win_str[4];
        char try_str[4];
        sprintf(win_str, "%d", win);
        sprintf(try_str, "%d", num);
        int match = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (win_str[i] == try_str[j])
                {
                    try_str[j] = 'x';
                    match++;
                    break;
                }
            }
        }

        switch (match)
        {
        case 0:
            printf("No digits in %.3d match %3d - you win nothing\n", num, win);
            break;
        case 1:
            printf("One digit of %.3d matches %3d - you win $10,000\n", num, win);
            break;
        case 2:
            printf("Two digits of %.3d match %3d - you win $20,000\n", num, win);
            break;
        case 3:
            printf("Three digits of %.3d match %3d - you win $50,000\n", num, win);
            break;
        default:
            printf("The impossible happened (%.3d vs %3d gives %d matches)\n", num, win, match);
            break;
        }
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    由于某种原因,您声明了 num[0],这使得 num 成为长度为零的数组。

    请注意,“中奖号码”并不是真正的数字,而是一个数字字符串。

    您应该将num 声明为字符数组:

    char num[4];
    

    您可以通过以下方式阅读:

     scanf("%3s", num)
    

    然后您需要将读取为num[0]、num[1]、num[2] 的 ASCII 字符转换为与随机数的数字进行比较。

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多