Skip to content

Commit 740a1a2

Browse files
committed
Refactor C++ solution in 1.cpp by improving code readability, adding a function to calculate the number of digits, and enhancing logic for counting valid pairs A and B based on their digit lengths.
1 parent 034c5e6 commit 740a1a2

1 file changed

Lines changed: 55 additions & 19 deletions

File tree

  • niuke/daily_problem/25_11-21小苯的计算式
Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,82 @@
1-
#include<bits/stdc++.h>
1+
#include <bits/stdc++.h>
22
#define il inline
33
using namespace std;
44

55
#define pb push_back
6-
#define fastio \
7-
ios::sync_with_stdio(false); \
8-
cin.tie(0);
6+
#define fastio \
7+
ios::sync_with_stdio(false); \
8+
cin.tie(0);
99

1010
typedef long long ll;
1111
typedef unsigned long long ull;
1212

13-
const ll N = 5e5+5, mod = 1e9+7, inf = 2e18;
13+
const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18;
1414
const double eps = 1e-9;
1515
const double PI = 3.1415926;
1616

17-
il void solve(){
18-
int n,c;
17+
// 获取数字的位数
18+
int getNumlen(int num)
19+
{
20+
if (num == 0)
21+
{
22+
return 1;
23+
}
24+
int len = 0;
25+
while (num > 0)
26+
{
27+
++len;
28+
num /= 10;
29+
}
30+
return len;
31+
}
32+
33+
il void solve()
34+
{
35+
int n, c;
1936
cin >> n >> c;
20-
n = n - 3;
37+
38+
// 计算 C 的位数,得到 A+B 的位数和
39+
int len_c = getNumlen(c);
40+
n = n - len_c - 2; // 减去 C 的位数和两个符号('+' 和 '=')
41+
42+
// 边界判断:A 和 B 至少各占 1 位
43+
if (n < 2)
44+
{
45+
cout << 0 << endl;
46+
return;
47+
}
48+
2149
int cnt = 0;
22-
for (int i = 0; i < n;++i)
50+
// 枚举 A 从 0 到 C,计算对应的 B,判断位数是否匹配
51+
for (int a = 0; a <= c; ++a)
2352
{
24-
for (int j = 0; j < n-i;++j)
53+
int b = c - a;
54+
if (b < 0)
55+
continue;
56+
57+
int len_a = getNumlen(a);
58+
int len_b = getNumlen(b);
59+
60+
// 检查 A 和 B 的位数之和是否等于目标长度
61+
if (len_a + len_b == n)
2562
{
26-
if(i + j == c)
27-
{
28-
++cnt;
29-
}
63+
++cnt;
3064
}
3165
}
66+
67+
cout << cnt << endl;
3268
}
3369

3470
int main()
3571
{
3672
fastio
37-
38-
int t = 1;
39-
cin >> t;
40-
while(t--)
73+
74+
int t = 1;
75+
// cin >> t;
76+
while (t--)
4177
{
4278
solve();
4379
}
44-
80+
4581
return 0;
4682
}

0 commit comments

Comments
 (0)