题解 | #找位置#

找位置

https://www.nowcoder.com/practice/e3b2cc44aa9b4851bdca89dd79c53150

#include <stdio.h>
#include <string.h>
int main() {
    char a[101];
    while (scanf("%s", a) != EOF) { 
        
        int len=0;
        int i=strlen(a);
        char b[101]={'0'};//value index
        int b1[101]={0};
        int n=0;
        for(int j=0;j<i;j++){
            int tag=0;
            for(int ii=0;ii<n;ii++){
                if(b[ii]==a[j]){
                    b1[ii]++;
                    tag=1;
                    break;
                }
            }
            if(tag==0){
                b[n]=a[j];b1[n]+=1;n++;
            }
        }
        for(int j=0;j<n;j++){
            if(b1[j]>1){
                int temp=b1[j];
                int te[temp];
                int length=0;
                for(int jj=0;jj<i;jj++){
                    if(a[jj]==b[j]){
                        te[length++]=jj;
                    }
                }
                for(int jj=0;jj<temp;jj++){
                    if(jj!=temp-1){printf("%c:%d,",b[j],te[jj]);}
                    else{
                        printf("%c:%d\n",b[j],te[jj]);
                    }
                }
            }
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务