Carries
frog has nn integers a1,a2,…,ana1,a2,…,an, and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines hardness h(x,y)h(x,y) for adding xxand yy the number of carries involved in the calculation. For example, h(1,9)=1,h(1,99)=2h(1,9)=1,h(1,99)=2.
Find the total hardness adding nn integers pairwise. In another word, find
∑1≤i<j≤nh(ai,aj)∑1≤i<j≤nh(ai,aj)
.
Input
The input consists of multiple tests. For each test:
The first line contains 11 integer nn (2≤n≤1052≤n≤105). The second line contains nn integersa1,a2,…,ana1,a2,…,an. (0≤ai≤1090≤ai≤109).
Output
For each test, write 11 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
这题题目简单粗暴 ,给你一个长度为n的数组,求出这个数组中任意两个数相加进位的次数这题是一个想法题,把一个数的每一位都分离出来,进行处理也就是取模运算int cnt=lower_bound(b,b+n,temp)-b; lower_bound的返回值是一个元素的指针,b为头指针两者相减就是这个元素与头元素之间有多少个元素。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 long long a[100010],b[100010]; 8 int main() { 9 int n; 10 while(scanf("%d",&n)!=EOF){ 11 for (int i=0 ;i<n ;i++){ 12 scanf("%lld",&a[i]); 13 } 14 long long s=10,ans=0; 15 for (int k=0 ;k<10 ;k++ ,s=s*10){ 16 for (int i=0 ;i<n ;i++ ){ 17 b[i]=a[i]%s; 18 } 19 sort(b,b+n); 20 long long temp; 21 for (int i=0 ;i<n ;i++){ 22 temp=s-b[i]; 23 if (b[n-1]>=temp){ 24 int cnt=lower_bound(b,b+n,temp)-b; 25 if (cnt>i) ans+=n-cnt; 26 else ans+=n-1-i; 27 } 28 } 29 } 30 printf("%lld\n",ans); 31 } 32 return 0; 33 }
原文地址:https://www.cnblogs.com/qldabiaoge/p/8516338.html
时间: 2024-10-19 23:01:04