如何检查除以7的大数字在C ++? [英] How to check for division by 7 for big number in C++?

查看:126
本文介绍了如何检查除以7的大数字在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查,如果给定的数字可以被7整除,这是通常做一些像 n%7 == 0 ,但问题是,该给定数字可以高达100000000,这甚至不适合 long long

I have to check, if given number is divisible by 7, which is usualy done just by doing something like n % 7 == 0, but the problem is, that given number can have up to 100000000, which doesn't fit even in long long.

另一个约束是,我只有几KB的内存可用,所以我不能使用数组。

Another constrain is, that I have only few kilobytes of memory available, so I can't use an array.

我期望的数字在stdin和输出为 1

I'm expecting the number to be on stdin and output to be 1/0.

这是一个示例

34123461273648125348912534981264376128345812354821354127346821354982135418235489162345891724592183459321864592158
0

应该可以只使用大约7个整数变量和 cin.get()

It should be possible to do using only about 7 integer variables and cin.get(). It should be also done using only standard libraries.

推荐答案

想想你如何在纸上划分。你看看第一个数字或两个,并写下最接近的七的倍数,携带剩下的,等等。您可以在任何长度编号上执行此操作,因为您不必将整个数字加载到内存中。

Think about how you do division on paper. You look at the first digit or two, and write down the nearest multiple of seven, carry down the remainder, and so on. You can do that on any abritrary length number because you don't have to load the whole number into memory.

这篇关于如何检查除以7的大数字在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