import java.util.Scanner;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 5 numbers: ");
Scanner input = new Scanner(System.in);
int[] ass = new int[5];
for(int i=0;i<5;i++)
{
ass[i]=input.nextInt();
}
System.out.println("The gcd of the numbers is "+gcd(ass));
}
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<numbers.length;i++)
gcd=gcd(gcd,numbers[i]);
return gcd;
}
}
public static int gcd(int n1,int n2)
{
int gkd = Math.min(n1,n2);
while((!(n1%gkd==0&&n2%gkd==0))&&gkd>=2)
gkd--;
return gkd;
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 10 numbers: ");
Scanner input = new Scanner(System.in);
int[] ass = new int[10];
for(int i=0;i<10;i++)
{
ass[i]=input.nextInt();
}
int[] jj = eliminateDuplications(ass);
System.out.print("The distinct numbers are: ");
for (int value : jj) System.out.print(value + " ");
}
public static int[] eliminateDuplications(int[] list)
{
int ptr = 0;
int len = list.length;
int[] ass = new int[len];
for (int item : list) {
int occur = 0;
for (int j=0;j<ptr;j++) {
if (item == ass[j])
occur++;
}
if (occur == 0) {
ass[ptr] = item;
ptr++;
}
}
int[] dick = new int[ptr];
System.arraycopy(ass, 0, dick, 0, ptr);
return dick;
}
}
public class book {
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[] ass = new int[100000];
for(int i=0;i<100000;i++)
ass[i]=(int)(100000*Math.random());
return ass;
}
public static int linearSearch(int key,int[] ass)
{
int jb=-1;
for(int i=0;i<ass.length;i++)
{
if(ass[i]==key)
{
jb=i;
break;
}
}
return jb;
}
public static void sort(int[] ass)
{
for(int i=ass.length-1;i>=2;i--)
{
for(int j=0;j<i;j++)
{
if(ass[j]>ass[j+1])
{
int tmp = ass[j];
ass[j] = ass[j+1];
ass[j+1]=tmp;
}
}
}
}
public static int binarySearch(int key,int[] ass)
{
int jb=-1;
int high=ass.length-1;
int low=0;
while(high>=low)
{
int mid=(high+low)/2;
if(ass[mid]==key)
jb=mid;
else if(ass[mid]<key)
low=mid+1;
else
high=mid-1;
}
return jb;
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int num = input.nextInt();
String[] ass = new String[num];
System.out.print("Enter the names of the students: ");
for(int i=0;i<num;i++)
ass[i]=input.next();
int[] dick = new int[num];
System.out.print("Enter the scores of the students: ");
for(int i=0;i<num;i++)
dick[i]=input.nextInt();
int[] keys = new int[num];
for(int i=0;i<num;i++)
keys[i]=i;
for(int j=num-1;j>=2;j--)
{
for(int i=0;i<j;i++)
if(dick[i]<dick[i+1])
{
int tmp=keys[i];
keys[i]=keys[i+1];
keys[i+1]=tmp;
}
}
for(int i=0;i<num;i++)
System.out.print(ass[keys[i]]+" ");
}
}
见7.16
import java.util.Scanner;
public class book {
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[] ass = new int[num];
System.out.print("Enter the contents of the list: ");
for(int i=0;i<num;i++)
ass[i]=input.nextInt();
System.out.print("The list has "+num+" integer(s)");
for(int i=0;i<num;i++)
System.out.print(" "+ass[i]);
System.out.print('\n');
if(isSorted(ass))
System.out.println("The list is already sorted");
else
System.out.println("The list is not sorted");
}
public static boolean isSorted(int[] list)
{
int len=list.length;
if(len==1||len==0)
return true;
else
{
for(int i=0;i<len-1;i++)
{
if(list[i]>list[i+1])
return false;
}
}
return true;
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 doubles: ");
double[] ass = new double[10];
for(int i=0;i<10;i++)
ass[i]=input.nextDouble();
selectionSort(ass);
for(int i=0;i<10;i++)
System.out.print(ass[i]+" ");
}
public static void selectionSort(double[] list)
{
for(int i=0;i<list.length-1;i++)
{
double currentMax = list[i];
int currentMaxIndex = i;
for(int j=i+1;j<list.length;j++)
{
if(currentMax<list[j])
{
currentMax=list[j];
currentMaxIndex=j;
}
}
if(currentMaxIndex!=i)
{
list[currentMaxIndex]=list[i];
list[i]=currentMax;
}
}
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
int sum=0;
for(int i=0;i<args.length;i++)
sum+=Integer.parseInt(args[i]);
System.out.println("The sum is "+sum);
}
}
public class book {
public static void main(String[] args)
{
int count=0;
String ass = args[0];
int len = ass.length();
for(int i=0;i<len;i++)
{
if(ass.charAt(i)<='Z'&&ass.charAt(i)>='A')
count++;
}
System.out.print("There are "+count+" numbers.");
}
}
public static void main(String[] args)
{
boolean[] ass = new boolean[101];
for(boolean dick:ass)dick=false;
for(int i=1;i<=100;i++)
{
int base=i;
while(base<=100)
{
ass[base]=!ass[base];
base+=i;
}
}
for(int i=1;i<=100;i++)
{
if(ass[i])
System.out.print(i+" ");
}
}
}
public class book {
public static void main(String[] args)
{
int[] pattern = new int[4];
int countPick = 0;
int countPattern = 0;
while(countPattern<4)
{
int card = (int)(Math.random()*52);
countPick++;
if(pattern[card/13]==0) {
pattern[card / 13] = card;
countPattern++;
}
}
String[] dock = {"Spades","Clubs","Hearts","Diamonds"};
String[] point={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
for(int i=0;i<4;i++)
System.out.printf("%s of %s\n",point[pattern[i]%13],dock[i]);
System.out.println("Number of picks: "+countPick);
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a b c: ");
double[] s = new double[3];
for(int i=0;i<3;i++)
s[i]=input.nextDouble();
double[] r = new double[2];
int ass = solveQuadratic(s,r);
System.out.println(ass +" roots");
for(int i=0;i<ass;i++)
System.out.print(r[i]+" ");
}
public static int solveQuadratic(double[] eqn,double[] roots)
{
double a = eqn[0];
double b = eqn[1];
double c = eqn[2];
double delta = b*b-4*a*c;
if(delta<0)
return 0;
else if (delta==0)
{
roots[0]=-1*b/(2*a);
return 1;
}
else
{
roots[0]=(-1*b+Math.sqrt(delta))/(2*a);
roots[1]=(-1*b-Math.sqrt(delta))/(2*a);
return 2;
}
}
}
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter list1 size and contents: ");
int s1 = input.nextInt();
int[] l1 = new int[s1];
for(int i=0;i<s1;i++)
l1[i]=input.nextInt();
System.out.print("Enter list2 size and contents: ");
int s2 = input.nextInt();
int[] l2 = new int[s2];
for(int i=0;i<s2;i++)
l2[i]=input.nextInt();
if(equals(l1,l2))
System.out.println("Two lists are strictly identical");
else
System.out.println("Two lists are not strictly identical");
}
public static boolean equals(int[] list1,int[] list2)
{
int l1 = list1.length;
int l2 = list2.length;
if(l1!=l2)
return false;
else
{
for(int i=0;i<l1;i++)
{
if(list1[i]!=list2[i])
return false;
}
return true;
}
}
}