稍微麻烦一点
道理还是一样的
如果两个都是高精度数的话好象只能够用减法来实现了
我喜欢创造,一只扑腾着翅膀向天空飞翔的乌鸦
稍微麻烦一点
道理还是一样的
如果两个都是高精度数的话好象只能够用减法来实现了
判断是否可以整除问题.
对输入的一个某进制下的整数,是否可以整除该数各位数之和.
这是背景:
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.
我就演变了一下.高手求教.
判断是否可以整除问题.
对输入的一个某进制下的整数,是否可以整除该数各位数之和.
这是背景:
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.
我就演变了一下.高手求教.
请问楼主,您是要解这题,还是最上面的问题?
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.
以这题来讲,您都可以把输入的数num,num为b进制(假设进制为b,由用户输入),在十进制的基础上,把各位相加,得数sum,再把b转换为十进制数de_num;只要判断sum|de_num就可以了,
如:5进制1234
step1:存入字符串str_num[];
step2:把各位相加,sum+=str_num[i++]-48;
step3:把str_num中的字符转换为十进制数de_num;
step4:de_num%sum?puts("no"):puts("yes");
您看看,这样可以不?
以下程序用递归实现别的进制到十进制的转换。
#include "stdio.h"
#include "math.h"
#include "string.h"
long num=0;
void to_decimal(char *str,int radix)
{
if(*str)
{
num+=(*str-48)*pow(radix,strlen(str)-1);
to_decimal(str+1,radix);
}
}
int main()
{
char str_num[]="1234";
to_decimal(str_num,5);
printf("%ld",num);
}