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 sum of its digits is 3, which divides 111. We can also specify a number in another base b, and a number in base b is a Niven number if the sum of its digits divides its value.
Given b (2 <= b <= 10) and a number in base b, determine whether it is a Niven number or not.
Input
Each line of input contains the base b, followed by a string of digits representing a positive integer in that base.
There are no leading zeroes. The input is terminated by a line consisting of
0 alone.
Output
For each case, print "yes" on a line if the given number is a Niven
number, and "no" otherwise.
Sample Input
10 111
2 110
10 123
6 1000
8 2314
0
Sample Output
yes
yes
no
yes
no
本题意思为给定一个进制(base),然后给一个在base进制下的数字NUM,判断NUM是否为尼玛数(NUM能否被NUM各位数字之和整除) 由于NUM的大小限制在int内,而base会让int型超出范围,比如8(10) = 1000(2),所以NUM需要为字符串类。第二个关键在于溢出处理,同HOJ1008中,
整除判断可以变求余边扩展。
#include<iostream>
using namespace std;
#include<string> int main(){
int base; string num;
while(cin>>base && base != ){
cin>>num;
int x = ; int y = ;
for(int i = ;i < num.length();i++){
x += num[i] - '';
}
for(int i = ;i < num.length();i++){
y = y * base + num[i] - '';
y %= x;
} if(y == ){
printf("yes\n");
}else{
printf("no\n");
}
}
return ;
}
HOJ1014的更多相关文章
- OJ题目分类
POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...
随机推荐
- List的方法和属性 方法或属性 作用
List的方法和属性 方法或属性 作用 Capacity 用于获取或设置List可容纳元素的数量.当数量超过容量时,这个值会自动增长.您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以 ...
- AngularJs(八) 过滤器filter创建
大纲 示例 过滤器的使用 创建过滤器 demo 这是整个示例demo 1.filter.js文件 angular.module("exampleApp", []) .constan ...
- ASP.NET动态引用WebService接口
尊重原著作:本文转载自http://www.mhzg.net/a/20124/20124912180589.html 有经验的朋友都知道,通常我们在引用webservice的时候,是在项目中就添加了引 ...
- JAVA 堆设置
JAVA 堆设置 第四节 堆已经讲得差不多啦,这章我们以一个例子来说说如何设置以及当发生堆溢出的时候怎么排查问题.先看一小段代码: 代码中使用了一个无限循环来为list添加对象,如果采 ...
- 我也能上google
之所以起个这么隐晦的标题就是因为怕被黑.但是不能上谷歌,对于我们搞技术的人来说真的很残忍. 目前只找到几个镜像网址: https://xie.lu/ https://www.gotosearch.in ...
- C#学习日志 day 5 plus------ interface 数组及stringBuilder相关
interface 接口interface可以理解为两个程序达成的协议. 实际就是一个留给后续开发的框架.若想继承这个interface,就必须实现interface规定的 函数及结构等.一般会以大写 ...
- JSP中的Attribute和InitParameter
属性:Attribute类型:应用/上下文,请求,会话(ServletContext,HttpServletRequest/ServletRequest,HttpSession)设置方法:setAtt ...
- CentOS下重新安装yum
1,下载最新的yum-3.2.28.tar.gz并解压 #wget http://yum.baseurl.org/download/3.2/yum-3.2.28.tar.gz#tar xvf yum- ...
- TLSAlloc()
为什么要有TLS?原因在于,进程中的全局变量与函数内定义的静态(static)变量,是各个线程都可以访问的共享变量.在一个线程修改的内存内容,对所有线程都生效.这是一个优点也是一个缺点.说它是优点,线 ...
- Delphi中TWebBrowser中注入Js
最近帮朋友做一个软件,其中要自动化某网页中的操作,最简的操作是调用自己写的代码. 代码如下: procedure TForm1.Button2Click(Sender: TObject);var i ...