将一个较短的字符串的字符交叉地插入到另一个较长的字符串中,每个字符以一定的间隔插入。该间隔=(长字符串长度/短字符串),即对两个字符串长度相除取整。例如:如果两个字符串等长(字符插入间隔=1),就将后输入的串的字符每隔一个字符插入先输入的串当中;被插入的字符串长度为12,待插入的字符串的长度为5,那么字符插入间隔为2,以此类推。请参考Sample Input和Sample Output。
Input
输入两个字符串(它们的长度不会超过100),两个字符串之间以空格间隔。
Output
输出一行字符串,即插入后的新串。
Sample Input
ABCDEFG abcdefg
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghi
Sample Output
AaBbCcDdEeFfGg
ABaCDbEFcGHdIJeKLfMNgOPhQRiSTUVWXYZ
Hint
注意:输入的字符串中第一个字符串长度不一定比第二个长。
#include <stdio.h>
#include <string.h>
int main()
{
char a[100],b[100],c[200];
int x,n,m;
int i,j;
scanf("%s%s",a,b);
if(strlen(a)/strlen(b)>=1)
{
x=strlen(a)/strlen(b);
}
else
{
x=strlen(b)/strlen(a);//判断间隔多少个字符
}
n=strlen(b)-1;
m=strlen(b)+strlen(a);
for(i=0,j=0; j<m; i++,j++)
{
if(i<=n)
{
c[x*i+x+i]=b[i];
}
if(j<=n*x+x+n)
{
if(i%x==0&&i!=0)
j++;
c[j]=a[i];
}
else
c[j]=a[i];
}//核心代码,就是插空按顺序排序
c[m]='\0';//插入终止符
printf("%s",c);
}