您的位置:首页 > 编程语言 > C语言/C++

Multiplying by Rotation UVA550

2013-10-04 11:47 399 查看


  Multiplying by Rotation 
Warning: Not all numbers in this problem are decimal numbers!

Multiplication of natural numbers in general is a cumbersome operation. In some cases however the product can be obtained by moving the last digit to the front.

Example: 179487 * 4 = 717948

Of course this property depends on the numbersystem you use, in the above example we used the decimal representation. In base 9 we have a shorter example:

17 * 4 = 71 (base 9)

as (9 * 1 + 7) * 4 = 7 * 9 + 1

Input 

The input for your program is a textfile. Each line consists of three numbers separated by a space: the base of the number system, the least significant digit of the first factor, and the second factor. This
second factor is one digit only hence less than the base. The input file ends with the standard end-of-file marker.

Output 

Your program determines for each input line the number of digits of the smallest first factor with the rotamultproperty. The output-file is also a textfile. Each line contains the answer for the corresponding
input line.

Sample Input 

10 7 4
9 7 4
17 14 12


Sample Output 

6
2
4


Miguel A. Revilla 

1998-03-10

一道简单的数论题目,题意大概是给你一个基数,尾数,乘数,找出这样一个数字这个数字乘以乘数是的最低位变换到最高位,其他的位向右移动一位,好比这串数字循环移位,并要求这个数字位数最少。。。

179487 * 4 = 717948(10进制)

4*7=28     28!=7    28/10=2    28=8;

4*8+2=34    34!=7    37/10=3    34=4;

4*4+3=19    19!=7    19/10=1    19=9;

4*9+1=37    37!=7    37/10=3    37=7;

4*7+3=31    31!=7    31/10=3    31=1;

4*1+3=7      7==7;(end)

则这个数为179487,6位数,输出6。

大家发现规律了么。。。用给出的末位乘上乘数后,求余得到的数是下一位,又可以用这个下一位乘上乘数加上进位看看是不是等于要求的末位。。感觉说的不清楚啊。。语言表达能力太弱。

#include<iostream>

using namespace std;

int main()
{
int base,last,multi;
while(cin>>base>>last>>multi)
{
int remain=0,num=0;
int last1=last;
while(1)
{
if(last==multi*last1+remain)
{
num++;
break;
}
num++;
int last2=last1;
last1=(multi*last1+remain)%base;
remain=(last2*multi+remain)/base;
}
cout<<num<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ ACM