With fraction + prime number (JAVA solution)

With score: user login

topic description

100 can be expressed as a fraction: 100 = 3 + 69258 / 714

It can also be expressed as: 100 = 82 + 3546 / 197

Pay attention to the characteristics: in the band fraction, the numbers 1~9 appear and only appear once (excluding 0).

For fractions like this, 100 has 11 representations.

enter description

Read a positive integer N (N<1000×1000) from standard input.

output description

The program outputs the number to form all kinds of numbers represented by fractions with numbers 1~9 without repetition.

Note: It is not required to output every representation, only how many representations there are!

Input and output samples

example

enter

100

output

11

operating limit

  • Maximum running time: 3s
  • Maximum running memory: 64M

code:

import java.util.Scanner;

public class 带分数 {
    static int n;
    static int ans = 0;
    static int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs(0);
        System.out.println(ans);
    }

    private static void dfs(int m) {
        if (m == a.length) {
//            for (int i = 0; i < a.length; i++) {
//                System.out.print(a[i] + " ");
//            }
//            System.out.println();
            for (int i = 0; i < 7; i++) {
                for (int j = i + 1; j < 8; j++) {
                    int a = f(0, i);
                    int b = f(i + 1, j);
                    int c = f(j + 1, 8);
                    if (n * c == a * c + b) {
                        ans++;
                        //System.out.println("" + a + " " + b + " " + c);
                    }
                }
            }
            return;
        }
        for (int i = m; i < a.length; i++) {
            swap(i, m);
            dfs(m + 1);
            swap(i, m);
        }
    }

    private static int f(int i, int j) {
        // TODO Auto-generated method stub
        int res = 0;
        while (i <= j) {
            res = res * 10 + a[i];
            i++;
        }
        return res;
    }

    private static void swap(int n, int m) {
        // TODO Auto-generated method stub
        int t = a[n];
        a[n] = a[m];
        a[m] = t;
    }

}

Prime Number: User Login

topic description

Given a positive integer N, please output the prime numbers within N (not including N) and the number of prime numbers.

enter description

Enter a line containing a positive integer N. 1≤N≤103

output description

There are two lines.

Line 1 contains several prime numbers, each two prime numbers are separated by a space, and the prime numbers are output from small to large.

Line 2 contains an integer representing the number of prime numbers up to N.

Input and output samples

example

enter

10

output

2 3 5 7
4

operating limit

  • Maximum running time: 1s
  • Maximum running memory: 128M

code:

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        int count = 0;
        if(n>=2){
          System.out.print(2+" ");
          count++;
        }
        for(int i = 3;i<n;i++){
          int j=0;
          for(j = 2;j<i;j++){
            if(i%j==0){
              break;
            }
          }
          if(j==i){
            System.out.print(i+" ");
            count++;
          }
        }
        System.out.println();
        System.out.print(count);
        scan.close();
    }
}

The cold water is boiled white mucus soup, neither cold nor hot but warm and cool

Guess you like

Origin blog.csdn.net/zzbzxzzdf/article/details/130552530