package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of students: ");
int num = input.nextInt();
int[] scores = new int[num];
int best = 0;
System.out.print("Enter "+num+"scores: ");
for(int i=0;ibest)
best = scores[i];
}
char[] grades = {'A','B','C','D'};
for(int i=0;i
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter ten integers: ");
int[] nums = new int[10];
for(int i=0;i<10;i++)
nums[i] = input.nextInt();
for(int j=9;j>=0;j--)
System.out.print(nums[j]+" ");
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter ten integers between 1 and 100: ");
Scanner input = new Scanner(System.in);
//定义一个counts数组,用来显示1到100数字的出现次数。
int[] counts = new int[101];
for(int i=0;i<101;i++)
counts[i]=0;
while(true){
int num = input.nextInt();
counts[num]++;
if(num==0)
break;
}
for(int i=1;i<=100;i++){
if(counts[i] !=0 ){
if(counts[i]>1)
System.out.println(i+" occurs "+counts[i]+" times");
else
System.out.println(i+" occurs 1 time");
}
}
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter integers: ");
Scanner input = new Scanner(System.in);
int[] occurs= new int[101];
int sum = 0;
int count = 0;
while(true){
int tmp = input.nextInt();
if(tmp<0)
break;
occurs[tmp]++;
sum += tmp;
count++;
}
double aver = 1.0*sum/count;
int lowerCounts = 0;
for(int i=0;i
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter 10 numbers: ");
Scanner input = new Scanner(System.in);
int[] nums = new int[10];
int dis = 0;
for(int i=0;i<10;i++){
int tmp = input.nextInt();
boolean in = false;
for(int j=0;j
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int[] nums =new int[60];
nums[0]=2;
int count=1;
while(true){
if(count==50)
break;
else{
int base = nums[count-1];
while(true){
base++;
if(isPrime(base,nums,count)){
nums[count]=base;
count++;
break;
}
}
}
}
for(int i=0;iMath.sqrt(num))
break;
if(num%num2[i]==0){
re = false;
break;
}
}
return re;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int[] nums = new int[10];
for(int i=0;i<100;i++)
nums[(int)(Math.random()*10)]++;
for(int i=0;i<10;i++)
System.out.println(i+" occurs "+nums[i]+" times");
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] nums = new double[10];
for(int i=0;i<10;i++)
nums[i]=input.nextDouble();
System.out.println("The average is "+average(nums));
}
public static int average(int[] array){
int sum = 0;
int len = array.length;
//foreach语句
for (int value : array)
sum += value;
return sum/len;
}
public static double average(double[] array){
double sum=0.0;
int len = array.length;
for(double ass : array)
sum+=ass;
return sum/len;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] num = new double[10];
for(int i=0;i<10;i++)
num[i]=input.nextDouble();
System.out.println("The minimum number is "+min(num));
}
public static double min(double[] array){
double num1 = array[0];
for (double num3 : array) {
if (num3 < num1)
num1 = num3;
}
return num1;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] nums=new double[10];
for(int i=0;i<10;i++){
nums[i]=input.nextDouble();
}
System.out.println("The lowest index is "+indexOfSmallestElement(nums));
}
public static int indexOfSmallestElement(double[] array){
int index = 0;
for(int i=0;i
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] nums=new double[10];
for(int i=0;i<10;i++){
nums[i]=input.nextDouble();
}
System.out.printf("The mean is %3.2f\n",mean(nums));
System.out.printf("The standard deviation is %6.5f",deviation(nums));
}
public static double deviation(double[] x){
double sum = 0;
double mean=mean(x);
for(double num2 : x)
sum += Math.pow(num2-mean, 2);
return Math.sqrt(sum/(x.length-1));
}
public static double mean(double[] x){
double sum=0;
for(double num1 : x)
sum +=num1;
return sum/x.length;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] nums=new double[10];
for(int i=0;i<10;i++){
nums[i]=input.nextDouble();
}
reverse(nums);
for(double n:nums)
System.out.print(n+" ");
}
public static double[] reverse(double[] num1){
int len = num1.length;
for(int i=0;i
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
System.out.println("The random number is "+getRandom(1,2,3,4,5,6,7));
}
public static int getRandom(int...numbers){
int ran = (int)(Math.random()*54)+1;
while(inArray(numbers,ran))
ran = (int)(Math.random()*54)+1;
return ran;
}
public static boolean inArray(int[] num1,int num2){
for(int value:num1){
if(value == num2)
return true;
}
return false;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
System.out.print("Enter 5 numbers: ");
Scanner input = new Scanner(System.in);
int[] nums = new int[5];
for(int i=0;i<5;i++)
nums[i]=input.nextInt();
System.out.println("The gcd of the numbers is "+gcd(nums));
}
public static int gcd(int...numbers){
if(numbers.length==1)
return numbers[0];
else if(numbers.length==2)
return gcd(numbers[0],numbers[1]);
else{
int gcd = gcd(numbers[0],numbers[1]);
for(int i=2;i=2)
gkd--;
return gkd;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
System.out.print("Enter 10 numbers: ");
Scanner input = new Scanner(System.in);
int[] num1 = new int[10];
for(int i=0;i<10;i++)
num1[i]=input.nextInt();
int[] num2 = eliminateDuplications(num1);
System.out.print("The distinct numbers are: ");
for(int value:num2)
System.out.print(value+" ");
}
public static int[] eliminateDuplications(int[] list){
int ptr=0;
int len=list.length;
int[] num = new int[len];
for(int value:list){
int occur=0;
for(int j=0;j
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int[] million = createRandomArray();
int kk = (int)(Math.random()*100000);
long startTime = System.nanoTime();
linearSearch(kk,million);
long endTime = System.nanoTime();
long timel = endTime-startTime;
sort(million);
startTime = System.nanoTime();
binarySearch(kk,million);
endTime = System.nanoTime();
long timeb = endTime-startTime;
System.out.println("The time of linear search is "+timel+", and the time of binary search is "+timeb);
}
public static int[] createRandomArray(){
int[] nums = new int[100000];
for(int i=0;i<100000;i++)
nums[i]=(int)(100000*Math.random());
return nums;
}
public static int linearSearch(int key,int[] num){
int jb=-1;
for(int i=0;i=2;i--){
for(int j=0;jnum[j+1]){
int tmp = num[j];
num[j] = num[j+1];
num[j+1]=tmp;
}
}
}
}
public static int binarySearch(int key,int[] num){
int jb=-1;
int high=num.length-1;
int low=0;
while(high>=low){
int mid=(high+low)/2;
if(num[mid]==key)
jb=mid;
else if(num[mid]
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int stuNum = input.nextInt();
String[] str1 = new String[stuNum];
System.out.print("Enter the names of the students: ");
for(int i=0;i=2;j--){
for(int i=0;i
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double[] list = new double[10];
System.out.print("ENter ten numbers: ");
for(int i=0;i<10;i++){
list[i]=input.nextDouble();
}
bubbleSort(list);
System.out.print("After bubble sort: ");
for(int i=0;i<10;i++)
System.out.print(list[i]+" ");
}
public static void bubbleSort(double[] list){
boolean n = false;
for(int i=0;ii;j--){
if(list[j-1]>list[j]){
double tmp = list[j-1];
list[j-1] = list[j];
list[j] = tmp;
n = true;
}
}
if(n == false)
break;
}
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the list: ");
int num = input.nextInt();
int[] str1 = new int[num];
System.out.print("Enter the contents of the list: ");
for(int i=0;ilist[i+1])
return false;
}
}
return true;
}
}
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] str = new double[10];
for(int i=0;i<10;i++)
str[i]=input.nextDouble();
selectionSort(str);
for(int i=0;i<10;i++)
System.out.print(str[i]+" ");
}
public static void selectionSort(double[] list){
for(int i=0;i
}
这个题要求使用命令行来解释,所以需要调用main中的args。
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int sum=0;
for(int i=0;i
}
和上题一样,命令行输入。
package demo;
import java.util.*;
public class diqizhang {
public static void main(String[] args){
int count=0;
String str = args[0];
int len = str.length();
for(int i=0;i='A')
count++;
}
System.out.print("There are "+count+" numbers.");
}
}