[英]print out all the numbers between user input and condition that are divisible by 2 and 3. while loop ask for another number
[英]Figure out if a very large number is divisible by another
我正在处理一个编程问题,我需要将一个非常大的数字(10^50000 的数量级)除以 1000 的数量级,以确定一个是否可以被另一个整除。 所以我需要使用字符串来存储这个大数字。
我不知道将字符串除以数字的任何方法。 谁能指出我的算法?
这是一个算法的开始。 它似乎可以满足您的要求:它在 4 个字符块中执行“长除法”,并跟踪其余部分。 它在最后打印余数。 您应该能够根据您的特定需求进行调整。 注意 - 在 4 字节块中执行此操作使其比传统的一次一个字符算法要快得多,但它确实依赖于b
足够小以适合int
(因此是 4 个字符块)。
#include <stdio.h>
#include <string.h>
int main(void) {
char as[]="123123123123126";
int l = strlen(as);
int rem;
int lm = l % 4; // the size of the initial chunk (after which we work in multiples of 4)
char *ap=as; // a pointer to keep track of "where we are in the string"
int b=123; // the divisor
int a;
sscanf(ap, "%4d", &a);
while(lm++ < 4) a /= 10;
// printf("initial a is %d\n", a);
rem = a % b;
for(ap = as + (l%4); ap < as + l; ap += 4) {
// printf("remainder = %d\n", rem);
sscanf(ap, "%4d", &a);
// printf("a is now %d\n", a);
rem = (a + 10000*rem) %b;
}
printf("remainder is %d\n", rem);
return 0;
}
输出:
remainder is 3
这需要一些清理,但我希望你明白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.