java语言程序设计基础篇第五章编程练习题

1

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double sum = 0;
		int total;
		int positive = 0;
		int negative = 0;
		while(true){
			double num = input.nextDouble();
			if(num == 0)
				break;
			sum += num;
			if(num > 0)
				++positive;
			else if(num < 0)
				++negative;
		}
		System.out.println(positive + "\n" + negative + "\n" + sum + "\n" + sum / (positive+negative));
	}
}

2

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		final int NUMBER_OF_QUESTIONS = 10;
		int correctCount = 0;
		int count = 0;
		long startTime = System.currentTimeMillis();
		String output = " ";
		Scanner input = new Scanner(System.in);
		
		while(count < NUMBER_OF_QUESTIONS){
			int number1 = (int)(Math.random()*15+1);
			int number2 = (int)(Math.random()*15+1);
			
			System.out.print("What is " + number1 + " + " + number2 + " ? ");
			int answer = input.nextInt();
			if(number1 + number2 == answer){
				System.out.println("You are correct!");
				++correctCount;
			}
			else
				System.out.println("Your answer is wrong.\n" + number1 + " + " + number2 
						+ " should be " + (number1+number2));
			++count;
			
			output += "\n" + number1 + " + " + number2 + " = " + answer + 
					((number1 + number2 == answer) ? " correct" : " wrong");
		}
		long endTime = System.currentTimeMillis();
		long testTime = endTime - startTime;
		
		System.out.println("Correct count is " + correctCount + "\nTest time is " + testTime/1000
				+ " seconds\n" + output);
	}
}

3
public class Main {
	
	public static void main(String[] args){
		System.out.println("千克                    磅");
		for(int i = 1; i < 200; ++i){
			if(i%2 == 1)
				System.out.println(i + "          " + i*2.2);
		}
	}
} 
4 5 6 
同上
7

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		double base = 10000.0;
		double num = base;
		for(int i = 0; i < 10; ++i){
			num += num*0.05;
		}
		double tuition = num;
		double sum = 0.0;
		
		//我从第十一年开始算的
		num += num*0.05;
		for(int i = 0; i < 4; ++i){
			sum += num;
			num += num*0.05;
		}
		System.out.println(tuition);
		System.out.println(sum);
	}
}

8

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		System.out.println("Enter information about ten students");
		String[] name = new String[10];
		double[] score = new double[10];
		Scanner input = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			name[i] = input.next();
			score[i] = input.nextDouble();
		}
		
		for(int i = 0; i < 10; ++i){
			System.out.println(name[i] + ":" + score[i]);
		}
	}
}

9

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		System.out.println("Enter information about ten students");
		String[] name = new String[10];
		double[] score = new double[10];
		Scanner input = new Scanner(System.in);
		for(int i = 0; i < 10; ++i){
			name[i] = input.next();
			score[i] = input.nextDouble();
		}
		
		double max;
		int temp = 0;
		//选择排序法
		for(int i = 0; i < 9; ++i){
			max = score[i];
			temp = i;
			for(int j = i + 1; j < 10; ++j){
				if(score[j] > max){
					max = score[j];
					temp = j;
				}
			}
			
			double tp = score[i];
			score[i] = score[temp];
			score[temp] = tp;
			
			String tstr = name[i];
			name[i] = name[temp];
			name[temp] = tstr;
		}
		
		for(int i = 0; i < 2; ++i)
			System.out.println(name[i] + " : " +score[i]);
	}
}

10

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int num = 0;
		for(int i = 1; i <= 1000; ++i){
			if(i%5 == 0 && i%6 == 0){
				System.out.print(i + " ");
				++num;
				if(num%10 == 0)
					System.out.println();
			}
		}
	}
}

11

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int num = 0;
		for(int i = 1; i <= 1000; ++i){
			if(i%5 == 0 ^ i%6 == 0){
				System.out.print(i + " ");
				++num;
				if(num%10 == 0)
					System.out.println();
			}
		}
	}
}

