我有新的问题了。这个程序的目标是最终得到与用户开始时相同的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
<< "\n\n";
//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 << "\n\n";
//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 <<"\n\n";
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发布于 2017-02-15 15:29:16
我们通常不会在这里提供完整的答案。既然你已经展示了你的努力,我将给你一个完整的解决方案。
解决方案
#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 << "\n\n";
//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 << "\n\n";
//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 << "\n\n";
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组合。现在,在一次运行中只能执行一条print语句。newQ和newR的代码块完全相同。
建议
正如评论中所建议的,尝试完全抓住if-else的概念。运行一些基本代码来观察行为,并尝试通过单步执行代码在某些IDE中进行调试。
https://stackoverflow.com/questions/42198551
复制相似问题