Multiplying by Rotation

来源:互联网 发布:新日铁住金软件待遇 编辑:程序博客网 时间:2024/03/29 10:18

Multiplying by Rotation

大意:

有一些数字的乘法计算很奇特,如179487*4=717948;

即把最后一位提到最前面;

现在给出n1,n2,n3,n1代表计算是的进制,n2代表第一个乘数的最后以为,n3代表第三个乘数,求出n2的长度;

要点:

以179487*4=717948为例,由于.....7*4=7.....;

4×7 = 28,得出右数第1个数为8,即.......87×4 = 7.....8;

4×8 = 32,得出第2个数为(28/10)+(32%10)= 4,即.....487×4 = 7.......48;

4*4 = 16,得出第3个数为(32/10)+(16%10)= 9,即....9487×4 = 7.......948;

4*9 = 36,得出第4个数为(16/10)+(36%10) = 7,即...79487×4 = 7....7948;

4*7 = 28,得出第5个数为((36/10)+(28%10)) % 10 = 1,即..179487*4 = 7...17948;

4×1 = 4, 得出第6个数为(28/10 + ((36/10)+(28%10)) / 10)+(4%10)= 7,由于4+3=7没有再进位,计算结束;

代码:

#include <iostream>using namespace std;int main(){int base, last, mul;while (cin >> base >> last >> mul){int cur = last * mul;int m1, m2, count = 1;while (cur != last){m1 = cur / base;m2 = cur % base;cur = m2 * mul + m1;count ++;}cout << count << endl;}}

0 0