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 ...
随机推荐
- 最短路(dijskra+SPFA+Bellman)
最短路 Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- UCS2转UTF-8方法
前两天工作时需要将UCS2编码转为UTF-8编码在网页显示.网上找了好久没有好方法,后来还是同事给了一个自己的函数,顺利解决问题.把函数贴在这里,愿帮助遇到同样问题的工友们~ /** * U ...
- 怎样解决xcode里开发cocos2dx改动lua脚本后不刷新的问题
用xcode来开发cocos2dx,结果发现一个非常纠结的问题,假设我一旦改动了一个Lua文件,我必须clean之后再build,否则改动的Lua文件不会体现出来.这是一个非常令纠结的结果,特别是我要 ...
- jQuery-瀑布流 布局 (处理页面滚动和AJAX加载延迟问题)
瀑布流:这种布局适合于小数据块,每个数据块内容相近且没有侧重.通常,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部. 一.功能分析: 1.判断图片是否进入可视区域: 2.用AJAX ...
- .NET面试题解答
抽象类和接口有什么区别?使用时候有什么需要注意的吗?答:相同点:都不能被直接实例化,都通过继承实现其抽象方法: 不同点: 1) 接口支持多继承:抽象类不能实现多继承: 2) 接口只能定义行为:抽象类既 ...
- asp.net验证控件中常用的正则表达式
只能输入数字:“^[0-9]*$” 只能输入n位的数字:“^\d{n}$” 只能输入至少n位数字:“^\d{n,}$” 只能输入m-n位的数字:“^\d{m,n}$” 只能输入零和非零开头的数字:“^ ...
- 从C++对象内存布局和构造过程来具体分析C++中的封装、继承、多态
一.封装模型的内存布局 常见类对象的成员可能包含以下元素: 内建类型.指针.引用.组合对象.虚函数. 另一个角度的分类: 数据成员:静态.非静态 成员函数:静态.非静态.虚函数 1.仅包含内建类型的场 ...
- 关于wireshark的两个抓包过滤显示的基本语法
关于wireshark的两个基本语法 关于wireshark的两个基本语法 1. Capture Filters 语法:<Protocol name><Direction>&l ...
- Art of Unit Test (1) - Breaking Dependency
#!/usr/bin/env python # encoding: utf-8 import unittest """ the simplyest way to test ...
- linux下/proc/sysrq-trigger文件的功能
/proc/sysrq-trigger该文件能做些什么事情呢? # 立即重新启动计算机 (Reboots the kernel without first unmounting file system ...