题解分享
题解分享简介
凑算式(结果填空) - 题解
纯暴力兄弟们,九层循环,排除各个字母相等的情况。直接暴力~
```
package com.xzy.dashoj.day03;
public class xzy_05_凑算式 {
public static void main(String[] args) {
int count = 0;
for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= 9; b++) {
for (int c = 1; c <= 9; c++) {
for (int d = 1; d <= 9; d++) {
for (int e = 1; e <= 9; e++) {
for (int f = 1; f <= 9; f++) {
for (int g = 1; g <= 9; g++) {
for (int h = 1; h <= 9; h++) {
for (int i = 1; i <= 9; i++) {
if (a != b && a != c && a != d && a != e && a != f && a != g && a != h && a != i &&
b != c && b != d && b != e && b != f && b != g && b != h && b != i &&
c != d && c != e && c != f && c != g && c != h && c != i &&
d != e && d != f && d != g && d != h && d != i && e != f && e != g && e != h && e != i &&
f != g && f != h && f != i && g != h && g != i && h != i) {
int shang = d * 100 + e * 10 + f;
int xia = g * 100 + h * 10 + i;
if (a * c * xia + b * xia + shang * c == 10 * c * xia) {
count++;
}
}
}
}
}
}
}
}
}
}
}
System.out.println(count);
}
}
```
查看全文
0
0
17
6
凑算式(结果填空) - 题解
方法一:DFS 全排列
```
#include<bits/stdc++.h>
using namespace std;
const int N=15;
bool used[N]; //记录该数字是否被使用过
int num[N]; //保存全排列的结果
int cnt; //计数
void dfs(int n){
if(n==10){
int A = num[1];
int B = num[2];
int C = num[3];
int DEF = num[4] * 100 + num[5] * 10 + num[6];
int GHI = num[7] * 100 + num[8] * 10 + num[9];
if(A * C * GHI + B * GHI + DEF * C == 10 * C * GHI){
cnt++;
return;
}
}
//搜索 全排列1~9
for(int i = 1;i <= 9; i++){
if(!used[i]){
used[i] = true;
num[n] = i;
dfs(n+1);
used[i] = false; //恢复现场
}
}
}
int main(){
dfs(1);
cout<<cnt<<endl;
return 0;
}
```
方法二: 利用STL 函数:next_permutation()
```
`
#include<bits/stdc++.h>
using namespace std;
int num[]={1,2,3,4,5,6,7,8,9};
int A,B,C,DEF,GHI;
int cnt;
int main(){
while(next_permutation(num,num+9)){
A=num[0]; B=num[1]; C=num[2];
DEF=num[3]*100+num[4]*10+num[5];
GHI=num[6]*100+num[7]*10+num[8];
if(A * C * GHI + B * GHI + DEF * C == 10 * C * GHI){
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}
```
`
查看全文
0
0
3
2
凑算式(结果填空) - 题解
import java.util.Scanner;
public class Main {
```
static int[] num = new int[10]; //存储数组
static int ans = 0;
static boolean[] v=new boolean[10]; //标记数组
public static void main(String[] args) {
dfs(1);
System.out.println(ans);
}
public static void dfs(int step) {
//终止状态
if(step>9) {
int A=num[1];
int B=num[2];
int C=num[3];
int DEF=num[4]*100+num[5]*10+num[6];
int GHI=num[7]*100+num[8]*10+num[9];
if(A*C*GHI+B*GHI+DEF*C==10*C*GHI) {
ans++;
return;
}
}
//寻找新状态
for(int i=1;i<10;i++) {
if(v[i]==true) {
continue; //true为走过
}
v[i]=true; //标记为走过
//加入队列中
num[step]=i;
dfs(step+1);
//回溯
v[i]=false;
num[step]=0;
}
}
```
}
查看全文
0
0
3
2
凑算式(结果填空) - 题解
1.注意题目描述出错,数字范围是1到9而不是0到9,这东西卡我1个多小时。
2.那个DEF是一个三位数而不是DEF
因为涉及到浮点运算可能会丢失精度,所以我们把除法式子先换成乘法式子(此题中不存在分母为0的情况)
include
define int long long
using namespace std;
int a[10],ans=0;
bool vis[10]={false};
bool check()
{
int h1=a[3]100+a[4]10+a[5],h2=a[6]100+a[7]10+a[8];
if(a[0]a[2]h2+a[1]h2+h1a[2]==10a[2]h2)
return true;
return false;
}
void dfs(int i)
{
if(i==9)
{
if(check())
ans++;
return;
}
for(int t=1;t<10;t++)
{
if(!vis[t])
{
a[i]=t;
vis[t]=true;
dfs(i+1);
vis[t]=false;
}
}
}
signed main()
{
dfs(0);
cout <<ans<< endl;
return 0;
}
查看全文
0
0
0
2
凑算式(结果填空) - 题解
include
include
using namespace std;
int num;
int arr[15], st[15];
void dfs(int x) {
if (x == 10) {
int n = arr[4] 100 + arr[5] 10 + arr[6];
int m = arr[7] 100 + arr[8] 10 + arr[9];
if (arr[1]arr[3]m + arr[2]m + n arr[3] == 10 arr[3]m)
num++;
return ;
}
for (int i = 1; i <= 9; i++) {
if (st[i])
continue;
st[i] = 1;
arr[x] = i;
dfs(x + 1);
st[i] = 0;
```
}
```
}
int main () {
dfs(1);
cout << num;
return 0;
}
查看全文
0
0
0
2
凑算式(结果填空) - 题解
> 你说得对,但这就是next_premutation,后面忘了
```
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
double a[] = { 1,2,3,4,5,6,7,8,9 };//注意别开int,涉及到除法
ll res = 0;
do
{
if ((a[0] + a[1] / a[2] + (a[3] * 100 + a[4] * 10 + a[5]) / (a[6] * 100 + a[7] * 10 + a[8])) == 10)
++res;
} while (next_permutation(a,a+9));
cout << res;
return 0;
}
```
查看全文
0
0
0
2
凑算式(结果填空) - 题解
include
using namespace std;
int ans, n ;
int a[20];
bool b[20];
void dfs(int n) {
if (n == 10) {
int x = a[4] 100 + a[5] 10 + a[6];
int y = a[7] 100 + a[8] 10 + a[9];
if (a[1]a[3]y + a[2]y + x a[3] == 10 a[3]y) {
ans++;
return;
}
}
for (int i = 1; i < 10; i++) {
if (b[i] == 1)
continue;
a[n] = i;
b[i] = 1;
dfs(n + 1);
b[i] = 0;
}
}
int main() {
dfs(1);
cout << ans;
return 0;
}
查看全文
0
0
0
2
凑算式(结果填空) - 题解
```
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 15;
int path[N];
bool st[N];
int n, res;
void dfs(int u)
{
if(u > n)
{
for (int i = 1; i <= n; i ++ )
{
int a = path[4] * 100 + path[5] * 10 + path[6];
int b = path[7] * 100 + path[8] * 10 + path[9];
if(path[1] * path[3] * b + path[2] * b + a * path[3] == 10 * path[3] * b)
{
res++;
return;
}
}
}
for (int i = 1; i <= n; i ++ )
{
if(st[i] == false)
{
st[i] = true;
path[u] = i;
dfs(u + 1);
st[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << res;
return 0;
}
```
查看全文
0
0
0
1
凑算式(结果填空) - 题解
```
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
using namespace std;
vector<int> path;
int used[10];
int res;
void dfs(int count){
if(count>9){
int x = path[4] * 100 + path[5] * 10 + path[6];
int y = path[7] * 100 + path[8] * 10 + path[9];
// 判断是否满足条件
if (path[1] * path[3] * y + path[2] * y + x * path[3] == 10* path[3] * y){
res++; // 满足条件的排列数量加一
return;
}
}
// 处理单层递归的逻辑
for(int i=1;i<=9;i++){
if(used[i]) continue;
path.push_back(i);
used[i]=1;
dfs(count+1);
used[i]=0;
path.pop_back();
}
}
int main(){
path.push_back(0);
dfs(1);
cout<<res;
return 0;
}
```
查看全文
0
0
0
1
凑算式(结果填空) - 题解
```Java
static int[] ans;
static int[] visited;
static int count;
public static void main(String[] args) throws IOException {
ans = new int[10];
visited = new int[10];
dfs(1);
System.out.println(count);
}
public static void dfs(int index) {
if (index == ans.length) {
if ((double)ans[1] + (double) ans[2] / ans[3] + (double) (ans[4] * 100 + ans[5] * 10 + ans[6]) / (ans[7] * 100 + ans[8] * 10 + ans[9]) == 10.0) {
count ++;
}
}else {
for (int i = 1; i < 10; i++) {
if (visited[i] == 0) {
visited[i] = 1;
ans[index] = i;
dfs(index + 1);
visited[i] = 0;
}
}
}
}
```
查看全文
0
0
0
1



