鱼C论坛

 找回密码
 立即注册
查看: 3310|回复: 3

[已解决]求大神帮我看看这个题目

[复制链接]
发表于 2015-12-1 19:31:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

using namespace std;
int main ()
{
        char source_code[300];
        int i,n=0,x=0,temp;
        char a[100];
        cout<<"please input the source code"<<endl;
        gets(source_code);
        for (i=0;source_code[i]!='\0';i++)
        cout<<source_code[i]<<" ";
        cout<<endl;
    for (i=0;source_code[i]!='\0';i++)
   {
           if (source_code[i]==0)
               n++;
           if (source_code[i]==1)
              {
              if (n==0)
                 {a[x]='a';
                 n=0;
                 x++;}
                 if (n==1)
                 {a[x]='b';
                  n=0;
                  x++;}
                      if (n==2)
                      {a[x]='c';
                      n=0;
                      x++;}
                      }
           }
           temp=x;
       for (x=0;x<temp;x++)
             cout<<a[x]<<" ";
        return 0;
        }
执行成功了,但是得不到正确的结果,不知道哪里出错了,求大神帮我看看
最佳答案
2015-12-1 22:15:52
QQ截图20151201221729.png

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {

  5.         char str[300];
  6.         printf("请输入要翻译的编码:\r\n");
  7.         scanf("%s",&str);
  8.         printf("翻译后的字符串为:\r\n");
  9.         for (int i=0;i<strlen(str);i++)
  10.         {
  11.                 if(str[i]=='0' && str[i+1]=='1')
  12.                 {
  13.                         printf("b");//如果当前字符是0并且下一个字符是1就被翻译成b
  14.                         i++;
  15.                 }
  16.                 else if (str[i]=='0' && str[i+1]=='0' && str[i+2]=='1')
  17.                 {
  18.                         printf("c");
  19.                         i+=2;
  20.                 }
  21.                 else if (str[i]=='1')
  22.                 {
  23.                         printf("a");
  24.                 }
  25.         }
  26.         printf("\n");
  27.         system("pause");
  28.         return 0;
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-1 22:15:52 | 显示全部楼层    本楼为最佳答案   
QQ截图20151201221729.png

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {

  5.         char str[300];
  6.         printf("请输入要翻译的编码:\r\n");
  7.         scanf("%s",&str);
  8.         printf("翻译后的字符串为:\r\n");
  9.         for (int i=0;i<strlen(str);i++)
  10.         {
  11.                 if(str[i]=='0' && str[i+1]=='1')
  12.                 {
  13.                         printf("b");//如果当前字符是0并且下一个字符是1就被翻译成b
  14.                         i++;
  15.                 }
  16.                 else if (str[i]=='0' && str[i+1]=='0' && str[i+2]=='1')
  17.                 {
  18.                         printf("c");
  19.                         i+=2;
  20.                 }
  21.                 else if (str[i]=='1')
  22.                 {
  23.                         printf("a");
  24.                 }
  25.         }
  26.         printf("\n");
  27.         system("pause");
  28.         return 0;
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-12-4 12:40:51 | 显示全部楼层
感谢二楼的解答!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-4 23:26:16 | 显示全部楼层

谢谢!!!!!!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 11:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表