回 帖 发 新 帖 刷新版面

主题:第80次编程比赛(08第二学期开学第一炮)

原79次冠军因长时间不再发帖子,故本次由本人开展

[color=0000FF]题目描述:[/color]
现在,有两个正整数A和B,例如A是345,B是478,现在,需要把B插入到A里,
而A有三位,所以有四个位置选择,所得结果分别是:
478345, 347845, 344785, 345478
我们通过对比可以知道,在这当中最小的一个是344785
这两个正整数长度不超过100000位,各个位均不包含数字0
现在的目标是,要找出插入后所能得到的最小的整数,输出这个整数

[color=0000FF]样例输入:[/color]
345 478
12345 678
12 21
12 23

[color=0000FF]样例输出:[/color]
344785
12345678
1212
1223

[color=0000FF]答题要求:[/color]
请完善以下代码(C/C++均可):
[code=c]#include <stdio.h>
#define MAXLEN 100000

//todo: 在此增加你所需要的函数或者变量或者头文件

void deal(char* a, char* b, char* c)
{
    // todo: 在此补充你的处理代码
    // 参数说明: a和b是输入的字符串,c要保存输出结果
}

char a[MAXLEN+1], b[MAXLEN+1], c[MAXLEN*2+1];
int main(void)
{
    while (scanf("%s%s", a, b)!=EOF)
    {
        deal(a, b, c);
        puts(c);
    }
    return 0;
}[/code]

其它信息:
本次比赛以公开答题代码的形式,且附以及时的测试,允许直接在本帖子讨论算法
测试内容:正确性,时间效率,空间效率,代码可读性
本次比赛结束时间3月8日 23:00

[color=FF0000]在本帖子回复广告或比赛无关内容者立即封ID及IP[/color]

回复列表 (共182个回复)

91 楼

上面那个错了,可不可以再贴过阿?

#include <stdio.h>
#include <assert.h>
#include <string.h>
#define MAXLEN 100000

void deal(char* a,char* b,char* c)
{
    char *ptr_a,*ptr_b,*temp;
    int equal_before = 0;
    int len_a,len_b,minus;
    ptr_a = a;
    ptr_b = b;
    temp = ptr_a;

    assert(a!=NULL && b!=NULL && c!=NULL);
    if((*ptr_a == '\0')||(*ptr_b == '\0'))
        return;

    while( *temp != '\0')
    {
        if(*ptr_a < *ptr_b)
        {
            temp++;
            ptr_a = temp;
            ptr_b = b;
            equal_before = 0;
        }
        else if( *ptr_a > *ptr_b)
        {
            if(equal_before == 1)
                ptr_a = temp;
            break;
        }
        else
        {
            ptr_a++;
            ptr_b++;
            if( (*ptr_a == '\0')||(*ptr_b == '\0'))
            {
                if(*ptr_b !='\0')
                {
                    ptr_a = a;    
                }
                else
                {
                temp++;
                ptr_a = temp;
                ptr_b = b;
                equal_before = 0;
                }
            }
            else
            {
                equal_before = 1;
            }
        }
    }

    /* copy*/
    len_a = strlen(a);
    len_b = strlen(b);
    minus = ptr_a - a;
    if( minus==0)/*beg*/
    {
        strcpy(c,b);
        strcpy(c+len_b,a);
    }
    else if( minus== len_a)/*end*/
    {
        strcpy(c,a);
        strcpy(c+len_a,b);
    }
    else if( minus >0 && minus<(len_a-1))
    {
        strncpy(c,a, minus);
        strcpy(c+minus,b);
        strcpy(c+minus+len_b,a+minus);
    }
}

char a[MAXLEN+1],b[MAXLEN+1],c[MAXLEN+1];
int main(void)
{
    while(scanf("%s%s",a,b)!=EOF)
    {
        deal(a,b,c);
        puts(c);
    }
    return 0;
}

92 楼

你第二次帖的连题目上提供的数据的计算结果都错了啊

93 楼

我的妈呀!!
难道真的是我的方法的问题?

94 楼

#include <stdio.h>
#define MAXLEN 100000

//todo: 在此增加你所需要的函数或者变量或者头文件

