poj1016 Numbers That Count

来源:互联网 发布:淘宝 童鞋 童装 编辑:程序博客网 时间:2024/04/28 01:36

     这是一道简单的模拟题,统计字符串中1,2....9的个数,同时组成一个新的字符串,并和最初的比较,有四种情况~~

    具体代码如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>char word[100],str[20][100],tempstr[100];int count[10];int index;void change(int x){    memset(count,0,sizeof(count));    int len,i,j;    len = strlen(str[x]);    for(i = 0;i < len;i++)       count[str[x][i]-'0']++;    index = 0;    for(i = 0;i < 10;i++){        if(count[i]){            if(count[i] > 9){                tempstr[index++] = count[i]/10 + '0';                tempstr[index++] = count[i]%10 + '0';            }else{                tempstr[index++] = count[i] + '0';            }            tempstr[index++] = i + '0';        }    }    tempstr[index] = 0;}int main(){    int i,j;    while(gets(str[0])){        if(str[0][0]=='-') break;        for(i = 1;i <= 15;i++){            change(i-1);        for(j = i-1;j>=0;j--){            if(!memcmp(tempstr,str[j],strlen(str[j])>strlen(tempstr)?strlen(str[j]):strlen(tempstr)))            {                if(i == 1) {                   printf("%s is self-inventorying\n",str[0]);                }                else if(j == i-1) {                        printf("%s is self-inventorying after %d steps \n",str[0],i-1);                        }                else {                    printf("%s enters an inventory loop of length %d\n",str[0],i - j);                    }                goto mark;            }        }           strcpy(str[i],tempstr);        }        printf("%s can not be classified after 15 iterations\n",str[0]);        mark:;    }    return 0;}


原创粉丝点击