我有这个代码,输入是随机的,例如:
33 32 65 55 44 pe *&44^ %123^
现在我需要找到最重复的两位数字(并且该数字需要包含1到9的数字,这意味着10,30..
不是有效数字),现在在两位数字之间分隔到另一位数字每个输入都是另一个数字,
在这种情况下,我希望输出为44然后我将打印:
The winner couple is: 4 blue, 4 red.
现在我认为做这样的事情的最好方法是使用2D数组,当输入有效时,我将在sutble位置使用数组,之后我将找到数组中的最大位置。 这是我目前的代码:
#include <stdio.h>
#include <stdlib.h>
#define N 9
int is_digit(char c);
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, 4%d red.",a,b);
return 0;
}
int is_digit(char c)
{
if(c<'0'&&c>'9')
return 1;
else
return 0;
}
void update_array(int num[N][N])
{
char c;
int digit = 0;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{
if (is_digit(c))
{
digits++;
digit = c -'0';
if (digits == 1)
redIndex = digit;
else if (digits == 2)
blueIndex = digit;
}
else
{
if (digits == 2)
{
(num[redIndex][blueIndex]++);
}
digits = 0;
}
}
int find_max(int b[N][N])
{
int max = b[0][0];
int x,y;
for (x = 0; x < N; x++)
{
for (y = 0; y < N; y++)
{
if (max < b[x][y])
{
max = b[x][y];
}
}
}
}
return max;
}
代码给了我incorect输出,我把所有的函数都搞定了,并且它们很好,exept for update_array函数我认为有一些不正确的东西,基本上这个函数小鸡哪里有两位数字并更新数组中的suatble位置,所以我可以使用fin_max函数找到最大值.. plz anyhelp为什么它给了我incorect值,我知道我应该使用debugg但我从来不知道这个,直到现在我没有时间来学习这个并使用它因为我需要在几个小时内提交!
答案 0 :(得分:0)
在您的代码中,您执行if (isdigit(c))
,但isdigit
是一个不同的(标准库)功能!您必须#include <ctype.h>
才能使用它。
答案 1 :(得分:0)
continue;
外部循环是非法的。w
。w
之后,分号丢失了。请尝试此操作,因为未使用is_digit
:
void is_digit(char c)
{
(void)c; /* avoid warning for unused variable */
}
请注意,这是invaiid,因为返回类型为void
:
void is_digit(char c)
{
return (c>'0'&&c<='9');
}
请勿忘记添加#include <ctype.h>
以使用isdigit()
。
另外,替换
if (isdigit(c))
与
if (isdigit((unsigned char)c) && c != '0')
避免未定义的行为以传递超出范围的值并拒绝数字0
。
答案 2 :(得分:-1)
您已使用名称is_digit()
声明了该函数,之后您使用名称为isdigit()
的函数。
因此,在更改上述错误后,您的代码看起来像
#include <stdio.h>
#include <stdlib.h>
#define N 9
int is_digit(char c);
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, 4%d red.",a,b);
return 0;
}
int is_digit(char c)
{
if(c<'0'&&c>'9')
return 1;
else if(c>'0'&&c<='9')
return 0;
}
void update_array(int num[N][N])
{
char c;
int digit = 0;
int digits = 0;
int redIndex = 0;
int blueIndex = 0;
while (scanf("%c", &c) != EOF)
{ // <= while started
if (is_digit(c))
{ // <= if started
digits++;
digit = c -'0';
if (digits == 1)
redIndex = digit;
else if (digits == 2)
blueIndex = digit;
} // <= If ended
else
{ // <= else started
if (digits == 2)
{
(num[redIndex][blueIndex]++);
}
digits = 0;
} // <= else ended
} // <= while ended
//where is the function ending?
} //hence another curly braces needed.
int find_max(int b[N][N])
{
int max = b[0][0];
int x,y;
for (x = 0; x < N; x++)
{
for (y = 0; y < N; y++)
{
if (max < b[x][y])
{
max = b[x][y];
}
}
}
return max;
}