1) write the program to sort the a[]={30,1,-9,70,25} array from small to large
public class Booktest {
public static void Main (string[] args) {
int a[]={30,1,-9,70,25};
System.out.print ("Array original order:");
for (int i=0;i<a.length;i++) System.out.print (A[i] + "");
for (int i = 0; i < a.length; i++) {
int lowerindex = i;
for (int j = i + 1; j < A.length; J + +)
if (A[j] < A[lowerindex]) Lowerindex = j;
int temp = A[i];
A[i] = A[lowerindex];
A[lowerindex] = temp;
}
System.out.print ("\ n the order of the array after sorting:");
for (int i=0;i<a.length;i++) System.out.print (A[i] + "");
}
}
2) write the program, find all the primes in 2-1000, and output in the format of 5 columns per row.
public class primetest{
public static void Main (String args[]) {
int num=2;
System.out.print (2 + "");
for (int i=3;i<=1000;i+=2) {
Boolean F = true;
for (int j=2;j<i;j++) {
if (i% j = 0) {
F= false;
Break
}
}
if (!f) {continue;}
System.out.print (i + "");
if (num++%5 = = 0) System.out.println ();
}
}
}
3) Write a program that generates a random number between 100 1~6 and 1~6 the probability of each number appearing.
public class Randomtest {
public static void Main (String[]args) {
Int[] Randomnum=new int[100];
Int[] N=new int[6];
Double A;
for (int i=0;i<100;i++) {
A = Math.random () *6;
A = Math.ceil (a);
Randomnum[i] = new Double (a). Intvalue ();
System.out.print (Randomnum[i]);
Switch (Randomnum[i]) {
Case 1:n[0]++; Break
Case 2:n[1]++; Break
Case 3:n[2]++; Break
Case 4:n[3]++; Break
Case 5:n[4]++; Break
Case 6:n[5]++; Break
}
}
System.out.println ();//The following can be changed to loop output
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 1 =" + (n[0]/100.0) *100+ "%");
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 2 =" + (n[1]/100.0) *100+ "%");
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 3 =" + (n[2]/100.0) *100+ "%");
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 4 =" + (n[3]/100.0) *100+ "%");
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 5 =" + (n[4]/100.0) *100+ "%");
SYSTEM.OUT.PRINTLN ("Probability of occurrence of number 6 =" + (n[5]/100.0) *100+ "%");
}
}
4) write the program, beg 1!+2!+3!+...+15!.
public class Factorialsum {
static int f (int x) {
if (x<=0) return 1;
Else
Return x*f (x-1);
}
public static void Main (String[]args) {
int sum=0;
for (int j=1;j<=15;j++)
{
Sum+=f (j);
}
SYSTEM.OUT.PRINTLN (sum);
}
}
5) write the program, respectively, with Do-while and for loop calculation 1+1/2!+1/3!+1/4!+ ... The first 15 items of the and.
For Loop code:
public class For_factorialsum {
static int f (int x) {
if (x<=0) return 1;
Else
Return x*f (x-1);
}
public static void Main (String[]args) {
Double sum=0;
for (int j=1;j<=15;j++)
{
Sum+=1.0/f (j);
}
SYSTEM.OUT.PRINTLN (sum);
}
}
Do-while Loop Code:
public class Dowhile_factorialsum {
static int f (int x) {
if (x<=0) return 1;
Else
Return x*f (x-1);
}
public static void Main (String[]args) {
Double sum=0;
int j=1;
do {
Sum+=1.0/f (j);
j + +;
}
while (J<=15);
SYSTEM.OUT.PRINTLN (sum);
}
}
6) Write a program to sort from large to small using the selection method of the array a[]={20,10,55,40,30,70,60,80,90,100}
Use bubble sort, select sort, and insert Sort method respectively)
public class Sortall {
public static void Main (string[] args) {
int a[]={20,10,55,40,30,70,60,80,90,100};
System.out.println ("-the result of the bubbling sort:");
Maopao (a);
System.out.println ();
System.out.println ("--Select the result of the sorting:");
Xuanze (a);
System.out.println ();
System.out.println ("--the result of inserting a sort:");
Charu (a);
}
Bubble sort
public static void Maopao (int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i + 1; j < X.length; J + +) {
if (X[i] > X[j]) {
int temp = X[i];
X[i] = X[j];
X[J] = temp;
}
}
}
for (int i:x) {
System.out.print (i + "");
}
}
Select sort
public static void Xuanze (int[] x) {
for (int i = 0; i < x.length; i++) {
int lowerindex = i;
Find the smallest index
for (int j = i + 1; j < X.length; J + +) {
if (X[j] < X[lowerindex]) {
Lowerindex = j;
}
}
Exchange
int temp = X[i];
X[i] = X[lowerindex];
X[lowerindex] = temp;
}
for (int i:x) {
System.out.print (i + "");
}
}
Insert Sort
public static void Charu (int[] x) {
for (int i = 1; I starts from 1 because the 1th number is already ordered
for (int j = i; j > 0; J –) {
if (X[j] < x[j-1]) {
int temp = X[j];
X[J] = x[j-1];
X[J-1] = temp;
}
}
}
for (int i:x) {
System.out.print (i + "");
}
}
}
7) Write the program, generate 30 prime numbers, in the order from small to large into the array prime[].
public class Primearray {
public static void Main (String args[]) {
Int[] Primearry=new int[30];
primearry[0]=2;
int num=1;
System.out.print (2 + "");
for (int i=3;i<=1000;i+=2) {
Boolean F = true;
for (int j=2;j<i;j++) {
if (i% j = 0) {
F= false;
Break
}
}
if (!f) {continue;}
Primearry[num++]=i;
System.out.print (i + "");
if (num%5 = = 0) System.out.println ();
if (num==30) break;
}
}
}
8) If a number is exactly equal to the sum of its factors, this number is called the "end number". Write an application and a small application to find all the numbers within 1000.
public class Wanshu {
public static void Main (string[] args) {
int sum=0,i,j;
for (i=1;i<=1000;i++)
{
for (j=1,sum=0;j<i;j++)
{
if (i%j==0)
Sum=sum+j;
}
if (sum==i)
{
System.out.print ("End number:" +i+ "" + "its factor is:");
for (int k=1;k<=sum/2;k++)
{
if (sum%k==0)
System.out.print ("+k");
}
System.out.println ();
}
}
}
}
9) Read several numbers from the keyboard, end with "1", sort from small to large
Import Java.util.Scanner;
public class Sc_num {
public static void Main (string[] args) {
Scanner scanner=new Scanner (system.in);
int scnum=0,i=0;
int []scarry=new int[30];
SYSTEM.OUT.PRINTLN ("Input integer (-1 end):");
while (Scnum!=-1) {
Scarry[i]=scanner.nextint ();
Scnum=scarry[i];
i++;
}
Xuanze (scarry,i-1);
}
Select sort
public static void Xuanze (int[] x,int N) {
for (int i = 0; I <n; i++) {
int lowerindex = i;
for (int j = i + 1; j < N; j + +) {
if (X[j] < X[lowerindex]) {
Lowerindex = j;
}
}
int temp = X[i];
X[i] = X[lowerindex];
X[lowerindex] = temp;
}
for (int i=0;i<n;i++) {
System.out.print (X[i] + "");
}
}
}
Java face question (06)