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

来源:互联网 发布:高新区网络问政平台 编辑:程序博客网 时间:2024/05/18 23:14

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;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机显示sd卡受损怎么办 美的冰箱出现e6怎么办 美的冰箱显示e6怎么办 冰箱电脑板坏了怎么办 笔记本网线接口坏了怎么办 蓝p吃了一片 怎么办 sd卡上锁了忘记密码怎么办 手机sd卡被锁定怎么办 冰箱制冷管堵了怎么办 冰箱的管子破了怎么办 淘宝京东e卡冻结怎么办 苏宁任性付冻结怎么办 苏宁订单删除了怎么办 联通销户话费有余额怎么办 暖气改地热不热怎么办 老楼房暖气不热怎么办 4s店修不好车怎么办 苏宁的发票丢了怎么办 京东退货没有发票怎么办 发票发错了邮箱怎么办 苹果手机忘记电子邮箱验证码怎么办 退差价把红包退还了怎么办 网上购票票丢了怎么办 岗位人手不够老板又不招人来怎么办 辞职后提成不发怎么办 老板给客户吵架员工该怎么办 冰柜声音大怎么办嗡嗡响 交了钱电没有怎么办 小白熊电动吸奶器显示F1怎么办 花洒的水变小了怎么办 手机插卡处坏了怎么办 吉利帝豪一键启动钥匙没电怎么办 居民医保断交5年怎么办 社保和医保断了怎么办 停缴了两年社保怎么办 医保交不够20年怎么办 医保断交4个月怎么办 医保断交5个月怎么办 换城市后社保卡怎么办 苹果6s呼叫失败怎么办 苹果手机打电话显示呼叫失败怎么办