SGU 118 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=118

118. Digital Root

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit number then it is a digital root for n and otherwise digital root of n is equal to digital root of f(n). For example, digital root of 987 is 6. Your task is to find digital root for expression A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1.

设 f(n) 表示十进制正整数 n 的各位数字之和。如果 f(n) 是一个1位数那么他就是 n 的数根。否则的话 f(n) 的数根就是 n 的数根。举例说明:987的数根是 6(9+8+7=24 2+4=6)。你的任务是算出这样的数的数根: A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1。

Input

Input file consists of few test cases. There is K (1 <=K <= 5) in the first line of input. Each test case is a line. Positive integer number N is written on the first place of test case (N<=1000). After it there are N positive integer numbers (sequence A). Each of this numbers is non-negative and not more than 109.

输入包含K个测试点.在第一行会给出 K (1 <= K <= 5).每个测试点一行.首先是一个正整数N (N <= 1000). 接着N个非负整数 (序列 A). 均不超过109.

Output

Write one line for every test case. On each line write digital root for given expression.

每个测试点一行,输出给定数的数根.

Sample Input

1
3 2 3 4

Sample Output

5

CODE

#include<stdio.h>
 
int main()
{
  int k,n,x,t,sum;
  scanf("%d",&k);
  while(k--)
    {
      sum=0;
      t=1;
      scanf("%d",&n);
      while(n--)
	{
	  scanf("%d",&x);
	  x%=9;
	  t*=x;
	  t%=9;
	  sum+=t;
	  sum%=9;
	}
      if(sum==0) sum=9;
      printf("%dn",sum);
    }
  return 0;
}
» 本博客采用署名 2.5 中国大陆许可协议进行许可,本文版权归作者所有,欢迎转载,但必须在明显位置给出原文连接。
anyShare分享到:
发表评论?

0 条评论。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>