【问题标题】:Program subtracting 1 from only even value output程序仅从偶数输出中减去 1
【发布时间】:2016-06-11 01:48:18
【问题描述】:

所以我不知道为什么会这样。这个程序要求用户输入一个整数,然后根据它是偶数还是奇数分别将数字加倍或加倍。问题在于,仅对于偶数循环,每次循环迭代都会将第一个数字减去 1。奇数的循环与偶数的循环完全相同,只是将数字翻了三倍而不是翻了一番,它工作得很好。有什么想法吗?

例如:

输入:12
预期输出:1122
实际输出:1121

输入:13
预期输出:111333
实际输出:111333

//This program asks the user to enter an integer and depending on whether the integer is even or odd, doubles or triples
//each digit in the integer respectively. It then reads out the result and asks the user if they would like to enter
//another integer and run the program again.
int main()
{
    string restart;
    int integer, remainder;

    while (restart != "n" && restart !="N")
    {
        cout << "Enter an integer: ";
        cin >> integer;
        cout << endl;

        //Creates variable "temp" and "mycount" and divides "integer" by 10 until remainder is 0 and counts number of
        //steps to do this to count the number of significant digits of the integer
        int temp = integer, mycount = 0;
        while (temp != 0)
        {
            temp = temp / 10;
            mycount++;
        }

        //Computes if the integer is even or odd by determining the remainder: 0 remainder = even, Non-0 remainder = odd
        //Creates two integer variables "exponent" and "sum" and sets them to 0
        //Assigns variable "temp" to the integer value
        remainder = integer % 2;
        int exponent = 0;
        int sum = 0;
        temp = integer;

        //If integer is even, doubles each significant digit
        if (remainder == 0)
        {
            //Begins for loop which runs for the number of times equal to the number of significant digits stored in "mycount"
            for (int i = mycount; i > 0; i--)
            {
                //Stores current significant digit in "digit"
                //Removes current significant digit by dividing by 10
                //Multiplies current significant digit by 11 and stores it in "timesTwo"
                //Multiplies current significant digit by 10^exponent then adds it to other modified digits
                //Adds 2 to current exponent to increase digit multiplier by 100
                int digit = temp % 10;
                temp = temp / 10;
                int timesTwo = digit * 11;
                sum = (timesTwo * pow(10, exponent)) + sum;
                exponent = exponent + 2;
                cout << sum << endl;
                cout << endl;
            }

            cout << "Number is even, doubling each digit in the integer ..." << endl;
            cout << sum << endl;
        }

        //If integer is odd this runs the same as the above function except it triples the digit and adds 3 to the multiplier
        else
        {
            for (int i = mycount; i > 0; i--)
            {
                int digit = temp % 10;
                temp = temp / 10;
                int timesThree = digit * 111;
                sum = (timesThree * pow(10, exponent)) + sum;
                exponent = exponent + 3;
                cout << sum << endl;
            }
            cout << "Number is odd, tripling each digit in the integer ..." << endl;
            cout << "Result: " << sum << endl;
        }

        cout << "Would you like to enter another integer? (y/n): ";
        cin >> restart;
    }

    return 0;
}

【问题讨论】:

  • 您是否使用调试器单步执行了代码,以便亲自查看数字是如何移动的?如果不是,为什么不呢?在将示例修复为minimal reproducible example 之后,它按我的预期工作。
  • 我没有看到你提到的错误发生。我建议您不要将int 用于sum。使用ulong 或仅用于显示目的,每次将值附加到字符串并显示它的反面。
  • 我尝试使用调试器并单步执行它,它显示一切正常,直到第一个循环运行后最后出现一个神奇的 -1。

标签: c++ if-statement for-loop while-loop int


【解决方案1】:

为了调试它,我会在第一个循环中打印所有输入,而不仅仅是输出。我能看到的唯一可疑之处是 pow() 返回一个浮点数,这可能会在转换回整数时被舍入。

为什么不完全避免 pow() 和隐式转换,而是使用一个整数因子来代替每一轮?

sum = (timesTwo * factor) + sum;
factor += 100;

顺便说一句:对于这两种情况,计算数字并使用不同的循环并不是真的必要——你可以将程序的核心简化为类似

bool even = (integer & 1) == 0;
int digitFactor = even ? 11 : 111;
int stepFactor = even ? 100 : 1000;
int result = 0;
while(integer != 0) {
  int digit = integer % 10;
  result += digit * digitFactor;
  integer /= 10;
  digitFactor *= stepFactor;
}

【讨论】:

  • 所以我对编码还是很陌生,不明白你的例子中的 digitFactor 和 stepFactor 行是怎么回事。你能解释一下发生了什么以及它们与上面的 bool 语句有什么关系吗?
  • digitFactor 是当前数字需要乘以“复制”或“三倍”并进入正确位置的因子。 stepFactor 是 digitFactor 在每一步都需要乘以以将下一个数字“移动”到正确位置的因子(它将 digitFactor 从 11 移动到 1100,然后移动到 110000 等)。打印 while 循环中的所有值可能会更清楚地详细说明发生了什么。
猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多