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 ...
随机推荐
- CodeForces 540B School Marks(思维)
B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 使用扩展名获取mimetype
在Android中很多时候我们需要计算出文件的mimetype,而我们通常的思路就是通过扩展名来获取对应的mimetype,而如果自行处理,将维护一个比较大的映射表,而实际上大可不必,Android提 ...
- [ACM] hdu 2191 珍惜如今,感恩生活 (多重背包)
Problem Description 急!灾区的食物依旧短缺! 为了拯救灾区同胞的生命,心系灾区同胞的你准备自己採购一些粮食支援灾区,如今如果你一共同拥有资金n元,而市场有m种大米,每种大米都是袋装 ...
- T-SQL流程控制
常用的T-SQL流程控制有三种,case ... when ... then...(else)...end (as) ... 判断句式,if判断句式和while循环句式. case...when .. ...
- CentOS7与Win7双系统引导问题
先安装的Win7,后安装的CentOS7,结果系统引导就只有CentOS7了.记得以前CentOS6.x系列没这个问题,主要是由于CentOS7.x使用grub2的原因吧. 方案一:使用Win PE. ...
- 让Linux修改IP、DNS等可以更简单
修改IP: 可以用 netconfig,可惜每次都得输入完整的IP.掩码.网关和DNS. 不如直接 vi /etc/sysconfig/network-scripts/ifcfg-eth0 再 /et ...
- 在IOS开发中,属性名为id的处理方法
在.h 文件中定义属性名为id { int _id; } @property (nonatomic, assign) int id; 在.m 文件中用synthesize声明该属性,会自动生成get和 ...
- C#:判断一个String是否为数字
方案一:Try...Catch(执行效率不高)private bool IsNumberic(string oText){ try { ...
- 摩根斯坦利 - 2016年09月8日 面试题 - HashMap
摩根斯坦利 - 2016年09月8日 面试题: 给定一个 Map<Person, Object> map = new HashMap<Person, Object>(); 放入 ...
- Programming C#.Inheritance and Polymorphism
继承 C#中,创建派生类要在派生类的名字后面加上冒号,后面再跟上基类的名字: public class ListBox : Control 提示:C++程序员注意了,C#没有私有或者保护继承 多态 继 ...