如何编写C程序找到获胜者的号码? [英] How can I write a C program find the number of the winner?

查看:77
本文介绍了如何编写C程序找到获胜者的号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,输入是随机的,例如:



33 32 65 55 44 pe *& 44 ^%123 ^

现在我需要找到哪个两位数字最重复(并且数字需要包含1到9的数字,这意味着10,30 ..不是有效数字),现在两个数字之间分隔到另一个数字是每个输入exept为另一个数字,在这种情况下,我希望输出为44然后我将打印:



获胜的一对是:4蓝色,4红色。

现在我认为做这样的事情的最好方法是使用2D数组,当输入有效时,我将在sutble位置使用数组,之后我将找到最大的位置数组。





我的尝试:



I have this code that the input is random for example :

33 32 65 55 44 pe *&44^ %123^
now I need to find which two digit number was repeaed most (and the number needs to contain digits from 1 to 9, which mean 10,30.. are not valid numbers), now what seperate between two digit number to another is every input exept for another number, in this case I want the output to be 44 then I will print:

The winner couple is: 4 blue, 4 red.
now I thought the best way to do something like this is using 2D array, that when a input is valid I will apdate the array in the sutble place, and after that I will find the maximum place in the array.
.

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#define N 9

void update_array(int num[N][N]);
int find_max(int b[N][N]);

int main()
{
    int i,j;
    int num[N][N];
    int maximum=0,a=0,b=0;
    for(i=0;i<N;++i)
    {
      for(j=0;j<N;++j)
      {
       num[i][j]=0;
      }
    }
    update_array(num);
    maximum=find_max(num);
    a=maximum/10;
    b=maximum%10;
    printf("The winner couple is: %d blue, %d red.",a,b);
  return 0;
}


void update_array(int num[N][N])
{
char c;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{
    if (c>'0'&&c<='9')
    {
        digits++;
        c = c -'0';
        if (digits == 1)
            redIndex = c;
        else if (digits == 2)
            blueIndex = c;
        else
            continue;
    }
    else
    {
       if (digits == 2)
        {
            (num[redIndex][blueIndex])++;
        }
        else
          continue;
        digits = 0;
    }

}
}




int find_max(int b[N][N])
{
   int max = b[0][0];
   int x,y;
   int a=0,v=0,c=0;
   for (x = 0; x < N; x++)
   {
       for (y = 0; y < N; y++)
       {
           if (max < b[x][y])
           {
               max = b[x][y];
                a=x;
                v=y;
           }
       }
   }
    c=((a*10)+v);

   return c;
}



i还没有绑定任何东西,因为我还在想办法,这就是为什么我要求你的帮助!


i have not tied anything yet cause i am still thinking of a way, thats why i asked for you're help!

推荐答案

您可以使用两个数组,一个用于蓝色,一个用于红色,每个包含9个元素。



You could use two arrays, one for blue and one for red with 9 elements each.

int redVotes[9];
int blueVotes[9];



确保用0初始化两个数组。(提示:使用memset)



当您筛选出有效的输入值对时,将每个对分成一个高(蓝色)和一个低(红色)值。



[更新]

您选择的输入法有一个问题是您应该忽略3位或更多位数的数字。这要求您可以预测未来或具有某种回滚功能。



也许是这样的。请注意我在这个编辑器中编写了这段代码并且它根本没有经过测试。


Make sure to initialize both arrays with 0. (Hint: Use memset)

When you have filtered out the valid input value pairs, you split each pair into one high (blue) and one low (red) value.

[UPDATE]
One problem with the input approach you have chosen is that you are supposed to ignore numbers with 3 or more digits. This requires that you either can predict the future or have some sort of rollback functionality.

Maybe something like this. Just beware that I wrote this code here in this editor and it is not tested at all.

char c;
int digit = 0;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{
    if (isdigit(c))
    {
        digits++;
        digit = c -'0';  // Or use atoi(&c); to convert the character to an int
        if (digits == 1)
            redIndex = digit;
        else if (digits == 2)
            blueIndex = digit;
    }
    else
    {
        // This should make sure you only accept 2 digit numbers
        if (digits == 2)
        {
            redVotes[redIndex]++;
            blueVotes[blueIndex]++;
        }
        digits = 0;
    }
}





此部分不再需要



This part is not necessary anymore

int input = 35; // This is probably an array as well.
int redValue = 5;
int blueValue = 3;



(提示:使用模数表示红色值,整数除法表示蓝色值)



然后使用每个单独的值作为两者中的每一个的索引数组并增加该索引的值。


(Hint: Use modulus for the red value and integer division for the blue value)

Then you use the each indvidual value as an index in each of the two arrays and increment the value for that index.

redVotes[redValue]++;
blueVotes[blueValue]++;





那么这只是找到最大值的问题每个数组,组合值,你就有一个胜利者。

如果找不到其中一个数组的唯一最大值,结果是优柔寡断的。



所以,这应该让你开始。我没有为你解决所有问题,只是向你展示了许多方法。



Then it is just a matter of finding the max value of each array, combine the values and you have a winner.
If you cannot find a unique max value for one of the arrays, the result is indecisive.

So, that should get you started. I have not solved everything for you, just showed you one way of many to do it.


建议:

不要这样问问题。永远不要混淆不相关的问题。

- 你想要无效输入的帮助。

- 要么你想知道谁是胜利者的帮助。

混合两者只会让每个人感到困惑。



所有这一切都因为你的程序会反映出来!

你需要编程1部分阅读投票并过滤无效输入; a,另一部分只接收有效输入并与获胜者打交道。
Advice:
Don't ask questions this way. Never mix unrelated problems.
- either you want help with invalid inputs.
- either you want help with how to tell who is the winner.
Mixing both is just confusing for everyone.

All this because your program will reflect that!
You have to program 1 part that read votes and filter invalid inputs; a,d another part that will receive only valid inputs and deal with the winner.


这篇关于如何编写C程序找到获胜者的号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