1016 Numbers That Count 模拟

本文介绍了一种通过压缩数字串并对其进行分类的方法。利用C++实现了一个程序,该程序能够统计数字串中每个数字出现的次数,并将这些信息压缩成更短的形式。随后,根据数字串是否能自我压缩成相同形式、压缩几步后可以达到自我压缩状态或进入循环状态等条件进行分类。
#include <iostream>
#include <cstring>
using namespace std;


/*压缩数字串n,存放到t*/
void R(char* n,char* t)
{
    int i,j;
    int time[10]={0};  //记录n中各个数字出现的次数
    for (int i=0;n[i];i++)
        time[ n[i]-'0' ]++;


    for (i=0,j=0; i<10; i++)
    {
        if (time[i])
        {
            if (time[i]<10)  //数字i出现次数<10,即占1位
            {
                t[j++]=time[i]+'0';
                t[j++]=i+'0';
            }
            else    //数字i出现次数>=10,即占2位
            {
                t[j++]=time[i]/10+'0';
                t[j++]=time[i]%10+'0';
                t[j++]=i+'0';
            }
        }
    }
    t[j]='\0';


    return;
}


int main()
{
    char n[16][85];    //n[0]为原串,n[1~15]分别为n连续压缩15次的数字串


    while (cin>>n[0] && n[0][0]!='-')
    {
        bool flag1=false;    //属性1,n is self-inventorying
        int flag2=0;         //属性2,n is self-inventorying after j steps,顺便记录j
        int flag3=0;         //属性3,n is enters an inventory loop of length k,顺便记录k


        for (int i=1; i<=15; i++)
            R(n[i-1],n[i]);


        if (!strcmp(n[0],n[1]))  //属性1,n压缩1次就是其本身
            flag1=true;


        if (!flag1)
        {
            for (int j=1; j<15; j++)
                if (!strcmp(n[j],n[j+1]))  //属性2, n压缩j次后的数字串n[j]具有属性1
                {
                    flag2=j;
                    break;
                }


            if (!flag2)
            {
                for (int j=1; j<=15; j++)  //属性3,两两枚举各次压缩的数字串,注意循环间隔>=2
                {
                    for (int i=0;i<=j-2;i++)
                    {
                        if (!strcmp(n[j],n[i]))
                        {
                            flag3=j-i;
                            break;
                        }
                    }
                    if(flag3)
                        break;
                }
            }
        }


        if (flag1)
            cout<<n[0]<<" is self-inventorying"<<endl;
        else if (flag2)
            cout<<n[0]<<" is self-inventorying after "<<flag2<<" steps"<<endl;
        else if (flag3)
            cout<<n[0]<<" enters an inventory loop of length "<<flag3<<endl;
        else
            cout<<n[0]<<" can not be classified after 15 iterations"<<endl;
    }
    return 0;
}
请用c++回答,代码无注释,思考过程简短 7-3 Memories of Youth 分数 35 作者 陈越 单位 浙江大学 mem.jpg There was a group of 54 youths who were very good friends. Each one of them picked a card from a deck of cards to commemorate their friendship. But 54 years later, they cannot remember who has picked which card… Fortunately they have taken a lot of pictures at the time, and the card numbers have been recorded on the back of some pictures for some of them. Hopefully these clues are sufficient to recover the card numbers for everyone, even thought the correspondence between the person and the card was not recorded. Now your job is to help these friends recover the memories of their youth. Input Specification: Each input file contains one test case. For each case, the first line contains 2 positive integers M (≤54) and N (≤100), which are the number of friends (hence it is assumed that each of them corresponds to a distinct card number in the range [1, M]) and the number of pictures, respectively. Then N pictures are given. The information of each picture occupies 3 lines. The 1st line contains a positive integer k (<M) which is the number of persons in that picture. The 2nd line gives k distinct codes, each consists of 2 English capital letters, representing the names of the persons in that pictures. Their card numbers (k distinct integers in [1, M]) are given in the 3rd line. All the numbers in a line are separated by a space. Output Specification: For each test case, print the name code for each card in ascending order of the card numbers from 1 to M. Each code occupies a line. In case that it is impossible to determine the person who corresponds to a certain card, print ? in the line instead. It is guarenteed that there will not be any conflict conclusions. There might be cases such as persons A and B correspond to cards n 1 ​ and n 2 ​ , so without more information we cannot determine who corresponds to which card; yet there is no case so that a single card corresponds to more than one person. Sample Input: 8 5 2 AB CD 3 6 3 EF GH IJ 1 2 5 3 AB GH IJ 5 2 6 3 KL GH CD 4 2 3 2 UV XY 7 8 Sample Output: EF GH CD KL IJ AB ? ? 代码长度限制 16 KB Java (javac) 时间限制 800 ms 内存限制 64 MB 其他编译器 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值