还是需要将输入数据当作字符串来处理,不能直接使用整型。
/* zoj 1154 Niven numbers */
#include <stdio.h>
#define MAX 100
int isNivenNum(int base, char str[]);
int main(void)
{
int totalBlocks;
int base;
int first = 1;
char str[MAX];
scanf("%d", &totalBlocks);
while(totalBlocks-- > 0)
{
if(first)
first = 0;
else
printf("\n");
while(scanf("%d", &base) == 1 && base != 0)
{
scanf("%s", str);
if(isNivenNum(base, str))
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}
int isNivenNum(int base, char str[])
{
int digitSum = 0, decNum = 0;
int temp;
int i;
for(i = 0; str[i] != '\0'; i++)
{
temp = str[i] - '0';
digitSum += temp;
decNum = base *decNum + temp;
}
if(decNum % digitSum == 0)
return 1;
else
return 0;
}
本文介绍了一个用于判断Niven数的C语言程序实现。通过读取输入的基数和字符串形式的数字,该程序能够准确地判断给定数字是否为Niven数(即其能被各位数字之和整除)。文章提供了完整的源代码,并详细解释了核心函数isNivenNum的实现逻辑。
377

被折叠的 条评论
为什么被折叠?



