ACM:SCU 4437 Carries - 水题

本文解析了一道关于计算两数相加进位次数的算法题,通过排序和二分查找的方法,有效地解决了暴力计算可能产生的超时问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SCU 4437  Carries
Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
  Practice 

Description

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 xx and 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 integers a1,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个数,问其中任意两个数相加能够有多少次进位。

例子:1+99=100,个位进位一次,十位进位一次,就是两次;

     50+50 十位进位一次,就是一次。
思维题,比较水的题目,一开始没有想到,想到了觉得好脑残的题目。 暴力会超时,直接sort再用lower_bound去找边界值,加上边界值就OK了。 AC代码: /
*/

#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"stack"
#include"queue"
#include"cmath"
#include"map"
using namespace std;
typedef long long LL ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FK(x) cout<<"["<<x<<"]\n"
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define bigfor(T)  for(int qq=1;qq<= T ;qq++)

const int MX=1e5+1e3;

LL num[MX],temp[MX];

int main(){
	int n;
	while(~scanf("%d",&n)){
	LL ans=0,mod=1;
		for(int i=1;i<=n;i++){
			scanf("%lld",&num[i]);
		}
		for(int i=1;i<=9;i++){
			mod*=10;
			for(int i=1;i<=n;i++)temp[i]=num[i]%mod; //按照个位十位百位千位轮着保存所有的数。 
			sort(temp+1,temp+n+1);//排序 
			for(int i=1;i<=n;i++)ans+=n-(lower_bound(temp+i+1,temp+n+1,mod-temp[i])-temp)+1;
			//如果某个数比此时mod-目标数大,那么这个数加上目标数就会进一位,把这些数个数求和。 
		}
		printf("%lld\n",ans);
	}	
	return 0;
} 
 
      

  

 

 

 

转载于:https://www.cnblogs.com/HDMaxfun/p/5785254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值