12

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int n = 1;
		while(true){
			if(n*n > 12000)
				break;
			++n;
		}
		System.out.println(n);
	}
}

13

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int n = 1;
		while(true){
			if(n*n > 12000)
				break;
			++n;
		}
		System.out.println(n-1);
	}
}

14

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter two positive integers");
		int num1 = input.nextInt();
		int num2 = input.nextInt();
		int t = 0;
		//辗转相除法
		while(true){
			t = num1 % num2;
			if(t == 0)
				break;
			num1 = num2;
			num2 = t;
		}
		System.out.println(num2);
	}
}

15

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int start = (int)'!';
		int end = (int)'~';
		for(int i = start; i <= end; ++i){
			System.out.print((char)i + " ");
			if((i-start+1)%10 == 0)
				System.out.println();
		}
	}
}

16

import java.util.Scanner;

public class Main{
	//素数表
	static int[] primeNumber = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,
			83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,
			193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,
			313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,
			443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,
			587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,
			719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,
			859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997};
	
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int num = input.nextInt();
		int renum = num;
		int[] nums = new int[100];
		int i = 0;
		int cur = 0;
		//测试的时候不要输入素数
		//不要害怕找不到结果,因为有下边那句话
		//唯一分解定理:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积
		while(true){
			if(num > primeNumber[i] && renum%primeNumber[i] == 0){
				nums[cur] = primeNumber[i];
				++cur;
				renum = renum / primeNumber[i];
				if(renum == 1)
					break;
			}
			else if(renum%primeNumber[i] != 0){
				++i;
			}
		}
		for(int j = 0; j < cur; ++j)
			System.out.print(nums[j] + " ");
		
	}
}

17 18 19 
.....................虐人的循环语句大集合-----博客链接:http://www.dgcontinent.com/?p=112

20

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int time = 0;
		for(int i = 2; i <= 1000; ++i){
			if(isPrimeNumber(i)){
				System.out.print(i + " ");
				++time;
				if(time % 8 == 0)
					System.out.println();
			}
		}
	}
	
	static boolean isPrimeNumber(int n){
		for(int i = 2; i*i <= n; ++i){
			if(n % i == 0)
				return false;
		}
		return true;
	}
}

21

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Loan Account:");
		double account = input.nextDouble();
		System.out.print("Number of Years:");
		int year = input.nextInt();
		System.out.println("Interests Rate        Monthly Payment        Total Payment");
		double srate = 0.05;
		double mrate = 0.05/12;
		double erate = 0.08;
		double increase = 0.01/8.0;
		int acc = 0;
		while(true){
			++acc;
			double monthlyPayment = (account*mrate)/(1-1/Math.pow(1+mrate, year*12));
			double totalPayment = monthlyPayment*12.0*year;
			System.out.printf("%.3f%%                %.2f                 %.2f\n",srate*100,monthlyPayment,totalPayment);
			srate += increase;
			mrate = srate/12;
			if(acc == 25)
				break;
		}
	}
}

22

参考上一题

23

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int n = 50000;
		double sum1 = 0.0;
		double sum2 = 0.0;
		while(n > 0){
			sum1 += 1.0/n;
			n--;
		}
		n = 50000;
		for(int i = 1; i <= n; ++i){
			sum2 += 1.0/i;
		}
		
		System.out.println("From right to left:" + sum1);
		System.out.println("From left to right:" + sum2);
	}
}

24

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int numerator = 1;
		int denominator = 3;
		double sum = 0.0;
		while(true){
			sum += (double)numerator/(double)denominator;
			if(denominator == 99)
				break;
			numerator += 2;
			denominator += 2;
		}
		System.out.println(sum);
	}
}

25

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double pi = 0.0;
		double[] nums = new double[10]; 
		double molecule = 1.0;
		for(int i = 1; i <= 100000; ++i){
			pi += molecule/(2*i-1);
			molecule = -molecule;
			if(i%10000 == 0){
				nums[(i/10000)-1] = 4*pi;
			}
		}
		
		for(int i = 0; i < 10; ++i)
			System.out.println(nums[i]);
	}
}

26

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		double e = 0.0;
		double factor = 1.0;
		double[] nums = new double[10];
		for(int i = 1; i <= 100000; ++i){
			e += factor;
			factor /= i;
			if(i % 10000 == 0)
				nums[i/10000-1] = e;
		}
		
		for(int i = 0; i < 10; ++i)
			System.out.println(nums[i]);
	}
}

27

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int cun = 0;
		for(int i = 101; i <= 2100; ++i){
			if(isLeapYear(i)){
				++cun;
				System.out.print(i + " ");
				if(cun % 10 == 0)
					System.out.println();
			}
		}
		System.out.println("\nThe number of leap year:" + cun);
	}
	
	static boolean isLeapYear(int n){
		if((n%400 == 0) || (n%4 == 0 && n%100 != 0))
			return true;
		return false;
	}
}
28

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
		String[] weeks = {"Sunday","Monday","Tudeday","Wednesday","Thursday","Friday","Saturday"};
		String[] months = {"January","February","March","April","May" ,"June","July","August","September","October","November","December"};
		Scanner cin = new Scanner(System.in);
		int year = cin.nextInt();
		int week = cin.nextInt();
		if(isLeapYear(year) && days[1] == 28)
			++days[1];
		if(!isLeapYear(year) && days[1] == 29)
			--days[1];
		int sum = 1;
		for(int i = 0; i < 12; ++i){
			System.out.println(months[i] + " " + "1," + year +" is " 
		+ weeks[(sum-1+week)%7]);
			sum += days[i];
		}
	}
	
	static boolean isLeapYear(int n){
		if((n%400 == 0) || (n%4 == 0 && n%100 != 0))
			return true;
		return false;
	}
}

29.
import java.util.*;


public class Main {
    
    public static void main(String[] args){
        CalendarBean cb = new CalendarBean(2016,12);
        String[] a = cb.getCalendar();
        for(int i = 0; i < a.length; ++i){
            if(i%7 == 0)
                System.out.println();
            System.out.printf("%4s", a[i]);
        }
    }
}

class CalendarBean{
    int year = 0;
    int month = 0;
    
    public CalendarBean(){
        this.year = this.month = 0;
    }
    
    public CalendarBean(int year, int month){
        this.year = year;
        this.month = month;
    }
    
    public void setYear(int year){
        this.year = year;
    }
    
    public void setMonth(int month){
        this.month = month;
    }
    
    public String[] getCalendar(){
        String[] a = new String[42];
        Calendar rili = Calendar.getInstance();
        rili.set(year,month-1, 1);
        int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1;
        int day = 0;
        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 1)
            day = 31;
        if(month == 4 || month == 6 || month == 9 || month == 12)
            day = 30;
        if(month == 2){
            if((year%4 == 0 && year%100 != 0) || (year%400 == 0))
                day = 29;
            else
                day = 28;
        }
        for(int i = 0; i < weekDay; ++i)
            a[i] = " ";
        for(int i = weekDay, n = 1; i < weekDay + day; ++i){
            a[i] = String.valueOf(n);
            ++n;
        }
        for(int i = weekDay+day; i < a.length; ++i)
            a[i] = " ";
        return a;
    }
}

30
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		double money = cin.nextDouble();
		double interestRate = cin.nextDouble();
		int month = cin.nextInt();
		interestRate = interestRate / 100.0;
		interestRate = interestRate / 12.0;
		for(int i = 0; i < month; ++i){
			if(i != 0) money += 100;
			money = money + money*interestRate;
		}
		System.out.println(money);
	}
}

31
同上
32
程序清单3-8
33
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		for(int i = 6; i <= 10000; ++i){
			double flag = 0;
			for(int f = 1; f <= Math.sqrt(i); ++f){
				if(i%f == 0){
					flag += f;
					if(i/f != i)
						flag += i/f;
				}
			}
			if(flag == i)
				System.out.println(i);
		}
	}
}

34
编程练习题3.17
35
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		double res = 0;
		for(int i = 2; i <= 625; ++i){
			res += 1/(Math.sqrt(i-1) + Math.sqrt(i));
		}
		System.out.println(res);
	}
}

36
编程练习题3.9
37
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int num = cin.nextInt();
		int[] arr = new int[64];
		int len = 0;
		while(num != 0){
			arr[len++] = num%2;
			num = num/2;
		}
		for(int i = len-1; i >= 0; --i){
			System.out.print(arr[i]);
		}
	}
}

38
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int num = cin.nextInt();
		int[] arr = new int[64];
		int len = 0;
		while(num != 0){
			arr[len++] = num%8;
			num = num/8;
		}
		for(int i = len-1; i >= 0; --i){
			System.out.print(arr[i]);
		}
	}
}

39

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		double one = 5000;
		double two = 5000;
		double goal = 30000;
		double res = (goal - one*0.08 - two*0.1)/0.12;
		System.out.println(res);
	}
}

40
。。
41

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int max = Integer.MIN_VALUE;
		int count = 0;
		int num = 0;
		while(cin.hasNext()){
			num = cin.nextInt();
			if(num == 0) break;
			if(num == max){ 
				++count;
				continue;
			}
			if(num > max){ 
				max = num;
				count = 1;
			}
		}
		System.out.println(count);
	}
}

42
。。
43

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int cnt = 0;
		for(int i = 1; i <= 7; ++i){
			for(int j = i+1; j <= 7; ++j){
				System.out.println(i + "    " + j);
				++cnt;
			}
		}
		System.out.println("----" + cnt + "----");
	}
}

44

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int num = cin.nextInt();
		for(int i = 15; i >= 0; --i){
			if((num & (1 << i)) != 0)
				System.out.print(1);
			else
				System.out.print(0);
		}
		System.out.println();
	}
}

45
。。
46
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		String str = cin.nextLine();
		StringBuffer strBuf = new StringBuffer(str);
		System.out.println(strBuf.reverse());
	}
}
47
。。

48

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		String str = cin.nextLine();
		
		for(int i = 0; i < str.length(); ++i){
			if(i%2 == 0)
				System.out.print(str.charAt(i));
		}
		System.out.println();
	}
}

49

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter a string:");
		String str = cin.nextLine();
		int len = str.length();
		int vowel = 0;
		int consonant = 0;
		for(int i = 0; i < len; ++i){
			String s = str.charAt(i) + "";
			if(s.equalsIgnoreCase("A") || s.equalsIgnoreCase("E") 
					|| s.equalsIgnoreCase("I") || s.equalsIgnoreCase("O")
					|| s.equalsIgnoreCase("U")){
				++vowel;
			}else{
				++consonant;
			}
		}
		System.out.println("vowel:" + vowel);
		System.out.println("consonant:" + consonant);
	}
}
50

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter a string:");
		String str = cin.nextLine();
		int len = str.length();
		int num = 0;
		for(int i = 0; i < len; ++i){
			Character ch = str.charAt(i);
			if(ch.isUpperCase(ch)) ++num;
		}
		System.out.println("uppercase:" + num);
	}
}
51

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter the first string:");
		String str1 = cin.nextLine();
		System.out.print("Enter the second string:");
		String str2 = cin.nextLine();
		int i = 0;
		int j = 0;
		int len = 0;
		int len1 = str1.length();
		int len2 = str2.length();
		while(i < len1 && j < len2){
			if(str1.charAt(i) == str2.charAt(j)){
				++len;
				++i;
				++j;
			}
			else
				break;
		}
		if(len == 0){
			System.out.println("no common prefix");
		}else{
			for(i = 0; i < len; ++i)
				System.out.print(str1.charAt(i));
			System.out.println();
		}
	}
}
 

你可能感兴趣的:(Java学习)