UVA - 550 Multiplying by Rotation

来源:互联网 发布:php获取post json数据 编辑:程序博客网 时间:2024/04/29 00:52

Warning: Not all numbers in this problem are decimal numbers!


Multiplication of natural numbers in general is a cumbersome operation. In somecases 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 aboveexample 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 standardend-of-file marker.

Output 

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

Sample Input 

10 7 49 7 417 14 12

Sample Output 

624题意:就是将最后一位数变为第一位数,需要几次?步骤如下:
179487 * 4 = 717948(10进制)
4*7=28    28!=7   28/10=2   28%10 =8
4*8+2=34   34!=7   34/10=3   34%10=4;
4*4+3=19   19!=7   19/10=1   19%10=9;
4*9+1=37   37!=7   37/10=3   37%10=7;
4*7+3=31   31!=7   31/10=3   31%10=1;
4*1+3=7    7==7;(end)
则这个数为179487,6位数,输出6。
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int main(){int jz;int last;int cs;int n = 0;while(scanf("%d%d%d",&jz,&last,&cs) != EOF){int ys = 0;int n = 0;int ls = last;for(;;){if(ls*cs + ys == last){n ++ ;break;}n ++ ;int l = ls;ls = (cs * ls + ys) % jz;ys = (cs * l + ys) / jz;}printf("%d\n",n);}return 0;}


 

0 0