# 带分数（Java）

100 可以表示为带分数的形式：100 = 3 + 69258 / 714。

100

11

105

6

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class 带分数 {
static List<List<Integer>> resList = new ArrayList<>();
static int count = 0;

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
permutation(arr, 0);
fun(n);
System.out.println(count);
}

// 1~9的全排列
public static void permutation(int[] arr, int index) {
if (index == arr.length) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
resList.add(list);
return;
}
for (int i = index; i < arr.length; i++) {
swap(arr, index, i);
permutation(arr, index + 1);
swap(arr, index, i);
}
}

public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void fun(int n) {
for (List<Integer> list : resList) {
for (int i = 0; i < 9; i++) {
// 找左边的数
int leftNum = 0;
for (int j = 0; j <= i; j++) {
leftNum = leftNum * 10 + list.get(j);
}
if (leftNum > n - 1) {
continue;
}
// 找右边的数
rightSearch(list, i, n, leftNum);
}
}
}

public static void rightSearch(List<Integer> list, int i, int n, int leftNum) {
int left = i + 1;
for (int j = i + 1; j < list.size() - 1; j++) {
int up = 0;
int down = 0;
int right = left;
while (right <= j) {
up = up * 10 + list.get(right);
right++;
}
while (right < list.size()) {
down = down * 10 + list.get(right);
right++;
}
if (up < down) {
continue;
}
if (leftNum + 1.0 * up / down == n) {
count++;
}
}
}
}


## 总结：将大的问题分解成小的问题，逐步击破

01-08 152

02-09 1万+
03-27 1958
02-27 278
03-04 365
03-30 948
07-13
03-02 281
02-22 384
03-23 314
01-08 392
04-07 921
05-25
05-27 305
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客