快速切题 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*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 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
快速切题 sgu118. Digital Root 秦九韶公式的更多相关文章
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】
3751: [NOIP2014]解方程 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4856 Solved: 983[Submit][Status ...
- Digital Root 的推导
背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...
- 【HDOJ】4351 Digital root
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
随机推荐
- map按value值查找——find_if的使用(转载)
转载:http://www.cnblogs.com/xufeiyang/archive/2012/05/09/2491871.html CValueFind #ifndef _CVALUEFIND_H ...
- IOS项目中的细节处理,如更改状态栏等等
一,状态栏更改为白色 1 在info.plist中添加一个字段:view controller -base status bar 为NO 2 在需要改变状态栏颜色的ViewController中在Vi ...
- HTML切换页面IE版本
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...
- Python3基础 set 自动将重复合并掉 不支持索引
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- arm linux利用alsa驱动并使用usb音频设备
一.背景: arm linux的内核版本是3.13.0 二.准备工作 添加alsa驱动到内核中,也就是在编译内核的时候加入以下选项: 接下来就重新编译内核即可 三.交叉编译alsa-lib和alsa- ...
- vim的加密和解密?
vim中出现的错误提示含义: 参考: http://blog.csdn.net/u014599371/article/details/43955169 E488: trailing character ...
- 【TCP/IP详解 卷一:协议】第二十一章 TCP的超时与重传
作为TCP的重头戏,本章节涉及了许多关于计算方面的内容,使用了大量的例子来指明一些观点. 我使用的理解方法是:通过别人的博客,以及实例结合进行理解,不然会很吃力. 21.1 引言 reliable T ...
- 《剑指offer》第二十三题(链表中环的入口结点)
// 面试题23:链表中环的入口结点 // 题目:一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, // 环的入口结点是结点3. #include <iostream> ...
- Spring AMQP 源码分析 05 - 异常处理
### 准备 ## 目标 了解 Spring AMQP Message Listener 如何处理异常 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListene ...
- Spring Boot中注解事务