在运行题上的实例时无法输出最后一个“1”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string op1 = Console.ReadLine();
string op2 = Console.ReadLine();
string op3 = op1.Substring(0, op1.IndexOf(" "));
string op4 = op1.Substring(0, op2.IndexOf(" "));
string op5 = op1.Substring(op1.IndexOf(" ")+1);
string op6 = op2.Substring(op1.IndexOf(" ")+1);
int n1, n2,flag;
n1 = int.Parse(op3);
n2 = int.Parse(op4);
string[] a = op5.Split(' ');
string[] b = op6.Split(' ');
string[] c = new string[20];
int count = 0;
for (int j = 0; j < n1; j++)
{
flag = 0;
for ( int i = 0; i < n2; i++)
{
if (a[j] == b[i])
flag = 1;
}
if (flag == 0)
{
int h, flag1 = 0;
for (h = 0; h <=count; h++)
{
if (c[h] == a[j])
{
flag1 = 1;
}
}
if (flag1 == 0)
{
c[count] = a[j];
count++;
}
}
}
for (int j = 0; j < n2; j++)
{
flag = 0;
for (int i = 0; i < n1; i++)
{
if (b[j] == a[i])
flag = 1;
}
if (flag == 0)
{
int h, flag1 = 0;
for (h = 0; h <= count; h++)
{
if (c[h] == b[j])
{
flag1 = 1;
}
}
if (flag1 == 0)
{
c[count] = b[j];
count++;
}
}
}
Console.Write(c[0]);
for (int i = 1; i < count; i++)
{
Console.Write(" " + c[i]);
}
}
}
}
求大佬们指导。
已解决,改动代码如下。
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string str1 = Console.ReadLine();
string str2 = Console.ReadLine();
int a = str1.IndexOf(" ");
int b = str2.IndexOf(" ");
int c = Convert.ToInt32(str1.Substring(0, a));
int d = Convert.ToInt32(str2.Substring(0, b));
string str3 = str1.Substring(a + 1);
string str4 = str2.Substring(b + 1);
string[] str5 = str3.Split(' ');
string[] str6 = str4.Split(' ');
int[] x = new int[c];
int[] y = new int[d];
string t = "";
for (int i = 0; i < c; i++)
{
x[i] = Convert.ToInt32(str5[i]);
}
for (int o = 0; o < d; o++)
{
y[o] = Convert.ToInt32(str6[o]);
}
int z;
for (int m = 0; m < c; m++)
{
bool flag1 = true;
z = x[m];
foreach (int val in y)
{
if (val == z)
{
flag1 = false;
break;
}
else
continue;
}
if (flag1 == true)
t += z.ToString() + " ";
else
continue;
}
int h;
for (int n = 0; n < d; n++)
{
bool flag2 = true;
h = y[n];
foreach (int val in x)
{
if (val == h)
{
flag2 = false;
break;
}
else
continue;
}
if (flag2 == true)
t += h.ToString() + " ";
else
continue;
}
int e = t.Length;
string q = t.Substring(0, e - 1);
string[] qw = q.Split(' ');
int[] r = new int[qw.Length];
for (int u = 0; u < qw.Length; u++)
{
r[u] = Convert.ToInt32(qw[u]);
}
for (int sa = 0; sa < qw.Length; sa++)
{
for (int sb = sa+1; sb < qw.Length;sb++)
{
if (r[sa] == r[sb])
r[sb] = 10086;
}
}
string w = "";
for (int sc = 0; sc < qw.Length; sc++)
{
if (r[sc] != 10086)
{
w += (r[sc]).ToString() + " ";
}
}
int ew = w.Length;
string qwe = w.Substring(0, ew - 1);
Console.WriteLine(qwe);
}
}
}