问题信息
禁止使用逻辑运算符、关系运算符、布尔变量或选择构造
我试着这样创建数字:
int year_diff = grad_year - start_year;
int prog_years = year_diff % 100;
int uniq_id = student_id % 10000;
int id_num = start_year * 100000000 + program_num * 1000000 + prog_years * 10000 + uniq_id;
把这些数字像这样
int loc_diff = loc2 - loc1;
int div = pow(10, loc1 - 1);
int desired_digits = (id_num / div) % (int) pow(10, loc_diff + 1);
但是它不起作用,我真的很困难。有人能帮忙吗
2条答案
按热度按时间bqucvtff1#
=
的右侧使用足够宽的数学运算。OP的代码可能存在int
溢出的风险。int
对于12位十进制数字的数学运算来说肯定不够宽。long long
对于至少18位十进制数字来说是好的。long long
常量把数学运算变成long long
。pow()
。pow()
在转换为整数类型时会面临“相差1”或更多的风险。整数问题应避免浮点运算。unsigned
和unsigned long long
。vjhs03f72#
您需要添加一些参数检查(以查看它们是否有效)