hoj 1014 Niven Numbers

新手上路之我的水题之路

刚开始时,我首先想到的是定义一个int数组,然后让输入的数字从最低位开始在循环不断地*base,从而将

k进制转化成十进制;然后再作取模判断就可以了;

这时在将最低位到最高位依次乘以相应的n次幂是,需要知道读入数字的位数:

while(t>0)

{
    n++;  //n是最后得到的数字位数;
    t=t/10; //t是输入的数字;
}

  然后得到了k进制转化成十进制的简单代码:

int main()

{
   int i,j,a,b,c,t,sum=0,n=0;
   scanf("%d %d",&c,&b);
   t=c;
   while(t>0)
   {
          n++;
          t=t/10;
   }
   t=c;
   for(i=1; i<=n; i++)
   {
    a=t%10;
    t=t/10;
    for(j=1; j<i; j++)
   {
       sum+=a*b;
     }
   }
   sum+=c%10;
   printf("%d\n",sum);
  return 0;
}

但是用这样的想法得到的代码一直是WA;

后来,经过一阵刻骨铭心的检查,发现应该使用字符串来读入数字;

这就有一个问题:

数字字符如何转化为数字来进行四则运算?

经过dalao的帮助,找到了s [ i ] - ‘ 0 ‘;来实现转化;

可是,用字符串改了之后一样不过!!!!!!!!

沃日!!!!!

然后,又是在dalao的帮助下(这次是马dalao与聚聚(∩_∩))告诉我由于

未告知数据范围,默认为1e6!!!!

那么在a*b的时候就有可能爆炸!!!

没办法,改算法(@﹏@)~

在大佬的提示下,自己写了个除法竖式体会了一下:

最初的算法是从最低位开始乘幂,但实际上从最高位开始会更简单;

定义int x=0;

x=x*base+每次的最高位是s [ i ];

再令 x=x%sum //sum是每位上数字之和;

原理就在于:

1234%6==34%6;

从最高位开始,将可以舍去的位数数字全部舍去,这样就避免了数字过大的问题

一下是好不容易AC的代码C(89)

#include <stdio.h>
#include <string.h>
char s[10000010];
int main()
{
    int b,n,i,j,q,t;
    while(scanf("%d",&b)==1&&b!=0)
    {
        scanf("%s",s);
        n=strlen(s);
        q=0;
        t=0;
        for(i=0; i<n; i++)
        {
            q+=s[i]-‘0‘;
        }
        for(i=0; i<n; i++)
        {
            t=t*b+(s[i]-‘0‘);
            t=t%q;
        }

        if(t%q==0)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}
时间: 2024-12-27 09:41:14

hoj 1014 Niven Numbers的相关文章

ZOJ1154 Niven Numbers【进制】

Niven Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB A Niven number is a number such that the sum of its digits divides itself. For example, 111 is a Niven number because the sum of its digits is 3, which divides 111. We can also specify a numb

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

数位dp专题

第一题:Amount of degrees (ural 1057) 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1057 第二题:windy数. 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 第三题:Hdu 2089 不要62 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 第四题:Hdu 3555 Bomb 题目连

HOJ1014

Niven Numbers My Tags   (Edit)   Source : Unknown   Time limit : 1 sec   Memory limit : 32 M Submitted : 5349, Accepted : 965 A Niven number is a number such that the sum of its digits divides itself. For example, 111 is a Niven number because the su

HDU 1014 Uniform Generator 题解

找到规律之后本题就是水题了,不过找规律也不太容易的,证明这个规律成立更加不容易. 本题就是求step和mod如果GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choice 为什么这个结论成立呢? 因为当GCD(step, mod) == 1的时候,那么第一次得到序列:x0, x0 + step, x0 + step-- 那么mod之后,必然下一次重复出现比x0大的数必然是x0+1,为什么呢? 因为(x0 + n*step) % mod: 且不需要考虑x0 % mod的值为

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T