【问题标题】:I have a list of names and have to return the amount of each name in C我有一个名称列表,必须返回 C 中每个名称的数量
【发布时间】:2020-05-10 01:35:15
【问题描述】:

我有这个清单: [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]){

            }

【问题讨论】:

  • 只需使用while...
  • 我不明白如何做这样的列表 [[name1,count1],[name2,count2]]

标签: c arrays multidimensional-array


【解决方案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++;
    }
}

【讨论】:

  • 是的,也一样,但是如何创建这样的列表? [[something1,count_of_something1],[something2,count_of_something2]] 是 something1 和 something2 在列表中重复的事物 [something1,something2,something1,something2,something2]
  • @cde33 查看编辑,(但不要复制粘贴这段代码)
猜你喜欢
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多