- 闫晟淏 的博客
暑假七月DAY3
- @ 2025-7-16 19:15:05
错题:
C
错因:
运用了暴力做法,骗了60。可以进行优化
思路:
输入时将首位为i,末位为j的数存入一个桶数组, 便利桶数组将首位为i末位为j的数弹出,与首位为j末位为i的数相乘,(因为X个数和Y个数组合共有X乘Y种方法)计数变量加X乘Y 最后输出计数变量
主要思想
桶
参考代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll cnt[10][10];
int main(){
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
ll n,c=0;
cin>>n;
for(ll i=1;i<=n;i++){
ll b=i;
while(b>=10) b/=10;
cnt[b][i%10]++;
}
for(ll i=1;i<=9;i++){
for(ll j=1;j<=9;j++){
c+=cnt[i][j]*cnt[j][i];
}
}
cout<<c;
return 0;
}
THE END