快速切题 sgu118. Digital Root 秦九韶公式

118. Digital Root

time limit per test: 0.25 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 987is 6. Your task is to find digital root for expression A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A+ 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 areN positive integer numbers (sequence A). Each of this numbers is non-negative and not more than 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

//started 21:52
#include<cstdio>
#include <cstring>
using namespace std;
const int maxn=1000;
int a[maxn],n;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%d",a+i);
        int ans=0;
        for(int i=n-1;i>=0;i--){
            ans+=1;
            ans*=(a[i]%9);
            ans%=9;
        }
        if(ans!=0)printf("%d\n",ans);
        else {
            for(int i=0;i<n;i++){
                if(a[i]!=0){
                    puts("9");
                    break;
                }
                if(i=n-1)puts("0");
            }
        }
    }
    return 0;
}
//first ok 21:57
//first wa 22:02 假设爆int

  

posted @ 2014-10-02 22:05  雪溯  阅读(198)  评论(0编辑  收藏  举报