2015四川省acm B题
Carries
frog has n
integers a1,a2,…,an,
and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines hardness
h(x,y)
for adding x
and y
the number of carries involved in the calculation. For example,
h(1,9)=1,h(1,99)=2.
Find the total hardness adding n
integers pairwise. In another word, find
.
Input
The input consists of multiple tests. For each test:
The first line contains 1
integer n
(2≤n≤105).
The second line contains n
integers a1,a2,…,an.
(0≤ai≤109).
Output
For each test, write 1
integer which denotes the total hardness.
Sample Input
2
5 5
10
0 1 2 3 4 5 6 7 8 9
Sample Output
1 20原题链接: http://acm.scu.edu.cn/soj/problem.action?id=4437
题意:给出几组测试数据,要求分别输出每组数的所有元素相加会发生多少次进位,输出进位数。
方法:分别求每位上的的进位数,直接快速排序,然后两个指针扫描。
代码如下:
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 100005
using namespace std;
long long int mergesort(int a[],int begin,int end)
{
int b[maxn];
long long num;
long long count=0;
int n=end+1,p;
int q,r;
for(int k=1;;k++)
{
p=0,q=0,r=end;
num=pow(10,k);
for(int i=0;i<n;i++)
{
b[i]=a[i]%num;
if(b[i]!=a[i])
{
p=1;
}
}
sort(b,b+n);
while(r!=q)
{
if(b[q]+b[r]>=num)
{
count+=r-q;
r--;
}
else
{
q++;
}
}
if(p==0)
break;
}
return count;
}
int main(void)
{
int n,a[maxn];
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>a[i];
cout<<mergesort(a,0,n-1)<<endl;
}
return 0;
}
2015四川省acm B题的更多相关文章
- 2015.12.29~2015.12.30真题回顾!-- HTML5学堂
2015.12.29~2015.12.30真题回顾!-- HTML5学堂 吃饭,能够解决饥饿,提供身体运作机能.练习就像吃饭,强壮自己,提升编程技能,寻求编程技巧的最佳捷径!吃饭不能停,练习同样不能停 ...
- 2015.12.21~2015.12.24真题回顾!-- HTML5学堂
2015.12.21~2015.12.24真题回顾!-- HTML5学堂 山不在高,有仙则名!水不在深,有龙则灵!千里冰封,非一日之寒!IT之路,须厚积薄发!一日一小练,功成不是梦!小小技巧,尽在HT ...
- ACM 做题过程中的一些小技巧。
ACM做题过程中的一些小技巧. 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 2.有时候int型不够用,可以用long l ...
- ACM 刷题小技巧【转】
转载自URl-team ACM做题过程中的一些小技巧. 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout.cin和printf.scanf最好不要混用. 大数据输入输出时最好 ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- 杭电acm 1076题
水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...
- 杭电acm 1037题
本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...
- 杭电acm 1038题
本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...
- 杭电acm 1049题
一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...
随机推荐
- duilib消息类型
//定义所有消息类型 ////////////////////////////////////////////////////////////////////////// #define DUI_MS ...
- sqlserver2008客户端设置主键自增
是标识改为是
- Linux下安装mysql(yum和源码编译两种方式)
这里介绍Linux下两种安装mysql的方式:yum安装和源码编译安装. 1. yum安装 (1)首先查看centos自带的mysql是否被安装: # yum list installed |grep ...
- nc简单应用
传输本地文件test到172.19.135.12: 172.19.135.12接收端 nc -l 1234 > test 本地为 发送端 nc 172.19.135.12 1234 & ...
- java 中字符串比较equals()和equalsIgnoreCase()的区别
1.使用equals( )方法比较两个字符串是否相等 boolean equals(Object str) 这里str是一个用来与调用字符串(String)对象做比较的字符串(String)对象. 如 ...
- 【转】char*,string,CString,int,char[]之间的转换
CString 头文件#include <cstring>.CString 转char * CString cstr; ..data(),返回没有”/“的字符串数组..c_str(),返 ...
- tomcat中session在两个webapp中实现共享
现在遇到一个需求就是要求完成简单的单点登录,通过在一个tomcat实例中放置两个webapps应用ROOT应用和CEO应用来完成在ROOT应用登录后,在CEO可以直接使用,而未在ROOT应用登录时,不 ...
- 二目运算符 “->”
-> 在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符“.”.用法:p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员.表达式p->a引用了指针p指向 ...
- centos7添加图像化桌面并设置中文
我前面是使用的centos6.最近才最小化安装了一个centos7.4(最小化安装有很多命令都没有,所以不建议这样干).完了装了图形化界面和设置中文,感觉和centos6有些区别,所以记录一下过程. ...
- c# 实现mysql事务
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...