ZOJ1154 Niven Numbers【进制】

Niven Numbers
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

You will be given a number of test cases. 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

1

10 111
2 110
10 123
6 1000
8 2314
0

Sample Output

yes
yes
no
yes
no

问题链接ZOJ1154 Niven Numbers
问题简述:(略)
问题分析
????Niven数指数的各位数字之和能够整除数的数。对于指定的b进制数,问是否为Niven数?
????需要注意,题面中没有指定字符串的长度。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* ZOJ1154 Niven Numbers */

#include <stdio.h>

char s[128];

int main(void)
{
    int t, b;
    scanf("%d", &t);
    while(t--) {
        while(~scanf("%d", &b) && b) {
            int sum = 0, num = 0, i;
            scanf("%s", s);
            for(i = 0; s[i]; i++) {
                sum += s[i] - '0';
                num = num * b + s[i] - '0';
            }
            printf("%s\n", num % sum == 0 ? "yes" : "no");
        }
        if(t) printf("\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10355869.html

时间: 2024-08-02 14:00:31

ZOJ1154 Niven Numbers【进制】的相关文章

HDOJ 1390 Binary Numbers(进制问题)

Problem Description Given a positive integer n, find the positions of all 1's in its binary representation. The position of the least significant bit is 0. Example The positions of 1's in the binary representation of 13 are 0, 2, 3. Task Write a prog

HDU1887 ZOJ2982 UVALive3958 Weird Numbers【进制】

Weird Numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 594 Accepted Submission(s): 185 Problem Description Binary numbers form the principal basis of computer science. Most of you have hear

HDU 1197 Specialized Four-Digit Numbers (枚举+进制转化,简单)

题意:让求从2992-9999中所有数字,满足10进制各位之和和12进制和16进制各位数字之和相等. 析:没啥可说的,只能枚举从2992-9999,每个进制都算一下. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map

【HDU 1887】Weird Numbers(负进制转换)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1887 题意:有两种操作,from-r n代表把一个-r进制下的数字n转换成10进制,to-r n代表把一个10进制下的数字n转化成-r进制. 分析:两种操作中from操作很容易实现,和转换成正进制一样,没什么好说的,而to操作我们首先可以像正常转化一样进行短除法,举个例子: 把15转化为-3进制.短除法并且倒序之后结果为1,-2,0.而我们的进制中是不能出现-2的,所以我们可以这样考虑,-2这位代表的

ZOJ 2405 Specialized Four-Digit Numbers(写个进制求和函数)

原题:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 注:看到网上的一些写法,10进制/12进制/16进制 都写了个字函数,其实统一写个进制求和子函数很简单 #include<iostream> using namespace std; int base_sum(int num,int base) { int sum=0; while(num) { sum+=num%base; num/=base; } retur

NUMBER BASE CONVERSION(进制转换)

Description Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-9,A-Z,a-z } HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when

[ACM] ZOJ Martian Addition (20进制的两个大数相加)

Martian Addition Time Limit: 2 Seconds      Memory Limit: 65536 KB   In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on M

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a

______________________________贴一个进制转换的题吧________________________1197________

Specialized Four-Digit Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5291 Accepted Submission(s): 3823 Problem Description Find and list all four-digit numbers in decimal notation that h