0

我有这个列表: [michelle,michelle,sara,jorge,sara,marie] 我需要返回一个这样的列表, [[michelle,2],[sara,2],[jorge,1],[marie,1]] 我想用两个做一些事情,for();但不知道该怎么做。有人可以指导我吗?提前致谢。

for(int i=0;names[i]!=NULL;i++){
        for (int j=0;names[j]!=NULL;j++){
            if(names[i]==names[j]){

            }
4

1 回答 1

-1
int i,j;
i=0;
while (name[i++]!=NULL){
    j=0;
    while (name[j++]!=NULL){
        /* whatever */
    }
}

会做与 for 循环相同的事情。事实上,一个人可以做到

int i=0,j,ptr=0,flag;
char strlist[NUMBER_OF_STRING][LENGTH_OF_STRING];
int count[NUMBER_OF_STRING];
while (name[i++]!=NULL){
    j=flag=0;
    while (j++<ptr){
        if (strcmp(name[i],strlist[j])){
            flag=1;
            count[j]++;
            break;
        }
    }
    if (!flag){
        strcpy(strlist[ptr],name[i]);
        count[ptr]=1;
        ptr++;
    }
}
于 2020-05-10T01:41:03.500 回答