zoj 1154 Niven numbers

 见zoj 1154

 还是需要将输入数据当作字符串来处理,不能直接使用整型。

 

/* 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;
}
 

 

你可能感兴趣的:(number)