千里水草 2019-03-20 19:19 采纳率: 0%
浏览 548
已结题

字符串输出出现白色空格,感觉加了'\0'啊,求解答

图片说明

图片说明

题目如上,运行结果如上

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
#define maxn 100
int Maxcount=0;//记录最大容纳几张牌
char Laststr[20]={0};
int hashtable[maxn];

void count13(int hashtable[],char str[],int Length,double b[])
{
    if(b[0]==13&&Length>Maxcount)
    {
        Maxcount=Length;
        strcpy(Laststr,str);
    }

    else
    {
        for(int i=0;i<maxn;i++)
        {
            if(hashtable[i]!=0)
            {
                //分+-*/四种情况
                double temp=b[0];
                //+
                if(Length!=0)
                {
                str[Length*2-1]='+';
                str[Length*2]=i;
                b[0]=temp+i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }
                else
                {
                str[1]='+';
                str[1]=i;
                b[0]=temp+i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }


                //-
                if(Length!=0)
                {
                str[Length*2-1]='-';
                str[Length*2]=i;
                b[0]=temp-i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }
                else
                {
                str[1]='-';
                str[1]=i;
                b[0]=temp-i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }

                //*
                if(Length!=0)
                {
                str[Length*2-1]='*';
                str[Length*2]=i;
                b[0]=temp*i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }
                else
                {
                str[1]='*';
                str[1]=i;
                b[0]=temp*i;
                hashtable[i]--;
                count13(hashtable,str,Length+1,b);
                hashtable[i]++;
                }

                // /
                if(i!=0)
                {
                    if(Length!=0)
                    {
                    str[Length*2-1]='/';
                    str[Length*2]=i;
                    b[0]=temp/i;
                    hashtable[i]--;
                    count13(hashtable,str,Length+1,b);
                    hashtable[i]++;
                    }
                    else
                    {
                    str[1]='/';
                    str[1]=i;
                    b[0]=temp/i;
                    hashtable[i]--;
                    count13(hashtable,str,Length+1,b);
                    hashtable[i]++;
                    }
                }

            }
        }
    }
}

int main()
{
    int temp;//临时记录输入的数 ,要求的结果 
    double answer;
    fill(hashtable,hashtable+maxn,0);
    for(int i=0;i<6;i++)
    {
        scanf("%d",&temp);
        hashtable[temp]++;
     } 
    scanf("%d",&temp);//小明手上的牌
    scanf("%lf",&answer); 
    char str[20]={0};
    double b[maxn];
    b[0]=temp;
    str[0]=temp;
    count13(hashtable,str,0,b);
    for(int i=0;i<20;i++)
    {
        printf("%c",Laststr[i]);
    }
    cout<<Maxcount;
    return 0;
 } 

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-03-21 00:07
    关注
    for(int i=0;i<20;i++)
    {
        printf("%c",Laststr[i]);
    }
    

    你是按照字符输出的,和\0就没关系了。按照%s方式字符串输出,才会自动在\0截断

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误