frog has
n
integers
a1,a2,…,an
, and she wants to add them pairwise.
Unfortunately, frog is somehow afraid of carries (进位). She defines \emph{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
∑1≤i<j≤nh(ai,aj)
.
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我用字符串做的!但是一直wa,和别人ac的代码一直对比测试数据,答案都对得上!不知道哪里错了!若有人看到,希望能得到指教!
别人的ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#include<bitset>
using namespace std;
const int mmax = 100010;
typedef long long LL;
int a[mmax];
int b[mmax];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int tmp=1;
LL ans=0;
for(int i=0;i<9;i++)
{
tmp*=10;
for(int j=0;j<n;j++)
b[j]=a[j]%tmp;
sort(b,b+n);
int r=n;
for(int j=0;j<n;j++)
{
while( r && b[j]+b[r-1]>=tmp)
r--;
if(r<=j)
ans+=(n-r-1);
else
ans+=(n-r);
}
}
ans/=2;
printf("%lld\n",ans);
}
return 0;
}
我自己wa的代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
typedef long long ll;
using namespace std;
int main()
{
ll n;
while(~scanf("%lld",&n))
{
ll cnt=0;
char ch[13];
char ch1[13];
char ch2[13];
memset(ch,0,sizeof(ch));
memset(ch1,0,sizeof(ch1));
memset(ch2,0,sizeof(ch2));
ll arr[100005];
for(ll i=0;i<n;++i)
cin>>arr[i];
sort(arr,arr+n);
for(ll j=n-1;j>=0;--j)
{
if(j>=1&&arr[j]+arr[j-1]<10)
break;
for(ll i=j-1;i>=0;--i)
{
if(arr[i]+arr[j]<10)
break;
sprintf(ch1,"%lld",arr[i]);
sprintf(ch2,"%lld",arr[j]);
ll lenth1=strlen(ch1);
ll lenth2=strlen(ch2);
ll min_lenth=min(lenth1,lenth2);
ll max_lenth=max(lenth1,lenth2);
ll t=min_lenth-1,k=max_lenth-1;
for(;t>=0;--t,--k)
{
ll ans=ch1[t]-'0'+ch2[k]-'0';
// cout<<ch1[t]-'0'<<" "<<ch2[k]-'0'<<endl;
if(ans>=10)
{
ch2[k-1]+=1;
// cout<<"ch"<<ch2[k-1]-'0'<<" "<<k-1<<endl;
cnt++;
}
}
// cout<<k<<endl;
if(min_lenth!=max_lenth&&ch2[k]-'0'>=10)
{
//cout<<"sdfsdf"<<ch2[k]-'0'<<endl;
cnt++;
}
}
}
cout<<cnt<<endl;
}
return 0;
}