int main(void) {
int no, flimit, i, pos_of_fval, freq_of_fval, n, j;
long long int max;
scanf("%d %d", &n, &flimit);
char list[n][21];
long long int fval[n];
char disp[flimit][21];
for (i = 0; i < n; i++)
scanf("%s %lld", list[i], &fval[i]);
for (i = 0; i < flimit; i++) {
max = 0;
for (j = 0; j < n; j++)
if (fval[j] > max) {
max = fval[j];
pos_of_fval = j;
}
freq_of_fval = freq(fval, max, n);
if (freq_of_fval == 1) {
strcpy(disp[i], list[pos_of_fval]);
fval[pos_of_fval] = 0;
} else
lexical(list, disp, fval, i, n, freq_of_fval, max);
}
for (i = 0; i < flimit; i++)
printf("%s\n", disp[i]);
return 0;
}
int freq(long long fval[], long long max, int n) {
int count = 0, i;
for (i = 0; i < n; i++)
if (fval[i] == max)
count++;
return count;
}
void lexical(char list[][21], char disp[][21], long long fval[],
intn dispos ,intn, int freq_of_fval, long long max) {
int a[freq_of_fval], i, j, apos = 0, temp;
for (j = 0; j < freq_of_fval; j++) {
for (i = 0; i < n; i++)
if (fval[i] == max)
a[apos++] = i;
}
for (i = 0; i < freq_of_fval - 1; i++) {
for (j = 0; j < freq_of_fval - 1 - i; j++) {
if (strcmp((list[a[j]]), list[a[j + 1]]) > 1) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
strcpy(disp[dispos], list[a[0]]);
}
错误:
warning: passing argument 1 of 'lexical' from incompatible pointer type
lexical(list,disp,fval,i,n,freq_of_fval,max);
note: expected 'char \*' but argument is of type 'char (\*)[21]'
void lexical(char list[],char disp[][21],long long fval[], int dispos, int
答案 0 :(得分:0)
您好,欢迎来到C编码的精彩世界 - 您需要在阵列长度上保持紧张的统治。
此代码存在以下问题 - 首先,您要声明大小未知的数组。例如在 -
char list[n][21];
这样的行只能在C99中工作 - 否则你不能声明一个可变大小的数组。 但即使在这种情况下你也可以这样做 - 在你的情况下,在你使用它之前从未设置过n。
通常,在C中 - 您必须选择: 1.你事先知道你的数组大小,然后在分配和函数签名中使用它
例如,如果你有20x21矩阵,那么比主代码:
char list[20][21]
然后在函数
中void lexical(char list[20][21],...)
或
void lexical(charlist[][],...)
:
//N is how many rows you have
char **list = malloc(sizeof (char *) * N);
if (list )
{
for (i = 0; i < N; i++)
{
a[i] = malloc(sizeof(char) * 21);
}
}
然后在函数中: void lexical(char ** list,...)
答案 1 :(得分:-1)
#include <stdio.h>
#include <string.h>
void lexical(char list[][21], char disp[][21], long long fval[],
int dispos ,int n, int freq_of_fval, long long max);
int freq(long long fval[], long long max, int n) ;
int main(void) {
int no, flimit, i, pos_of_fval, freq_of_fval, n, j;
long long int max;
printf("请输入n 和 flimit\n");
scanf("%d %d", &n, &flimit);
char list[n][21];
long long int fval[n];
char disp[flimit][21];
for (i = 0; i < n; i++) {
printf("字符串 和 long long数据\n");
scanf("%s %lld", list[i], &fval[i]);
}
for (i = 0; i < flimit; i++) {
max = 0;
for (j = 0; j < n; j++)
{
if (fval[j] > max) {
max = fval[j];
pos_of_fval = j;
}
}
freq_of_fval = freq(fval, max, n);
if (freq_of_fval == 1) {
strcpy(disp[i], list[pos_of_fval]);
fval[pos_of_fval] = 0;
} else {
lexical(list, disp, fval, i, n, freq_of_fval, max);
}
}
for (i = 0; i < flimit; i++)
printf("%s\n", disp[i]);
return 0;
}
// 计算数组中最大值有多少个(相同的最大值)
int freq(long long fval[], long long max, int n) {
int count = 0, i;
for (i = 0; i < n; i++)
if (fval[i] == max)
count++;
return count;
}
void lexical(char list[][21], char disp[][21], long long fval[],
int dispos ,int n, int freq_of_fval, long long max) {
int a[freq_of_fval], i, j, apos = 0, temp;
for (j = 0; j < freq_of_fval; j++) {
for (i = 0; i < n; i++)
if (fval[i] == max)
a[apos++] = i;
}
for (i = 0; i < freq_of_fval - 1; i++) {
for (j = 0; j < freq_of_fval - 1 - i; j++) {
if (strcmp((list[a[j]]), list[a[j + 1]]) > 1) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
strcpy(disp[dispos], list[a[0]]);
}