c++如何使用模数
C++ how to use modulus
#include<iostream>
using namespace std;
int main()
{
int ID,year;
cout<<"Enter your ID";
cin>>ID;
year=
cout<<"Year="<<year;
system("pause");
}
cin
示例(20132724,20115555)
我的问题是我如何能使Year
采取前4个数字(从左)从ID
为什么要使用模数运算符?
你应该把你的int变成一个字符串,取前4个字符,然后把结果字符串变成一个int
int ID_only = ID % 10000; //ID only can be extracted like this
year = ID / 10000; //this will get integer result not fraction
假设:您的ID
将只有8位数字
像这样考虑模数——它返回除法的余数。在您的示例中,例如20132724。如果你把这个数除以10000,你会得到2013.2724对它取余就是周期之后的数。
我将以这种方式提取前四个数字-因为它已经是int型了。
#include<iostream>
int main()
{
int ID, year;
ID = 20132724;
year = ID / 10000;
std::cout << "Year = " << year; // since the type is int it
//wont print the fraction part
return 0;
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用新行和不使用新行读取文件
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 使用CMake创建QML插件
- 使用 LLVM 的 libc++ 时,__1 符号从何而来?
- 在std::cout之后使用std::cin时,换行符从何而来
- 为什么以及在何处在C++中使用引用和指针
- 基例如何影响使用递归函数的哪些行