給定3個數(假設B,A,S),B表示B進位,A表示被乘數的最低位數字,S表示乘數,也就是現在是xxxxxA * S,問符合 xxxxxA * S = Axxxxx則Axxxxx是幾位數?
想法:
可以解出xxxxx是多少,以題目example(179487)為例,B=10, A=7, S=4,可以從等式
abcde7 * 4 =
7abcde
發現,7*4=28,所以e=8,進位2,然後知道e以後,8*4+2=34,d=4,進位3,4*4+3=19,c=9,進位1,依此類推...。因此我們可以將每個位數算出來,迴圈終止條件為該位數==A且進位為0。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
int main() | |
{ | |
int B, A, S; | |
while (scanf("%d%d%d", &B, &A, &S) != EOF){ | |
int carry = 0, len = 0, x = A; | |
while (1){ | |
int tmp = x * S + carry; | |
carry = tmp / B; | |
x = tmp % B; | |
len++; | |
if (carry == 0 && x == A) break; | |
} | |
printf("%d\n", len); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言