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 ...
随机推荐
- AngularJs(八) 过滤器filter创建
大纲 示例 过滤器的使用 创建过滤器 demo 这是整个示例demo 1.filter.js文件 angular.module("exampleApp", []) .constan ...
- React系列(一):React入门
React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...
- uri 和 url 的区别
uri 统一资源标识符,值是唯一标识资源的任意字符,比如guid url 统一资源定位符,值是标识资源的字符串,但是包含定位信息,比如http://localhost/index.html
- w3school教程整理
原文链接:http://www.flygon.net/w3school 原文链接:https://github.com/wizardforcel/w3school w3school教程整理 离线版大部 ...
- Linux下包含头文件的路径问题与动态库链接路径问题
C/C++程序在linux下被编译和连接时,GCC/G++会查找系统默认的include和link的路径,以及自己在编译命令中指定的路径.自己指定的路径就不说了,这里说明一下系统自动搜索的路径. [1 ...
- 学习C++语言的50条忠告
50条忠告:(其中有几条觉得写的不够贴切,所以删了,发了余下的部分) 1.把C++当成一门新的语言学习: 2.看<Thinking In C++>,不要看<C++变成死相>: ...
- sql防注入代码
function defend_sql($string, $force = 1) { $preg = "select|insert|and|or|update|delete|\'|\/\*| ...
- Linux学习之常用技巧
▌基础 学习 Bash .你可以man bash来看看bash的东西,并不复杂也并不长.你用别的shell也行,但是bash是很强大的并且也是系统默认的.(学习zsh或tsch只会让你在很多情况下受到 ...
- struts2日常
更新struuts2 的action后不能马上反应,要重启服务器才可以,加上 <constant name="struts.devMode" value="true ...
- 深入理解this对象
最近一直在看js关于面向对象编程方面的东西,那么this肯定是需要一个被吃透 理解 同时灵活运用的对象 现在总结一下自己的学习成果: 我们可以用一句很形象的话来理解什么是this关键字? " ...