50 JAVA basic programming exercises 5

[Procedure 41] Title: There are a bunch of peaches on the beach, and five monkeys come to divide. The first monkey divided the pile of peaches into five parts, and one more. This monkey threw one more into the sea and took one. The second monkey divided the remaining peaches into five equal parts, and one more. It also threw one more into the sea and took one. The third, fourth, and fifth monkeys did so. Yes, how many peaches are there on the beach?

public class Prog41{
       public
static void main(String[] args){
              int
n=fun(0);
              System.out.println(“原来有”+n+“个桃子”);
       }
       public
static int fun(int i) {
              if(i==5)
{
                     return
1;
              }else
{
                     return
fun(i+1)*5+1;
              }
       }
}

[Program 42] Title: 809 * ?? = 800 * ?? + 9 * ?? + 1 where ?? represents the two-digit number, the result of 8 * ?? is a two-digit number, and the result of 9 * ?? is 3 Digits. Find the two digits represented by ??, and the result after 809 * ??.

public class Prog42{
       public
static void main(String[] args){
              int
n = 0;
              boolean
flag = false;
              for(int
i=10;i<100;i++) {
                if(809i==800i+9i+1&&8i<100&&9*i>99){
                    flag
= true;
                    n
= i;
                    break;
                }
              }
              if(flag)
                System.out.println(n);
              else
                System.out.println(“无符合要求的数!”);
       }
}

[Procedure 43] Topic: Find the odd number that can be composed of 0-7.

public class Prog43{
              public static void main(String[] args)
{
              int
i = 7;
              System.out.println(“0—”

  • i + "The odd number that can be composed:");
                  count (i);
           }
           public
    static int count (int num) {
                  if
    (num == 0)
                         return
    0;
                  if
    (num == 1)
                         return
    1;
                  int
    sum = 0;
                  int
    temp = 0;
                  if
    (num% 2 == 0)
                         temp
    = num / 2;
                  else
                         temp
    = num / 2 + 1;
                  sum
    + = temp;
                  System.out.println ("1 digit" + sum + "number, algorithm: even number divided by 2; odd number divided by 2 plus 1");
                  for
    (int i = 0; i < num; i++) {
                         int
    temp1 = temp;
                         int
    temp2 = num - 1;
                         boolean
    boo = true;
                         StringBuffer
    sb = new StringBuffer();
                         for
    (int j = 0; j < i + 1; j++) {
                                sb.append(temp2).append("*");
                                temp1
    = temp2 * temp1;
                                if
    (boo) {
                                       boo
    = false;
                                       continue;
                                }
                                temp2–;
                         }
                         sum
    += temp1;
                         System.out.println((i
      • "Digits" + temp1
  • "One, the algorithm:"
  • sb + temp);
                  }
                  System.out.println ("total" + sum + "number");
                  return
    sum;
           }
    }

[Procedure 44] Topic: An even number can always be expressed as the sum of two prime numbers.

import java.util.Scanner;
public class Prog44{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“输入一个偶数:”);
              int
n=sc.nextInt();
              for(int
i=2;i<n/2+1;i++) {
                     if(isPrime(i)&&isPrime(n-i))
{
                            System.out.println(n+"="+i+"+"+(n-i));
                     }
              }
       }
       public
static boolean isPrime(int n) {
              boolean
flag=true;
              for(int
i=2;i<Math.sqrt(n)+1;i++) {
                     if(n%i==0)
{
                            flag=false;
                     }
              }
              return
flag;
       }
}

[Procedure 45] Topic: Determine whether a prime number can be divided by several 9s

import java.util.Scanner;
public class Prog45 {
       public
static void main (String [] args) {
              Scanner
sc = new Scanner (System.in);
              System.out.println ("Enter a number:");
              int
n = sc.nextInt ();
              int
count = 0;
              int
x = n;
              while (x> 8)
{
                     if (x% 9 == 0)
                     count ++;
                     x / = 9;
              }
              System.out.println (n + “can be ”+ Count +“ Division by 9 ”);
       }
}

[Program 46] Title: Two string concatenation program

public class Prog46{
       public
static void main(String[] args){
              Scanner
sc=new Scanner(System.in);
              System.out.println(“输入两个字符串:”);
              String
s1=sc.nextLine();
              String
s2=sc.nextLine();
              System.out.println(s1+s2);
       }

}

[Program 47] Read the integer value of 7 numbers (1-50), and each time a value is read, the program prints out the number * of the value.

