石焕楷 发表于 2015-12-1 19:31:14

求大神帮我看看这个题目

.给定一个前缀码如下:
1 01 001
a b c
输入一个长度不少于16的01字符串,输出该字符串的译码。
例:
输入:001011101001011001
输出:cbaabcbac

using namespace std;
int main ()
{
        char source_code;
        int i,n=0,x=0,temp;
        char a;
        cout<<"please input the source code"<<endl;
        gets(source_code);
        for (i=0;source_code!='\0';i++)
        cout<<source_code<<" ";
        cout<<endl;
    for (i=0;source_code!='\0';i++)
   {
           if (source_code==0)
             n++;
           if (source_code==1)
              {
              if (n==0)
                 {a='a';
                 n=0;
                 x++;}
                 if (n==1)
                 {a='b';
                n=0;
                x++;}
                      if (n==2)
                      {a='c';
                      n=0;
                      x++;}
                      }
           }
           temp=x;
       for (x=0;x<temp;x++)
             cout<<a<<" ";
        return 0;
        }
执行成功了,但是得不到正确的结果,不知道哪里出错了,求大神帮我看看

machimilk 发表于 2015-12-1 22:15:52



#include <iostream>
using namespace std;
int main()
{

        char str;
        printf("请输入要翻译的编码:\r\n");
        scanf("%s",&str);
        printf("翻译后的字符串为:\r\n");
        for (int i=0;i<strlen(str);i++)
        {
                if(str=='0' && str=='1')
                {
                        printf("b");//如果当前字符是0并且下一个字符是1就被翻译成b
                        i++;
                }
                else if (str=='0' && str=='0' && str=='1')
                {
                        printf("c");
                        i+=2;
                }
                else if (str=='1')
                {
                        printf("a");
                }
        }
        printf("\n");
        system("pause");
        return 0;
}

淫令天下 发表于 2015-12-4 12:40:51

感谢二楼的解答!

石焕楷 发表于 2015-12-4 23:26:16

machimilk 发表于 2015-12-1 22:15


谢谢!!!!!!!!!!
页: [1]
查看完整版本: 求大神帮我看看这个题目