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 ...
随机推荐
- window快捷登陆linux的的设置方式(设置ssh的config配置)
看看网上其他人如何写的: http://www.xuebuyuan.com/414672.html 文中~的意思是用户目录下的意思: http://blog.csdn.net/newjueqi/art ...
- 配置自己风格的Clang-Format-Xcode
在项目根目录下,创建一个文件.clang-format,使用vim打开并修改. 具体的配置风格可以看这里:http://clang.llvm.org/docs/ClangFormatStyleOpti ...
- 测试中的代码分享~将可以合并的方法去合并Func不赖
在面向对象的设计中,我们经常会谈到“重构”,而重构之中有个叫合并方法的,就是将多个方法干的事类似,或者说,方法体的长向很像,那么,我们需要去考虑,将它们进行抽象! 原来的代码: /// <sum ...
- MVC5 Controller简要创建过程(2):由ControllerFactory创建Controller
上文已经完成了ControllerFactory的创建,接下来就是调用其CreateController()方法创建Controller了. DefaultControllerFactory中Crea ...
- SSIS之Foreach循环容器用法
要实现的业务:A数据库服务器上某库的T_GOODS_DECL的状态字段“Is_Delete”标记为“1”的时候删除B数据库服务器上对应库的T_GOODS_DECL表中的记录,二者的主键为“DECL_N ...
- c# 根据中文汉字获取到拼音
public static String ConvertToPinyin(String str) { if (String.IsNullOrEmpty(str)) return String.Empt ...
- OCP prepare 20140627
1. catalog start with catalog start with 是一个很好的命令. 有了这个命令后, 基本上可以不再使用catalog数据库了 . 因为可以通过这个命令将以前的备份 ...
- mysql函数操作(5)
<?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...
- php 按列值合并数据
/* * PHP按值合并数组 * */ function my_array_merge(&$array1, &$array2) { $result = Array(); foreach ...
- client|server 最简单的聊天
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket ...