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中,
整除判断可以变求余边扩展。

 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 int main(){
 6     int base;    string num;
 7     while(cin>>base && base != 0){
 8         cin>>num;
 9         int x = 0;    int y = 0;
10         for(int i = 0;i < num.length();i++){
11             x += num[i] - '0';
12         }
13         for(int i = 0;i < num.length();i++){
14             y = y * base + num[i] - '0';
15             y %= x;
16         }
17         
18         if(y == 0){
19             printf("yes
");
20         }else{
21             printf("no
");
22         }
23     }
24     return 0;
25 } 

What Greek,do put shit!
原文地址:https://www.cnblogs.com/grey-qisen/p/4747524.html