Home > Back-end >  Novice consult!!!!!!
Novice consult!!!!!!

Time:01-24

This is an issue when doing oj,
# include & lt; Stdio. H>
Int main () {
int a;
While (~ the scanf (" % d ", & amp; A)) {
Int b [a], c [a];
for(int i=0; iThe scanf (" % d ", & amp; B [I]);
}
for(int i=0; i[I] c=0;
}
for(int i=0; iFor (int j=I + 1; jIf [I]==b (b) [j] [I] c + +;
}
}
Int Max=c [0];
for(int i=1; iIf (c [I] & gt; Max) Max=c [I];
}
Printf (" % d ", Max + 1);
}
return 0;
}
This code can be compiled on my computer, and then submit it said compiler error, concrete is like this:
C
0 _0_35177313_6881.0 _0_35177313_6881. C (5) : error C2057: constant expression should be input
0 _0_35177313_6881. C (5) : error C2466: could not allocate constants of 0 to the size of the array
0 _0_35177313_6881. C (5) : error C2133: "b" : the size of the unknown
0 _0_35177313_6881. C (5) : error C2133: "c" : the size of the unknown,
I don't know why bosses, teach me appreciate

CodePudding user response:

C/c + + requires the size of the array must be a constant value, therefore the int b [a], c [a]; It is no good, if you want to use a dynamic array, can use malloc allocates memory
For example:
int a;
The scanf (" % d ", & amp; A);
Int b=malloc sizeof (int) * a); This form

CodePudding user response:

Dynamic array modifications as follows, for your reference:
 # include & lt; Stdio. H> 
Int main () {
int a;
While (~ the scanf (" % d ", & amp; A)) {
//int b [a], c [a];
Int * b=(int *) malloc sizeof (int) * a);
Int * c=(int *) malloc (sizeof (int) * a);

for(int i=0; iThe scanf (" % d ", & amp; B [I]);
}
for(int i=0; i[I] c=0;
}
for(int i=0; i<=a - 1; I++) {//for (int I=0; i
For (int j=I + 1; jIf [I]==b (b) [j] [I] c + +;
}
}
Int Max=c [0];
for(int i=1; iIf (c [I] & gt; Max) Max=c [I];
}
Printf (" % d ", Max + 1);
Free (b);
Free (c);
}

return 0;
}

CodePudding user response:

C99 version can, also can use the memory allocation functions malloc calloc
  • Related