如果任何余数的总和是奇数,则添加 11 *除非*添加 11 个原因
If the sum of any of the remainders is odd then add 11 *unless* adding 11 causes
我遇到了一个新问题。该程序的目标是最终获得与用户开始时相同的 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
组合。现在,一次运行中只能执行一个打印语句。newQ
和newR
的块完全相同。
建议
正如评论中所建议的那样,尝试完全抓住if-else
的想法。运行一些基本代码以观察行为,并尝试通过单步执行代码在某些 IDE 中进行调试。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- Xapian - 搜索查询不检索匹配项,除非将""添加到每个单词
- 如果任何余数的总和是奇数,则添加 11 *除非*添加 11 个原因
- 应用程序一直接收分段错误-除非我添加一个for循环
- Qt5无法识别快捷方式,除非将操作添加到工具栏中