void deal(char* a, char* b, char* c)
{
    // todo: 在此补充你的处理代码
    // 参数说明: a和b是输入的字符串,c要保存输出结果
    char*pa,*pb,*pc=c,*index;
    
    for(index=a,pb=b;*index&&*pb&&*index<=*pb;index++)
        if(*index==*pb)
        {
            if(*index>*(pb+1))break;
            if(*index==*(pb+1))
            {
                for(pb+=1;*pb==*index&&*pb;pb++);
                index--;
            }
        }

    for(pa=a;pa<index;pa++)
        *pc++=*pa;
    for(pb=b;*pb;pb++)
        *pc++=*pb;
    for(pa=index;*pa;pa++)
        *pc++=*pa;
    *pc=0;
    
}

char a[MAXLEN+1], b[MAXLEN+1], c[MAXLEN*2+1];
int main(void)
{
    while (scanf("%s%s", a, b)!=EOF)
    {
        deal(a, b, c);
        puts(c);
    }
    return 0;
}

95 楼

94楼的代码错在98 991这组数据

96 楼

#include <stdio.h>
#define MAXLEN 100000
#include<string.h>
void deal(char* a, char* b, char* c)
{
   int alen=strlen(a);
   int i;
   char s[MAXLEN*2+1]={'\0'};
    strcpy(c,a);
    strcat(c,b);
   for(i=0;i<alen;i++)
   { 
      strncpy(s,a,i+1);
      s[i+1]='\0';
      strcat(s,b);
      strcat(s,a+i+1);
    
      if(strcmp(s,c)<0) strcpy(c,s);

   }


}

char a[MAXLEN+1], b[MAXLEN+1], c[MAXLEN*2+1];
int main(void)
{
     while (scanf("%s%s", a, b)!=EOF)
    {
        deal(a, b, c);
        puts(c);
    }
    return 0;

}

97 楼

#include<string.h>
#include <stdio.h>
#define MAXLEN 100000

//todo: 在此增加你所需要的函数或者变量或者头文件




void deal(char* a, char* b, char* c)
{
    // todo: 在此补充你的处理代码
    // 参数说明: a和b是输入的字符串,c要保存输出结果
    char*pa,*pc=c,*pos;
    char temp[2*MAXLEN-1]={0};
    char *t,*index_t=temp;
    for(pos=b;*pos;pos++)
        *pc++=*pos;
    for(pos=a;*pos;pos++)
        *pc++=*pos;

    char *index_a=a;
    for(pa=a;*pa;pa++)
        if(*pa>=*b)
        {            
            for(pos=index_a,t=index_t;pos<pa;pos++)
                *t++=*pos;
            index_t=t;
            for(pos=b;*pos;pos++)
                *t++=*pos;
            for(pos=pa;*pos;pos++)
                *t++=*pos;
            *t=0;
            if(strcmp(c,temp)>0)
                strcpy(c,temp);
            index_a=pa;
    
        }
    strcpy(temp,a);
    strcat(temp,b);
    if(strcmp(c,temp)>0)
        strcpy(c,temp);
    
    
}

char a[MAXLEN+1], b[MAXLEN+1], c[MAXLEN*2+1];
int main(void)
{
    while (scanf("%s%s", a, b)!=EOF)
    {
        deal(a, b, c);
        puts(c);
    }
    return 0;
}

98 楼

96楼的试试 987654 9876542
97楼的代码效率过低

99 楼

 

100 楼

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
   char *p1,*p2,Integer1[100000],Integer2[100000],Temp[100000];
   int Lenth1,Lenth2;
   bool Flag=false;
   printf("Input Num1 and Num2\n");
   scanf("%s",Integer1);
   scanf("%s",Integer2);
   Lenth1=strlen(Integer1);
   Lenth2=strlen(Integer2);
   p2=Integer2;
   for(p1=Integer1;p1<Integer1+Lenth1;p1++)
   {
      if(*p2<*p1)
      {
         break;
      }
      else if(*p2==*p1)
      {
         for(int i=1;i<Lenth2;i++)
         {
            if(*(p2+i)<*(p1+i))
            {
               Flag=true;
               break;
            }
            else if(*(p2+i)>*(p1+i))
            {
               break;
            }
         }
         if(Flag==true)
         {
            break;
         }
      }
   }
   memcpy(Temp,Integer1,p1-Integer1);
   memcpy(Temp+(p1-Integer1),Integer2,Lenth2);
   memcpy(Temp+(p1-Integer1)+Lenth2,p1,Lenth1-(p1-Integer1));
   printf("%s",Temp);
   return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册