import java.util.Scanner;
public class Prog47{
       public
static void main(String[] args){
              System.out.println(“7个整数(1-50):”);
              Random
random=new Random();
              for(int
i=0;i<7;i++) {
                     int
m=random.nextInt(50);
                     System.out.println(m);
                     for(int
j=0;j<m;j++) {
                            System.out.print("*");
                     }
                     System.out.println();
              }
       }
}

[Procedure 48] Topic: A company uses a public telephone to transfer data. The data is a four-digit integer, which is encrypted during the transfer. The encryption rules are as follows: add 5 to each digit, and then divide the sum by 10 Instead of this number, swap the first and fourth digits, and the second and third digits.

public class Prog48{
public static void main(String[] args){
              Scanner
sc=new Scanner(System.in) ;
              System.out.println(“请输入一个四位数:”);
              int
n=sc.nextInt();
              int
[] a=new int[4];
              for(int
i=a.length-1;i>=0;i–) {
                     a[i]=n%10;
                     n=n/10;
              }
              for(int
i=0;i<a.length;i++) {
                     a[i]+=5;
                     a[i]%=10;
              }
              int
t1=a[0];
              a[0]=a[3];
              a[3]=t1;
              int
t2=a[1];
              a[1]=a[2];
              a[2]=t2;
              for(int
i=0;i<a.length;i++) {
                     System.out.print(a[i]);
              }
       }
}

[Program 49] Topic: Count the number of occurrences of substrings in a string

public class Prog49 {
       public
static void main (String [] args) {
              Scanner
sc = new Scanner (System.in);
              System.out.println ("Please enter a string:");
              String
str = sc.nextLine () ;
              System.out.println ("Please enter a substring:");
              String
s = sc.nextLine ();
              int
index = 0; // Traversing the string
              int
count = 0; // The number of statistics
              for
(index = 0 ; index <str.length () + 1-s.length (); index ++) {
                     if
(str.substring (index, s.length () + index) .equals (s)) {
                            count ++;
                     }
              }
              System.out.println ("Substring repeated" + count + "time");
       }
}

[Procedure 50] Title: There are five students, each student has the results of 3 courses, enter the above data from the keyboard (including student number, name, and results of the three courses), calculate the average score, and compare the original data and The calculated average score is stored in the disk file "stud".

import
java.io. *;
public class
Prog50 {
    // Define student model
    String [] number = new String [5];
    String [] name = new String [5];
    float [] [] grade = new float [5] [3];
    float [] sum = new float [5];
    public static void main (String [] args)
throws Exception {
        Prog50 stud = new Prog50 ();
        stud.input ();
        stud.output ();
    }
    / / Enter the student number, name, grade
    void input () throws IOException {
        BufferedReader br = new
BufferedReader (new InputStreamReader (System.in));
        // Enter the status indicator
        boolean isRecord = true;
        while (isRecord) {
            try {
             
for (int i = 0; i <5; i ++) {
                  System.out.print ("Please enter the student number:");
                  number [i] = br.readLine ();
                  System.out.print ("Please Enter name: ");
                  name [i] = br.readLine ();
                  for (int j = 0; j <3; j ++) {
                      System.out.print (" Please enter the number "+ (j + 1) +" Course results: ");
                      grade [i] [j] = Integer.parseInt (br.readLine ());
                  }
                  System.out.println ();
                  sum [i] = grade [i] [0] + grade [ i] [1] + grade [i] [2];
             
}
               
isRecord = false;
           
} catch (NumberFormatException e) {
                 System.out.println ("Please enter a number!");
         
}
        }
    }
    //输出文件
    void output() throws IOException{
        FileWriter fw = new
FileWriter(“E://java50//stud.txt”);
        BufferedWriter bw = new
BufferedWriter(fw);
        bw.write("No.  "+"Name  "+"grade1  "+"grade2  "+"grade3  “+“average”);
        bw.newLine();
        for(int i=0;i<5;i++){
         
bw.write(number[i]);
         
bw.write(”  “+name[i]);
         
for(int j=0;j<3;j++)
           
bw.write(” 
“+grade[i][j]);
         
bw.write(” 
"+(sum[i]/5));
         
bw.newLine();
        }
        bw.close();
    }
}

Published 11 original articles · praised 0 · visits 192

Guess you like

Origin blog.csdn.net/gj55678/article/details/105460705