zoukankan      html  css  js  c++  java
  • Carries SCU

    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

    1i<jnh(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 (2n1052≤n≤105). The second line contains nn integersa1,a2,,ana1,a2,…,an. (0ai1090≤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
    ",ans);
    31     }
    32     return 0;
    33 }


  • 相关阅读:
    闭包和this
    闭包与变量
    闭包
    ES6扩展运算符的几个小技巧
    js对象的深拷贝
    js获取当前点击元素的索引
    前端学习指北
    css实现心形图案
    this 知多少
    js数字进制转换
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8516338.html
Copyright © 2011-2022 走看看