ZOJ 3449 Doraemon's Number Game III (数论)

Doraemon likes to play number game with Nobita.

One day, Doraemon came up with an idea, he gave a number a in base 10 and a number x(x < 10), and then he let Nobita multiplies the first digit of a with x and add the second digit of a, then multiplies x again and add the third digit of a. Do the above rules until there is only one digit (the leftmost digit of a is called the first digit).

For example, let a = 123, x = 2. Then, 123 → (1*2+2)*2+3=11 → 1*2+1=3. That is to say, Nobita should give the answer 3.

Sometimes a is too large, Nobita cannot work it out, can you help him?

Input

Input contains multiple cases, process to the end of file. Each case contains one line which contains only one number a (the length of a would not exceed 100000) as described above.

Output

For each case, you need to output the number when x = 1, 2 ... 9. You should output the nine numbers in one line separated by a single space.

Sample Input
123
Sample Output
6 3 4 9 8 7 9 9 9

 【题解】

 题意:给定一个数,最长有100000位,现在讲这个数按照a[i]*x+a[i+1]的规律求和,求和后的数再用相同的方法求和,直到只剩下一位数,其实这个过程就是将一个十进制数一直按b进制解释转成十进制,b小于10,于是最后变成一个数字,问这个数字是多少。,现在给定一个数,输出x的值从1 到 9 时对应的数。

 

分析:

 如果给的数的长度是 len ,那么这个式子就变成了 a[1]*b^(len)+a[2]*b^(len-1)+...+a[len];

 因为要按b进制转换成十进制,而且只要求最后的值,所以我们可以边转化边取模,这样的结果和最后一次转化的结果是一样的,最后答案就是b+(n-10)%(10-b)了,具体过程见代码注释:

 

 ps:这道题是浙大的,有个小问题,就是判断字符串终点的时候我用i<len是错的,而改成s[i]!='\0' 就对了,目前还不清楚这两者有什么区别,这个也是个坑点,,如果哪位大佬晓得,可以留言为我解惑,感激不尽~  如果是玄学....就不用管它了。

 【AC代码】

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+10;
char s[N],len;

int judge(int k)//
{
    if(len==1 && s[0]<='9') return s[0]-'0';//只有一位的话直接返回
    int ans=0;
    for(int i=0;i<len;++i)//这儿的终点判断  注意了
    {
        ans= ans*10 + s[i]-'0';//转化
        ans %= (10-k);//先转化成10-k进制
    }
    ans=(ans-10) % (10-k);
    while(ans<0) ans+=(10-k);
        return ans+k;
}

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值