我正在使用C语言处理一个问题。问题在于确定数字n是否可被15整除"。 n
的范围是[1到10 1000 ]。 C中没有可以满足需要的数据类型,所以我该怎么办?
答案 0 :(得分:5)
是的,没有内置的数据类型来处理它,你不需要检测一个数字是否可被char
整除。只需在字符串中获取这些数字(NUL在c中终止5
数组)并检查最后一位(最低有效数字)是0
还是10001
并检查是否这些数字的数字总和,构成数字(根据您的需要 - 大致在最大3
位数)可以被15
整除。如果两个条件都满足,那么15
可以整除的条件(3
的主要因素是5
和15
,它指示一个可被{{1}整除的数字}}应该可以被3
和5
两者整除,它可以被15
整除。
答案 1 :(得分:2)
"确定数字n是否可被15整除"
要使这一点更加通用并回答问题并不是很难:
"确定数字N是否可以被M整除"
将大整数N
编码为十进制数字的字符串。对每个数字执行模数学运算(%
),将先验结果缩放10.然后检查最终结果是否为0.
y = (y * 10 + digit) % m;
简单的函数调用
unsigned string_mod(const char *s, unsigned m) {
unsigned y = 0;
while (*s) {
unsigned digit = (unsigned) (*s++ - '0');
y = (y * 10 + digit) % m;
}
return y;
}
测试代码
void test(const char *s, unsigned m) {
printf("%s is%s divisible by %u\n",
s, string_mod(s,m) == 0 ? "" : " not", m);
}
int main(void) {
test("100",3);
test("123456",3);
test("100",15);
test("105",15);
// Make big numbers
char buffer[1001+1];
sprintf(buffer, "1%01000d", 0);
test(buffer,15);
sprintf(buffer, "1%0999d5", 0);
test(buffer,15);
}
输出
100 is not divisible by 3
123456 is divisible by 3
100 is not divisible by 15
105 is divisible by 15
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 is not divisible by 15
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005 is divisible by 15
答案 2 :(得分:0)
你可以使用一些“BigInt”库 像https://gmplib.org/只是谷歌搜索“BigInt C库”会给出很多例子。 根据你所说的任务...很可能它确实特别是以这样一种方式组成,你不必寻找一个直截了当的答案,但不知何故作弊