我需要确定n个数和被交换n之和的所有数字是否都是奇数.
例如:
36 + 63 = 99(9和9都是奇数)
409 + 904 = 1313(1和3都是奇数)
Visual Studio构建我的代码并运行,但它不会返回答案.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
long num = Convert.ToInt64(Console.Read());
long vol = voltea(num);
long sum = num + vol;
bool simp = simpares(sum);
if (simp == true)
Console.Write("Si");
else
Console.Write("No");
}
static private bool simpares(long x)
{
bool s = false;
long [] arreglo = new long [1000];
while ( x > 0)
{
arreglo [x % 10] ++;
x /=10;
}
for (long i=0 ; i <= arreglo.Length ; i++)
{
if (arreglo [i]%2 != 0)
s = true;
}
return s;
}
static private long voltea(long x)
{
long v = 0;
while (v > 0)
{
v = 10 * v + x % 10;
x /= 10;
}
return v;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定你的代码有什么问题,但我认为更简单的方法就是使用字符串,而不是用10来完成所有的分区和mod.