如何求余数
How to find a remainder
本文关键字:何求余 更新时间:2023-10-16
我通过使用模(%
)运算符进行了尝试。但是每次我看到我的应用程序停止工作的消息。下面是我的代码:
using namespace std;
int main ()
{
int A;
int B;
int C;
C = A % B;
cout << "What is A";
cin >> A;
cout << "What is B";
cin >> B;
cout << A % B;
return 0;
}
int A;
int B;
int C;
C=A%B;
因此,您根据尚未设置的值A
和B
计算C。它们可以是任何东西,因此,它们实际上是什么是未定义的,当你计算A%B
时也是如此。可能B
恰好为0,这会在CPU中产生算术错误。
读取未初始化的变量是未定义的行为,例如您的A
, B
和C
。
您的变量B
没有初始化值,但编译器似乎很友好地将其设置为0,因此A%B
(内部)除零,这不是一个有效的数学操作,因此发生严重错误
欢迎来到c++ !:)
使用C,因为你正在计算它:
cout << C << endl; //outputs the computation of A % B
总之,这是一个编辑版本的代码片段。
#include <iostream> //used for cout and cin
using namespace std;
int main ()
{
int A; //A,B,C are initialized with no values
int B;
int C;
cout << "What is A";
cin >> A; //A is given a value
cout << "What is B";
cin >> B; //B is given a value
C = A % B; //previous place in the code is computing empty values. but placing this line of code AFTER values have been set, allows the program to compute.
cout << C;
return 0;
}
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- If语句未被求值C++
- Python中的for循环与C++有何不同
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 给定两个偶数,求出它们之间所有偶数的平方和
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 求最长递增子序列C++的长度
- 求最大元素位置的分治算法
- 线性丢番图方程 - 求给定区间内的解数和解
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 在C++中释放内存期间,迭代器与指针有何不同
- 特征稀疏向量:求最大系数
- 如何擦除冗余输入?
- 求所有整数 1 到 N 的最大奇数除数之和
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 类中的 C++ int 被设置为值,似乎不知从何而来
- 求最小素数 x 和最大 m = power_of(x) 的函数,使得 n % m = 0 和 n % x = 0?
- -fvisibility-inline-hidden 与 gcc 中的 -fvisibility=hidden 有何不同
- 如何使用 while 循环进行求幂
- C++ 和 Java 之间的表达式求值顺序有何不同