题目链接:http://ac.jobdu.com/problem.php?pid=1199
题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。 输入: 输入包括一个由字母和数字组成的字符串,其长度不超过100。 输出: 可能有多组测试数据,对于每组数据, 按照样例输出的格式将字符出现的位置标出。 样例输入: abcaaAB12ab12样例输出: a:0,a:3,a:4,a:9 b:1,b:10 1:7,1:11 2:8,2:12提示: 1、下标从0开始。 2、相同的字母在一行表示出其出现过的位置。
简单题,AC
//题目1199:找位置 //AC #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char s[101]; int visit[101]= {0}; int i,j,len,flag; while((scanf("%s",s))!=EOF) { len = strlen(s);//len字符串的长度 for(i=0; i<101; i++) { visit[i]=0;//初始化 } for(i=0; i<len; i++) { if(visit[i]==0)//没有访问过 { flag=0;//flag=1表示后面还有该字符并且不是第一个遇到 for(j=i+1; j<len; j++)//遍历后面的字符 { if(s[i]==s[j]) { if(flag==1) { printf(",%c:%d",s[j],j); visit[j]=1; } else { printf("%c:%d,%c:%d",s[i],i,s[j],j);//遇到后面第一个相同字符 visit[i]=1; visit[j]=1; flag=1; } } } if(flag==1) printf("\n");//有重复字符的才要打印,没有的不打印 } } } return 0; }