金桔希子哦 发表于 2021-11-12 14:46:29

解决问题急急急

求助,这个怎么做
先输入一个数值来确定之后要输入数值的位数,再分别输入三个相同位数的数值,要求在第一个数值的第一位后面插入第二,三个数值的第一位,在第一数值的第二位后面插入第二,三个数值的第二位,以此类推,形成一个数值并输出{:5_92:}

jhq999 发表于 2021-11-13 07:20:39

本帖最后由 jhq999 于 2021-11-13 08:00 编辑

int main ()
{
       
        int i=0,j=0,m=0,n=0;
      scanf("%d %d",&m,&n);
        char **a=new char*;
        char *b=new char,*b1=new char;
        for (i = 0; i < m*(n+1); i++)
        {
                b=0;
        }
        for (i = 0; i < m; i++)
        {
               
                        a=b+i*(n+1);
               
        }
        i=0;
      while(i<m)scanf("%s",a);
        for (i = 0; i < n; i++)
        {
                for (int j = 0; j < m; j++)
                {
                        b1=a;
                }
               
        }
        b1='\0';
        printf("%s",b1);

        delete[] b;
        delete[] b1;
        delete[] a;
        return 0;
}
//结果
4 4
1111 2222 3333 4444
1234123412341234

@littlefool 发表于 2021-11-13 08:33:42

好难理解

zy8818 发表于 2021-11-13 11:27:34

你这在讲绕口令吗?楼主你不能举个例子
是这样吗
1.确定输入位数:3
2.输入三个数 123, 456, 789
然后输出147258369吗?
这样的例子不好理解些吗?不知道我理解你的话的意思对不对

jhq999 发表于 2021-11-13 11:30:37

本帖最后由 jhq999 于 2021-11-13 11:33 编辑

zy8818 发表于 2021-11-13 11:27
你这在讲绕口令吗?楼主你不能举个例子
是这样吗
1.确定输入位数:3

//应该是输入3 3,3个数,每个数3位

zy8818 发表于 2021-11-13 15:38:28

本帖最后由 zy8818 于 2021-11-13 19:30 编辑

//参数int版本
//getAllnumber(几位数,第一个数, 第二个数, 第三个数)
char* getAllnumber(int n, int a, int b, int c)
{
        char *pAll = new char;//总和数
        memset(pAll, 0, n * 3 + 1);//pAll内存空间值全部清零
        char*err = "error";
        if (n<0 || n>9)//32位系统最大10位整数
        {
                goto error;
        }
        int temp = 1;//取模基数
        int j = 0,n2=n;
        while (n2--)
        {
                temp *= 10;//获取n位数取模基数
        }
        a %= temp;//截断n位数以上的数
        b %= temp;
        c %= temp;
        temp /= 10;
        if (a / temp > 0 && b / temp > 0 && c / temp > 0)//保证三个数都是n位数
        {
                char *pa = new char;
                char *pb = new char;
                char *pc = new char;
                itoa(a, pa, 10);//整数转字符串
                itoa(b, pb, 10);
                itoa(c, pc, 10);
                for (int i = 0; i < n; i++)//整合三个字符串
                {
                        //此处代码论坛显示有问题 我用小括号代替中括号
                        pAll = pa(i);
                        pAll = pb(i);
                        pAll = pc(i);
                }
                delete[]pa;
                delete[]pb;
                delete[]pc;
                return pAll;
        }
        else
        {
error:
                for (int i = 0; i < 5; i++)
                {
                        pAll(i) = err(i);//此处代码论坛显示有问题 我用小括号代替中括号
                }
                return pAll;//传入的数位数不正确返回错误
        }
}

//int argc参数个数,char* argv[]命令字符串
int main(int argc,char* argv[])
{
        int n = 4,a=1111,b=2222,c=3333;

        getAllnumber(n, a, b, c);//此处的参数n a b c变量值替换成键盘输入值即可
        char*pAll = getAllnumber(n, a, b, c);
        printf("%s\n",pAll);
        delete[]pAll;
        system("pause");
        return 0;
}

金桔希子哦 发表于 2021-11-13 16:24:37

zy8818 发表于 2021-11-13 11:27
你这在讲绕口令吗?楼主你不能举个例子
是这样吗
1.确定输入位数:3


是的是这样的

zy8818 发表于 2021-11-13 18:02:14

我想问下楼主jhq999发的代码没有注释你看懂了没,没有看懂问我,那个代码没有注释一般很难理解,你看下我写的代码,注释齐全

金桔希子哦 发表于 2021-11-16 12:59:20

zy8818 发表于 2021-11-13 18:02
我想问下楼主jhq999发的代码没有注释你看懂了没,没有看懂问我,那个代码没有注释一般很难理解,你看下我 ...

好的,懂啦谢谢
页: [1]
查看完整版本: 解决问题急急急