各路大神,求解:C#用键盘输入两个字符串数组,将他们合并然后排序
展开全部
class Program
{
public static void Main(String[] args)
{
Console.WriteLine("请输入两个字符串");
string a = Console.ReadLine();
string b = Console.ReadLine();
char []c =sort(a.ToCharArray());
char [] d=sort(b.ToCharArray());
foreach (char f in c)
Console.Write(f);
Console.WriteLine();
foreach (char g in d)
Console.Write(g);
Console.WriteLine();
char[] e = new char[c.Length+d.Length];
int j = 0, i = 0,n=0;
while (n < c.Length + d.Length)
{
if (d.Length == j)
{
e[n] = c[i];
n++;
i++;
continue;
}
if (c.Length == i)
{
e[n] = d[j];
n++;
j++;
continue;
}
if (c[i] < d[j])
{
e[n] = c[i];
n++;
i++;
}else if(c[i] >= d[j])
{
e[n] = d[j];
n++;
j++;
}
}
foreach (char f in e)
Console.Write("{0,-2}",f);
Console.ReadKey();
}
public static char [] sort(char [] a)
{
bool flag = true;
char temp;
for (int i = 0; i < a.Length-1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] >= a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = false;
}
}
if (flag)
break;
}
return a;
}
}
{
public static void Main(String[] args)
{
Console.WriteLine("请输入两个字符串");
string a = Console.ReadLine();
string b = Console.ReadLine();
char []c =sort(a.ToCharArray());
char [] d=sort(b.ToCharArray());
foreach (char f in c)
Console.Write(f);
Console.WriteLine();
foreach (char g in d)
Console.Write(g);
Console.WriteLine();
char[] e = new char[c.Length+d.Length];
int j = 0, i = 0,n=0;
while (n < c.Length + d.Length)
{
if (d.Length == j)
{
e[n] = c[i];
n++;
i++;
continue;
}
if (c.Length == i)
{
e[n] = d[j];
n++;
j++;
continue;
}
if (c[i] < d[j])
{
e[n] = c[i];
n++;
i++;
}else if(c[i] >= d[j])
{
e[n] = d[j];
n++;
j++;
}
}
foreach (char f in e)
Console.Write("{0,-2}",f);
Console.ReadKey();
}
public static char [] sort(char [] a)
{
bool flag = true;
char temp;
for (int i = 0; i < a.Length-1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] >= a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = false;
}
}
if (flag)
break;
}
return a;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询