ZOJ3449 Doraemon's Number Game III

数根的扩展版,把一个十进制的数,写成按b进制形式的十进制数,一直下去,直到在[0,10)之间。

有a(n)*10^n+a(n-1)*10^(n-1)...+a(0) = a(n)*b^n+a(n-1)*b^(n-1)...+a(0) mod (10 - b)
然后答案就是(n - 10) % (10 - b) + b了。

代码
#include <iostream>
#include
<math.h>
#include
<deque>
#include
<string>
#include
<vector>
#include
<string.h>
#include
<stdio.h>
#include
<algorithm>
using namespace std;

const int MAX = 100005;

char ch[MAX];
int len;

int go(int b)
{
if(len == 1 && ch[0] <= '9') return ch[0] - '0';
int res = 0;
for(int i = 0; ch[i] != '\0'; i++)
{
res
= res * 10 + ch[i] - '0';
res
%= (10 - b);
}
res
= (res - 10) % (10 - b);
while(res < 0) res += (10 - b);
return res + b;
}

int main()
{
while(gets(ch))
{
len
= strlen(ch);
for(int i = 1; i <= 9; i++)
{
if(i == 1) printf("%d", go(i));
else printf(" %d", go(i));
}
printf(
"\n");
}
}

 

posted @ 2010-12-31 17:23  litstrong  阅读(233)  评论(0编辑  收藏  举报