如果任何余数的总和是奇数,则添加 11 *除非*添加 11 个原因

If the sum of any of the remainders is odd then add 11 *unless* adding 11 causes

本文关键字:添加 除非 任何余 如果      更新时间:2023-10-16

我遇到了一个新问题。该程序的目标是最终获得与用户开始时相同的 A、B、C 值。我的代码几乎适用于每个 3 位整数,除了少数,例如 984 和 985。"A 和 B 的新值"是 9 和 8 的倍数,例如 3 和 2,而不是 9 和 8(应该如此(。

我已经评论了问题开始的地方。这是我的新尝试,也是我对代码的第一次尝试。提前谢谢。

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    //declare each variable
    int A,B,C;
    int ABC, BCA, CAB;
    int X,Y,Z; //remainders stored
    int P,Q,R; //sums of remainders
    //welcome
    cout<< "Welcome to Acelia's 3 digit reader n"
        <<"n";
    //prompt user
    cout << "Enter a number between 100 and 999: "; 
        cin >> ABC;
    //fits in range
    while ( (ABC < 100) || (ABC > 999) ) {
        cout << "Enter a valid number between 100 and 999: ";
        cin >> ABC;
    }
    cout << "Cool, you entered " << ABC 
         <<".n"
         <<"nIn the form of ABC...n";
    //strip each digit of the number
    A = (ABC / 100);
    B = ((ABC/10) % 10);
    C = (ABC % 10);

    BCA = (B * 100 + C * 10 +A); //hundreds, tens, ones
    CAB = (C * 100 + A * 10 +B);
    cout <<"A is "<< A<< "nB is " << B<<"nC is " << C <<"n"; //print individual #
    //print ABC, BCA, CAB
    cout <<"nYour number in the form ABC is " << ABC
         <<"nYour number in the form BCA is " << BCA
         <<"nYour number in the form CAB is " << CAB
         << "nn";
    //store remainder of each value when divided by 11
    X = (ABC % 11); cout<< "The remainder of " << ABC <<" divided by 11 is " << X;
    Y = (BCA % 11); cout<< "nThe remainder of " << BCA <<" divided by 11 is " << Y;
    Z = (CAB % 11); cout<< "nThe remainder of " << CAB <<" divided by 11 is " << Z << "nn";
    //sums of each remainder
    P = (X + Y); cout<< "The sum of remainders from ABC and BCA is: " << P;
    Q = (Y + Z); cout<< "nThe sum of remainders from BCA and CAB is: " << Q;
    R = (Z + X); cout<< "nThe sum of remainders from CAB and ABC is: " << R <<"nn";
    int newP=0; //it would not execute properly w/o being initialized to 0
    int newQ=0;
    int newR=0;
    // Check sum remainder of X & Y 
    /*!!!!!!PROBLEM IS HERE!!!!!*/
    if (P % 2 == 1){
        if(P + 11 > 20) {
            newP = (P-11);
        }
        else {
            newP = (P+11);
        }
    }
    newP = (P/2);
    cout << "nNew value of A is: " << newP; 
    //check sum remainder of Y & Z 
    /*ORIGNAL CODE */
    if ((Q % 2 == 1) && ((Q+11) > 20)){
            newQ = (Q-11);
            newQ = (newQ/2);
            cout<< ("nNew value of B is: ") << newQ << "n";
            if ((Q % 2 == 1) && ((Q + 11) < 20)) {
                newQ = (Q+11);
                newQ = (newQ /2);
                cout<< ("nNew value of B is: ") << newQ << "n";
            }
        }
        else {
            newQ = (Q/2);
            cout << "nNew value of B is: " << newQ; 
        }

    //check sum remainder of Z + X
    if ((R % 2 == 1) && ((R+11) > 20)){
            newR = (R-11);
            newR = (newR/2);
            cout<< ("nNew value of C is: ") << newR << "n";
            if ((R % 2 == 1) && ((R + 11) < 20)) {
                newR = (R+11);
                newR = (newR /2);
                cout<< ("nNew value of C is: ") << newR << "n";
            }
        }
        else {
            newR = (R/2);
            cout << "nNew value of C is: " << newR; 
        }
}//end of main

我们通常不会在这里提供完整的答案。既然你已经付出了努力,我就给你完整的解决方案。

溶液

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    //declare each variable
    int A, B, C;
    int ABC, BCA, CAB;
    int X, Y, Z; //remainders stored
    int P, Q, R; //sums of remainders
    //welcome
    cout << "Welcome to Acelia's 3 digit reader n" << "n";
    //prompt user
    cout << "Enter a number between 100 and 999: ";
    cin >> ABC;
    //fits in range
    while ((ABC < 100) || (ABC > 999)) {
        cout << "Enter a valid number between 100 and 999: ";
        cin >> ABC;
    }
    cout << "Cool, you entered " << ABC << ".n" << "nIn the form of ABC...n";
    //strip each digit of the number
    A = (ABC / 100);
    B = ((ABC / 10) % 10);
    C = (ABC % 10);
    BCA = (B * 100 + C * 10 + A); //hundreds, tens, ones
    CAB = (C * 100 + A * 10 + B);
    cout << "A is " << A << "nB is " << B << "nC is " << C << "n"; //print individual #
    //print ABC, BCA, CAB
    cout << "nYour number in the form ABC is " << ABC
            << "nYour number in the form BCA is " << BCA
            << "nYour number in the form CAB is " << CAB << "nn";
    //store remainder of each value when divided by 11
    X = (ABC % 11);
    cout << "The remainder of " << ABC << " divided by 11 is " << X;
    Y = (BCA % 11);
    cout << "nThe remainder of " << BCA << " divided by 11 is " << Y;
    Z = (CAB % 11);
    cout << "nThe remainder of " << CAB << " divided by 11 is " << Z << "nn";
    //sums of each remainder
    P = (X + Y);
    cout << "The sum of remainders from ABC and BCA is: " << P;
    Q = (Y + Z);
    cout << "nThe sum of remainders from BCA and CAB is: " << Q;
    R = (Z + X);
    cout << "nThe sum of remainders from CAB and ABC is: " << R << "nn";
    int newP = 0; //it would not execute properly w/o being initialized to 0
    int newQ = 0;
    int newR = 0;
    // Check sum remainder of X & Y
    /*!!!!!!PROBLEM IS HERE!!!!!*/
    if (P % 2 == 1) {
        if (P + 11 > 20) {
            newP = (P - 11);
            cout << "nNew value of A is: " << newP;
        } else {
            newP = (P + 11);
            newP = (newP / 2);
            cout << "nNew value of A is: " << newP;
        }
    } else {
        newP = (P / 2);
        cout << "nNew value of A is: " << newP;
    }
    //check sum remainder of Y & Z
    /*ORIGNAL CODE */
    if (Q % 2 == 1) {
        if ((Q + 11) > 20) {
            newQ = (Q - 11);
            newQ = (newQ / 2);
            cout << ("nNew value of B is: ") << newQ << "n";
        } else {
            newQ = (Q + 11);
            newQ = (newQ / 2);
            cout << ("nNew value of B is: ") << newQ << "n";
        }
    } else {
        newQ = (Q / 2);
        cout << "nNew value of B is: " << newQ;
    }
    //check sum remainder of Z + X
    if ((R % 2 == 1) && ((R + 11) > 20)) {
        if ((R + 11) > 20) {
            newR = (R - 11);
            newR = (newR / 2);
            cout << ("nNew value of C is: ") << newR << "n";
        } else {
            newR = (R + 11);
            newR = (newR / 2);
            cout << ("nNew value of C is: ") << newR << "n";
        }
    } else {
        newR = (R / 2);
        cout << "nNew value of C is: " << newR;
    }
    return 0;
} //end of main

问题

在此代码块中

if (P % 2 == 1){
    if(P + 11 > 20) {
        newP = (P-11);
    }
    else {
        newP = (P+11);
    }
}
newP = (P/2);       //always executes regardless of the previous
                    //changes to the newP 
cout << "nNew value of A is: " << newP;

无论if块做什么,newP都将始终P/2因为无论上述条件如何,它都将始终执行。我把它改成了

if (P % 2 == 1) {
    if (P + 11 > 20) {
        newP = (P - 11);
        cout << "nNew value of A is: " << newP;
    } else {
        newP = (P + 11);
        newP = (newP / 2);
        cout << "nNew value of A is: " << newP;
    }
} else {
    newP = (P / 2);
    cout << "nNew value of A is: " << newP;
}

在这里,我们通过整合所有可能性来获得详尽的if-else组合。现在,一次运行中只能执行一个打印语句。newQnewR的块完全相同。

建议

正如评论中所建议的那样,尝试完全抓住if-else的想法。运行一些基本代码以观察行为,并尝试通过单步执行代码在某些 IDE 中进行调试。