简体   繁体   English

确定n和被交换n的总和的所有数字是否为奇数

[英]Determine if all digits of the sum of n and swapped n are odd

I need to determine if all the digits of the sum of n numbers and swapped n are odd. 我需要确定n个数字之和与被交换n的所有数字是否都是奇数。

For example: 例如:

36 + 63 = 99 (9 and 9 are both odd) 36 + 63 = 99(9和9均为奇数)

409 + 904 = 1313 (1 and 3 are both odd) 409 + 904 = 1313(1和3均为奇数)

Visual Studio builds my code and it runs, but it doesn't return an answer. 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;
        }
    }
}

I'm not sure what's wrong with your code, but I was thinking an easier way to accomplish this would be to use strings, rather than doing all the divisions and mods by 10. 我不确定您的代码有什么问题,但是我在想一种更简单的方法来实现此目的,那就是使用字符串,而不是将所有的除法和除法运算都除以10。

  1. Convert original number to string, reverse the string, then convert that back to a long 将原始数字转换为字符串,反转字符串,然后将其转换回长整数
  2. Add the original and reversed numbers 添加原始数字和反向数字
  3. Convert the sum to a string 将总和转换为字符串
  4. Loop over the result string and check to see if each digit is odd 循环搜索结果字符串,并检查每个数字是否为奇数

It's not too clear what you mean by "Doesn't return an answer". 您不清楚“不返回答案”是什么意思。

Add: 加:

        Console.ReadKey();
        Console.ReadLine();

At the end of your Main function. 在主要功能的末尾。 I'd hazard a guess that you're not seeing an answer because the console is closing on you. 我可能会猜测您没有看到答案,因为控制台正在关闭。

EDIT: 编辑:

Found it: 找到了:

for (long i=0 ; i <= arreglo.Length ; i++)

Index out of bounds. 索引超出范围。 That should be: 应该是:

for (long i=0 ; i < arreglo.Length ; i++)

i should be "Less than" arreglo, not "Less than or equal to" 我应该是“小于” arreglo,而不是“小于或等于”

EDIT2: 编辑2:

This is why your current code is broken. 这就是为什么您当前的代码被破坏的原因。 I'd highly recommend also looking at alternative methods of solving the problem. 我强烈建议您也考虑解决问题的替代方法。 See Andy White's Answer. 参见安迪·怀特的答案。

It looks to me like you might have an infinite loop and a loop that never enters. 在我看来,您可能有一个无限循环和一个永远不会进入的循环。

// because v = 0, the while will never execute
long v = 0;

while (v > 0) 
{
    v = 10 * v + x % 10;
    x /= 10;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM