《Java语言程序设计与数据结构》编程练习答案(第七章)(一)

《Java语言程序设计与数据结构》编程练习答案(第七章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

7.1

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();
        int[] scores = new int[num];
        int best = 0;
        System.out.print("Enter "+num+" scores: ");
        for(int i=0;i<num;i++)
        {
            scores[i]=input.nextInt();
            if(scores[i]>best)
                best=scores[i];
        }
        char[] grades={'A','B','C','D'};
        for(int i=0;i<num;i++)
            System.out.printf("Student %d score is %d and grade is %c\n",i,scores[i],scores[i]==best?'A':grades[(best-scores[i]-1)/10]);
    }

}

7.2

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter ten integers: ");
        int[] nums = new int[10];
        for(int i=0;i<10;i++)
            nums[i]=input.nextInt();
        for(int i=9;i>=0;i--)
            System.out.print(nums[i]+" ");
    }

}

7.3

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] counts = new int[101];
        for(int i=0;i<101;i++)
            counts[i]=0;
        System.out.print("Enter the integers between 1 and 100: ");
        while(true)
        {
            int tmp=input.nextInt();
            counts[tmp]++;
            if(tmp==0)
                break;
        }
        for(int i=1;i<=100;i++)
        {
            if(counts[i]!=0)
            {
                if(counts[i]>1)
                    System.out.printf("%d occurs %d times\n",i,counts[i]);
                else
                    System.out.printf("%d occurs 1 time\n",i);
            }
        }
    }

}

7.4

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        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 avg = 1.0*sum/count;
        int lower = 0;
        for(int i=0;i<avg;i++)
        {
            lower+=occurs[i];
        }
        System.out.println(lower+" students are lower than average.");
        System.out.println((count-lower)+" students are not lower than average.");
    }

}

7.5

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] ass = new int[12];
        int ptr = 0;
        System.out.print("Enter 10 numbers: ");
        for(int i=0;i<10;i++)
        {
            int tmp=input.nextInt();
            boolean in = false;
            for(int j=0;j<ptr;j++)
            {
                if(ass[j]==tmp)
                {
                    in=true;
                    break;
                }
            }
            if(!in)
            {
                ass[ptr]=tmp;
                ptr++;
            }
        }
        System.out.println("The number of distinct number is "+ptr);
        System.out.print("The distinct numbers are: ");
        for(int i=0;i<ptr;i++)
            System.out.print(ass[i]+" ");
    }

}

7.6

public class book {
    public static void main(String[] args)
    {
        int[] ass =new int[60];
        ass[0]=2;
        int count=1;
        while(true)
        {
            if(count==50)
                break;
            else{
                int base = ass[count-1];
                while(true)
                {
                    base++;
                    if(isPrime(base,ass,count))
                    {
                        ass[count]=base;
                        count++;
                        break;
                    }
                }
            }
        }
        for(int i=0;i<count;i++)
            System.out.print(ass[i]+" ");
    }
    public static boolean isPrime(int num,int[] dicks,int count)
    {
        boolean re = true;
        for(int i=0;i<count;i++)
        {
            if(dicks[i]>Math.sqrt(num))
                break;
            if(num%dicks[i]==0)
            {
                re = false;
                break;
            }
        }
        return re;
    }

}

7.7

public class book {
    public static void main(String[] args)
    {
        int[] ass = new int[10];
        for(int i=0;i<100;i++)
            ass[(int)(Math.random()*10)]++;
        for(int i=0;i<10;i++)
            System.out.printf("%d occurs %d times\n",i,ass[i]);
    }
}

7.8

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();
        System.out.println("The average is "+average(ass));
    }
    public static int average(int[] array)
    {
        int sum = 0;
        int len = array.length;
        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;
    }
}

7.9

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();
        System.out.println("The minimum number is "+min(ass));
    }
    public static double min(double[] array)
    {
        double ss = array[0];
        for (double v : array) {
            if (v < ss)
                ss = v;
        }
        return ss;
    }
}

7.10

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();
        System.out.println("The lowest index is "+indexOfSmallestElement(ass));
    }
    public static int indexOfSmallestElement(double[] array)
    {
        int ss = 0;
        for(int i=0;i<array.length;i++)
        {
            if(array[i]<array[ss])
                ss=i;
        }
        return ss;
    }
}

7.11

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();
        System.out.println("The mean is "+mean(ass));
        System.out.println("The standard deviation is "+deviation(ass));
    }
    public static double deviation(double[] x)
    {
        double sum=0;
        double mean=mean(x);
        for(double v:x)sum+=Math.pow(v-mean,2);
        return Math.sqrt(sum/(x.length-1));
    }
    public static double mean(double[] x)
    {
        double sum=0;
        for (double v : x) sum += v;
        return sum/x.length;
    }
}

7.12

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();
        reverse(ass);
        for(double v:ass) System.out.print(v+" ");
    }
    public static double[] reverse(double[] ass)
    {
        int len = ass.length;
        for(int i=0;i<len/2;i++)
        {
            double tmp = ass[i];
            ass[i]=ass[len-i-1];
            ass[len-i-1]=tmp;
        }
        return ass;
    }
}

7.13

public class book {
    public static void main(String[] args)
    {
        System.out.println("The random number is "+getRandom(1,1,4,5,1,4,1,9,1,9,8,1,0));
    }
    public static int getRandom(int... numbers)
    {
        int jb=(int)(Math.random()*54)+1;
        while(inArray(numbers,jb))
            jb=(int)(Math.random()*54)+1;
        return jb;
    }
    public static boolean inArray (int[] ass,int dick)
    {
        for (int value : ass) {
            if (value == dick)
                return true;
        }
        return false;
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104541078
今日推荐