• 蓝桥杯——历年真题之带分数


    问题描写叙述

    100 能够表示为带分数的形式:100 = 3 + 69258 / 714。

    还能够表示为:100 = 82 + 3546 / 197。

    注意特征:带分数中,数字1~9分别出现且仅仅出现一次(不包括0)。

    类似这种带分数。100 有 11 种表示法。

    输入格式

    从标准输入读入一个正整数N (N<1000*1000)

    输出格式

    程序输出该数字用数码1~9不反复不遗漏地组成带分数表示的所有种数。

    注意:不要求输出每一个表示,仅仅统计有多少表示法。

    例子输入1
    100
    例子输出1
    11
    例子输入2
    105
    例子输出2
    6
     
    import java.util.Scanner;
    
    public class Main{
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner scanner = new Scanner(System.in);
    		N = scanner.nextInt();
    		cal(0);
    		System.out.println(count);
    	}
    
    	static int N;
    	static int n = 9;
    	static int count;
    	static int[] iarr = new int[n];
    	static boolean[] vis = new boolean[n];
    
    	static void cal(int p) {
    		if (p == n) {
    			// find
    			String string = "";
    			for (int i : iarr)
    				string += i + "";
    			jud(string);
    			/*
    			 * System.out.print(string); System.out.println();
    			 */
    		} else {
    			for (int i = 0; i < vis.length; i++) {
    				if (!vis[i] ) {
    					vis[i] = true;
    					iarr[p] = i + 1;
    					cal(p + 1);
    					vis[i] = false;
    				}
    			}
    		}
    	}
    
    	static void jud(String string) {
    		int len = (N + "").length();
    		for (int i = 0; i <= len; i++) {
    			int a = Integer.parseInt(string.substring(0, i + 1));
    			if (a < N)
    				for (int j = ((9 - i) / 2 + i); j < 9; j++) {
    					long b = Long.parseLong(string.substring(i + 1, j));
    					long c = Long.parseLong(string.substring(j));
    
    					if (b % c != 0) {
    						continue;
    					} else {
    						if (a + b / c == N) {
    							count++;
    							// System.out.println(a+" "+b+" "+c);
    						}
    					}
    				}
    		}
    	}
    }
    


     

  • 相关阅读:
    java 动态代理
    android中几个很有用的的api
    android 静态和动态设置 Receiver的 android:enabled值
    一个文件查看你选择 Run as Android applications 都干了啥
    ViewStub 的使用
    Linux 常用命令速查
    android自定义View&&简单布局&&回调方法
    西厢记 随笔
    git 命令使用速查手册( 个人版)
    Arraylist源码分析:
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7144309.html
Copyright © 2020-2023  润新知