Captain_Dong 题解分享 · 2024/4/11
数的分解(结果填空) - 题解
纯暴力解,也可以剪枝优化:利用题设条件:三个数相加等于2019,即a+b+c=2019,c可以用2019-(a+b)算出 ``` #include<bits/stdc++.h> using namespace std; typedef long long ll; ll res; bool check(ll x){ while(x){ if(x%10 == 2 || x%10 == 4) return false; x/=10; } return true; } int main(){ for(int i = 1; i < 2019; i++) for(int j = 1; j < 2019; j++) for(int k = 1; k < 2019; k++){ if(i + j + k == 2019){ //去掉重复的数即 三个数相等的情况 if(i != j && j != k && i != k){ //判断这三个数是否满足题设条件 即 每个正整数都不包含数字 22 和 44 if(check(i) && check(j) && check(k)){ res++; } } } } //注意去重 printf("%lld",res/6); return 0; } ``` 剪枝优化代码 ``` #include<bits/stdc++.h> using namespace std; typedef long long ll; ll res; bool check(ll x){ while(x){ if(x%10 == 2 || x%10 == 4) return false; x/=10; } return true; } int main(){ for(int i = 1; i < 2019; i++) for(int j = 1; j < 2019-i; j++){ int k=2019 - i - j; //剪枝 if(check(i) && check(j) && check(k) && i < j && j < k){ //避免重复计算 res++; } } cout<<res<<endl; return 0; } ``` 继续优化 ``` #include<bits/stdc++.h> using namespace std; typedef long long ll; ll res; bool check(ll x){ while(x){ if(x%10 == 2 || x%10 == 4) return false; x/=10; } return true; } int main(){ for(int i = 1; i < 2019/3; i++) for(int j = 1; j < 2019-i; j++){ // j 的上限为 2019 - i int k=2019 - i - j; //剪枝 if(check(i) && check(j) && check(k) && i < j && j < k){ //避免重复计算 // 确保 i, j, k 都不包含 2 或 4 res++; } } cout<<res<<endl; return 0; } ``` ​
查看全文
0 0 3 0
acmer10 题解分享 · 2024/4/5
数的分解(结果填空) - 题解
``` #include<iostream> #include<cstring> #include<algorithm> using namespace std; bool check(int x) { while(x) { if(x%10==2||x%10==4)return false; x/=10; } return true; } int cnt; int main() { for(int i=1;i<2019;i++) for(int j=1;j<2019-i;j++) if(check(i)&&check(j)&&check(2019-i-j)&&i<j&&j<2019-i-j)cnt++; cout<<cnt<<endl; return 0; } ```
查看全文
0 0 1 0
lixiang 题解分享 · 2024/4/6
数的分解(结果填空) - 题解
``` public class Main { public static void main(String[] args) { int count = 0; int n = 2019; //暴力,使用三重循环 for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { for (int k = 1; k < n; k++) { if (i + j + k == 2019 && i != j && i != k && j != k) { //三数之和为2019,且两两不相等 if (!isTure(i) && !isTure(j) && !isTure(k)) { //三个数都不能包含2 或 4 count++; } } } } } //注意:上述结果只是排序的结果,我们求的是组合的结果 //因为三个数排序有 6 中可能,所以最后的结果要 除6 int result = count / 6; System.out.println(result); } //判断这个数字不能包含2或4 public static boolean isTure(int num) { while (num > 0) { int temp = num % 10; if (temp == 2 || temp == 4) { return true; } num = num / 10; } return false; } } ```
查看全文
0 0 0 2
g666 题解分享 · 2024/4/9
数的分解(结果填空) - 题解
python写的,比较垃圾勿喷 ``` num = int(input()) count = 0 def check(n): # 定义一个判断数字是否含有2和4的函数 a = str(n) flag = False for i in a: if i == '2' or i == '4': flag = True break return flag for i in range(1, num): # 二重循环,三个数分别为i,j,num-i-j if check(i): continue for j in range(1, num - i): if check(j) or check(num - i - j) or i == j or i == num - i - j or j == num - i - j: continue else: count = count + 1 continue count = count / 6 # 得到的结果有重复的,有3!种重复,除6得到不重复的结果 print(count) ```
查看全文
0 0 0 1
yuri 题解分享 · 2024/4/9
数的分解(结果填空) - 题解
``` #include <bits/stdc++.h> using namespace std; int b24(int i) { do { int t = i % 10; if (t == 2 || t == 4) return 0; } while (i /= 10); return 1; } int main() { int cnt = 0; for (int i = 1; i < 2019; ++i) { for (int j = i + 1; j < 2019; ++j) { int k = 2019 - (i + j); if (k > j && b24(i) && b24(j) && b24(k)) { cnt++; } } } cout << cnt << endl; return 0; } ```
查看全文
0 0 0 1
WonderfulD 题解分享 · 2024/4/5
数的分解(结果填空) - 题解
```Java public static void main(String[] args) throws IOException { for (int i = 1; i < 2019; i++) { if ((i + "").contains("2") || (i + "").contains("4")) continue; for (int j = i + 1; j < 2019; j++) { if ((j + "").contains("2") || (j + "").contains("4")) continue; for (int k = j + 1; k < 2019; k++) { if ((k + "").contains("2") || (k + "").contains("4")) continue; if (i + j + k == 2019) count ++; } } } out.print(count); out.flush(); } ``` 填空题?后面的数比前面的大就不重复了 暴力:)
查看全文
0 0 0 1
didhv 题解分享 · 2024/4/4
数的分解(结果填空) - 题解
``` public class 数的分解 { public static void main(String[] args) { int n = 2019; int count = cutcount(n); System.out.println(count); } // 计算将整数n拆分成三个不同的正整数之和的方法数量 public static int cutcount(int n) { int count = 0; // 对于a的取值范围为1到n/3,因为三个数的和最小为n/3 for (int a = 1; a <= n / 3; a++) { // 对于b的取值范围为a+1到(n-a)/2,因为b必须比a大且c必须比b大 for (int b = a + 1; b < n - a - b; b++) { int c = n - a - b; // 检查a、b、c是否都不包含数字2和4 if (!contains(a) && !contains(b) && !contains(c)) { count++; } } } return count; } // 检查一个整数是否包含数字2或4 public static boolean contains(int num) { while (num > 0) { int digit = num % 10; if (digit == 2 || digit == 4) { return true; } num /= 10; } return false; } } ```
查看全文
0 0 0 1
Well 题解分享 · 2024/5/22
数的分解(结果填空) - 题解
``` def contains_2_or_4(num): """检查数字中是否包含2或4""" return '2' in str(num) or '4' in str(num) def generate_valid_numbers(limit): """生成不包含数字2和4的正整数列表""" valid_numbers = [] for i in range(1, limit): if not contains_2_or_4(i): valid_numbers.append(i) return valid_numbers def count_unique_combinations(total): """计算符合条件的组合数量""" valid_numbers = generate_valid_numbers(total) number_set = set(valid_numbers) # 使用集合以便快速查找 count = 0 length = len(valid_numbers) for i in range(length): for j in range(i+1, length): a = valid_numbers[i] b = valid_numbers[j] c = total - a - b # 检查 c 是否有效且大于 b(保证 c 不重复且比 b 大) if c in number_set and c > b: count += 1 return count # 目标和 total_sum = 2019 # 计算结果 result = count_unique_combinations(total_sum) print(result) ```
查看全文
0 0 0 0
4run 题解分享 · 2024/4/18
数的分解(结果填空) - 题解
include using namespace std; bool check(int n){ while(n>0){ if(n%10==2||n%10==4) return false; else n=n/10; } return true; } int ans; int main(){ for(int i=1;i+i+i<=2019;i++){ for(int j=i;i+j+j<=2019;j++){ for(int k=j;k<=2019;k++){ if((i!=j)&&j!=k&&check(i)&&check(j)&&check(k)&&i+k+j==2019){ ans++; } } } } cout<<ans; }
查看全文
0 0 0 0
Heng_Xin 题解分享 · 2024/4/5
数的分解(结果填空) - 题解
可能这样更直观些 ```c++ #include <cstdio> #include <vector> using namespace std; int main() { // 3 个各不相同的正整数之和, 每个正整数都不包含数字 2 , 4 int res = 0; vector<int> arr; for (int i = 1; i < 2019; ++i) { // 先把符合条件的放到arr里面 int tmp = i; bool tag = 1; while (tmp) { int wei = tmp % 10; if (wei == 2 || wei == 4) { tag = 0; break; } tmp /= 10; } if (tag) { arr.push_back(i); } } int n = arr.size(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j + 1; k < n; ++k) { if (arr[i] + arr[j] + arr[k] == 2019) { ++res; } } } } printf("%d\n", res); // 互不相同 return 0; } ```
查看全文
0 0 0 0