2005年华中科技大学计算机保研机试真题

来源:互联网 发布:小米2s数据恢复2 编辑:程序博客网 时间:2024/04/28 04:16
#include<stdio.h>#include<string.h>struct location{    char c;     //字符    int n[100]; //该字符在数组中的位置    int count;  //该字符在数组中的个数};struct location loc[50];void Location(char *str);int main(){    char str[101];    while(scanf("%s",&str)!=EOF)    {        Location(str);    }    return 0;}void Location(char *str){    int len=strlen(str);    int i,j,h,count;    int k=0;    //需要输出的行数    int flag;   //检查是否重复    int len1;   //同一个字符的重复次数    char c;    for(i=0;i<len;i++)    {        c=str[i];        flag=0;        for(h=0;h<k;h++)        {            if(loc[h].c==c)            {                flag=1;                break;            }        }        if(flag==1)            continue;        count=0;        for(j=0;j<len;j++)        {            if(str[j]==c)            {                count++;                if(count>1)                    loc[k].n[count-1]=j;            }        }        if(count>1)        {            loc[k].n[0]=i;            loc[k].c=c;            loc[k].count=count;            k++;        }    }    //输出    printf("\n");    for(i=0;i<k;i++)    {        len1=loc[i].count;        for(j=0;j<len1-1;j++)            printf("%c:%d,",loc[i].c,loc[i].n[j]);        printf("%c:%d\n",loc[i].c,loc[i].n[len1-1]);    }    printf("\n");}/*************************Author:张女名Date:2017/3/9*************************/

题目描述:
对给定的一个字符串,找出有重复的字符,并给出其位置,如: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、相同的字母在一行表示出其出现过的位置。

来源:
2005年华中科技大学计算机保研机试真题

0 0