java中南大学livelab习题

更新时间:2023-11-24 04:02:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.5

import java.util.*; public class Exercise01_01 {

public static void main(String[] args)

{ System.out.println(9.5*4.5-2.5*3/(45.5-3.5));

} }

1.11(人口预测)基于以下假设美国人口普查局的项目群:

每7秒一诞生 一人死亡每13秒 一个新移民每45秒

编写一个程序,以显示人口为每个未来五年。假设目前的人口为312032486和一年有365天。

1.3 P2numberExchange 将两个变量(a=10,b=20)的值交换,但要求不能使用额外的变量,只能通过简单的运算实现!请在控制台上以‘a=? b=?’的格式输出经过交互后的变量值。

写程序读取0~1000范围之间的整数,并把读取的整数的各个位段上的数字相加 Enter a number between 0 and 1000: 999 The sum of the digits is 27

import java.util.*; public class Exercise02_06 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); System.out.println(\ int n; n=sc.nextInt(); int a,b,c; int m; a=n; b=(n/10); c=n/100; m=a+b+c;

System.out.println(m); } }

根据用户输入的时区信息,输出给定时区的当前时间Enter the time zone offset to GMT: ?5 The current time is 4:50:34 import java.util.Date; import java.util.TimeZone; import java.text.SimpleDateFormat; public class Exercise02_08 {

public static void main(String[] args) {

1

SimpleDateFormat format=new SimpleDateFormat(\ Date date=new Date();

TimeZone zone=TimeZone.getTimeZone(\ format.setTimeZone(zone);

System.out.println(format.format(date)); } }

给定两点坐标,计算两点的欧式距离Enter x1 and y1: 1.5 -3.4 Enter x2 and y2: 4 5 The distance between the two points is 8.764131445842194 import java.util.Scanner; public class Exercise02_15 {

public static void main(String[] args) {

Scanner in=new Scanner(System.in); System.out.println(\ double x1=in.nextDouble(); double y1=in.nextDouble(); System.out.println(\ double x2=in.nextDouble(); double y2=in.nextDouble();

double distance=Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2))); System.out.println(\ } }

(Business: check ISBN-10国际标准书号) An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the other nine digits using the following formula: (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 +d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11

If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer. Here are sample runs:

Enter the first 9 digits of an ISBN as integer: 013601267 The ISBN-10 number is 0136012671

Enter the first 9 digits of an ISBN as integer: 013031997 The ISBN-10 number is 013031997X

import java.util.Scanner; public class Exercise03_09 {

public static void main(String[] args) {

System.out.println(\ Scanner n=new Scanner(System.in); int[] a=new int[9]; int i;

2

int s=0; for(i=0;i<9;i++) {

a[i]=n.nextInt();

s=s+a[i]*(i+1); } s=s; if(s==10) {

for(i=0;i<9;i++) {

System.out.println(a[i]);

}

System.out.println(\ } else {

for(i=0;i<9;i++)

{

System.out.println(a[i]);

}

System.out.println(s);

} }

(Game: heads or tails) Write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1, which represents head or tail. The program rompts the user to enter a guess and reports whether the guess is correct or incorrect.(游戏:正面或反面)写一个程序,让用户猜测是否硬币的结果正面或反面的翻转。程序随机地生成整数0或1,其表示头部或尾部。程序rompts输入的猜测和报告的猜测是正确的还是不正确的用户。 import java.util.Scanner; public class Exercise03_14 {

public static void main(String[] args) { Scanner n=new Scanner(System.in); int in=n.nextInt();

double random=Math.random(); if(in==random)

System.out.println(\}

else } }

(Lottery)Write a program that prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:1. If the user input matches the lottery number in the exact order, the award is $10,000. 2. If all digits in the user input match all digits in the lottery number, the award is $3,000. 3. If one digit in the user input matches a digit in the lottery number, the

System.out.println(\

3

award is $1,000.

(彩票)收件,提示用户输入一个3位数字,并确定用户是否根据下列规则赢得程序: 1.如果用户输入匹配的确切顺序的开奖号码,该奖项是10,000元。

2.如果所有的用户输入的数字匹配所有数字的彩票号码,获奖是3000美元。 3.如果用户输入一个数字在彩票号码的数字相匹配,该奖项是1000美元。 import java.util.Random; import java.util.Scanner; public class Exercise03_15 {

public static void main(String[] args)

{

int sum=0; int[] larr=new int[3]; int[] iarr=new int[3]; Random ra=new Random(); for(int i=0;i<3;i++) { }

System.out.println(\Scanner sc=new Scanner(System.in); for(int i=0;i<3;i++) { }

for(int i=0;i<3;i++) { }

switch(sum) { case 3:

if(larr[0]==iarr[0]&&larr[1]==iarr[1]&&larr[2]==iarr[2]) { } else

System.out.println(\break; for(int j=0;j<3;j++) { }

while(iarr[i]==larr[j]) { }

sum++; break;

iarr[i]=sc.nextInt(); larr[i]=ra.nextInt(10);

4

System.out.println(\

break; case 1:

System.out.println(\

break;

default: }

提示用户输入两个矩形的中心点和长宽信息,并判断第2个矩形是在第1个矩形之内还是与第1个矩形相交 import java.awt.Rectangle; import java.util.Scanner; public class Exercise03_28 {

public static void main(String[] args) }

{ }

Scanner sc=new Scanner(System.in);

System.out.println(\System.out.println(\int x1=sc.nextInt(); int y1=sc.nextInt(); int w1=sc.nextInt(); int h1=sc.nextInt(); int x2=sc.nextInt(); int y2=sc.nextInt(); int w2=sc.nextInt(); int h2=sc.nextInt();

if(Math.abs(x1-x2)>(w1+w2)&&Math.abs(y1-y2)>(h1+h2)) { } else { }

if(Math.abs(x1-x2)<(w1+w2)&&Math.abs(y1-y2)<(w1+w2)) { } else

System.out.println(\

System.out.println(\System.out.println(\

System.out.println(\ break; }

}

5

with n = 50000.

public class Exercise05_23 {

public static void main(String[] args) }

(Display calendars) Write a program that prompts the user to enter the year and first day of the year and displays the calendar table for the year on the console. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the calendar for each month in the year,(显示日历)写程序 提示用户输入年份的一年的第一天,并在控制台上显示今年的年历表的程序。例如,如果用户输入的2013年,和2日,2013年1月1日,你的程序应该显示日历每个月的一年。 import java.util.Scanner; import java.lang.String; class Calendar{ static int year;

int month; int totalday;

public static boolean isleap(int year) {

if((year%4==0&&year0!=0)||(year@0==0)) else }

public static void show(int month) {

switch(month) {

case 1:System.out.println(\ Jan \case 2:System.out.println(\ Feb \case 3:System.out.println(\ Mar \case 4:System.out.println(\ Apr \case 5:System.out.println(\ May \case 6:System.out.println(\ Jun \return false; return true; { }

double sum1=0,sum2=0; for(int i=1;i<=50000;i++) { }

for(int i=50000;i>0;i--) { }

System.out.println(\System.out.println(\

sum2+=1/(double)i; sum1+=1/(double)i;

11

}

public class Exercise05_29 {

public static void main(String[] args)

{

Calendar mycalendar =new Calendar(); System.out.println(\Scanner sc=new Scanner(System.in); int year=sc.nextInt(); Calendar.year=year;

System.out.println(\int weekday=sc.nextInt(); sc.close();

mycalendar.totalday=0; int count=weekday; for(int i=1;i<=12;i++) {

mycalendar.totalday+=mycalendar.monthday(year, i);

}

public int monthday(int year,int month) { }

int days=31; switch(month) { case 4: case 6: case 9: case 11:

days=30; break;

case 7:System.out.println(\ Jul \case 8:System.out.println(\ Aug \case 9:System.out.println(\ Sep \case 10:System.out.println(\ Oct \case 11:System.out.println(\ Nov \case 12:System.out.println(\ Dec \}

case 2: }

return days;

if(isleap(year)) else

days=28; days=29;

12

}

(Perfect number) A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers less than 10,000. Write a program to find all these four numbers.(完美号)的正整数称为一个完美的号码,如果它等于所有的正面的约数,不包括本身的总和。例如,图6是第一完全数,因为6= 3+ 2 +1的下一个是28= 14+ 7+ 4 +2+ 1。有四个完全数小于10,000。编写一个程序来找到所有这四个数字。 public class Exercise05_33 {

public static void main(String[] args)

{

for(int i=2;i<=10000;i++) {

int sum=0;

}

for(int i=1;i<=12;i++) { { }

for(int j=1;j<=mycalendar.monthday(year, i);j++) {

if(j<10)

{

System.out.print(\ \Calendar.show(i);

System.out.println(\

System.out.println(\for(int k=0;k

System.out.print(\

}

System.out.print(j+\ \

if((j+weekday)%7==0)

{

System.out.println();

} count++; }

}

System.out.println(); System.out.println(); weekday=count%7; }

for(int j=1;j

{

if(i%j==0)

13

}

(Computer architecture: bit-level operations) A short value is stored in 16 bits. Write a program that prompts the user to enter a short integer and displays the 16 bits for the integer. 写一个短整数显示它的位数 import java.util.Scanner; public class Exercise05_44 {

public static void main(String[] args) {

System.out.println(\

Scanner sc=new Scanner(System.in); byte num=sc.nextByte(); sc.close();

for(byte j=15;j>=0;j--)

if(((1<

System.out.print(\

}

} {

System.out.println(i);

if(sum==i)

sum+=j;

} }

else

System.out.print(\ }

(Longest common prefix) Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. 写提示用户输入两个字符串的用户并显示两个字符串的最大公共前缀的程序。 import java.util.Scanner; import java.lang.String; public class Exercise05_51 {

public static void main(String[] args)

while(str1.charAt(i)==str2.charAt(i)) {

i++;

{

System.out.println(\Scanner sc=new Scanner(System.in); String str1=sc.nextLine(); String str2=sc.nextLine(); sc.close(); int i=0; }

14

}

}

} if(i>0) else

System.out.println(str1+\System.out.println(\

(Palindrome integer) Write the methods with the following headers 写有下列标题的方法 // Return the reversal of an integer, i.e., reverse(456) returns 654 public static int reverse(int number) // Return true if number is a palindrome

public static boolean isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.

//返回一个整数的反转,即反转(456)返回654 公共静态INT反转(INT数) //返回true,如果号码是回文

公共静态布尔isPalindrome(INT数)

使用反向方法来实现isPalindrome。一个数字是一个回文如果其逆转是一样本身。编写一个测试程序提示用户输入一整数并报告是否整数是一个回文。 import java.util.Scanner; public class Exercise06_03 {

public static int reverse(int number) }

public static boolean isPalindrome(int number) { }

if(number==reverse(number))

return true;

{

String inputStr=Integer.toString(number); String outputStr=\

for(int i=inputStr.length()-1;i>=0;i--) {

outputStr+=inputStr.charAt(i); }

return Integer.parseInt(outputStr);

return false;

public static void main(String[] args) {

System.out.println(\Scanner sc=new Scanner(System.in); int num=sc.nextInt();

15

}

(Game: locker puzzle) A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100. After all the students have passed through the building and changed the lockers,which lockers are open? Write a program to find your answer and display all open locker numbers separated by exactly one space.(Hint: Use an array of 100 Boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.)

学生与储物柜的问题,第一个学生打开所有的储物柜,第二个学生关闭每隔一个储物柜,第三个学生关闭每隔两个储物柜,以此类推,最后一个关闭最后一个储物柜,问最后哪些储物柜是开着的。 public class Exercise07_23 {

public static void main(String[] args) { }

(Sort characters in a string) Write a method that returns a sorted string using the following header: public static String sort(String s) For example, sort(\

Write a test program that prompts the user to enter a string and displays the sorted string.

import java.util.Scanner; public class Exercise07_34 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); System.out.println(\String inputStr=sc.nextLine(); }

for(int i=1;i<=100;i++) { }

int count=0; for(int j=1;j<=i;j++) { }

if(count%2!=0)

System.out.print(i+\if((i%j)==0)

count++;

}

System.out.println(); } }

if(slot[j]>=(num_ball-i)) else

System.out.print(\System.out.print(\

26

}

(Use the Date class) Write a program that creates a Date object, sets its elapsed time to 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, and 100000000000, and displays the date and time using the toString() method, respectively.运用Data类,使逝去的时间为10000,100000,1000000,10000000,1000000000......显示日期和时间 import java.util.Date; public class Exercise09_03 {

public static void main(String[] args) {

System.out.println(\Date[] date=new Date[8]; date[0]=new Date(10000); date[1]=new Date(100000); date[2]=new Date(1000000); date[3]=new Date(10000000); date[4]=new Date(100000000); }

sc.close();

Character[] charList=new Character[inputStr.length()]; for(int i=0;i

for(int i=0;i

for(int i=0;i

System.out.print(charList[i]); Character currentMin=charList[i]; int currentMinIndex=i;

for(int j=i+1;j

if(currentMinIndex!=i) { }

charList[currentMinIndex]=charList[i]; charList[i]=currentMin;

if(currentMin>charList[j]) { }

currentMin=charList[j]; currentMinIndex=j;

charList[i]=inputStr.charAt(i);

27

}

(Use the Random class) Write a program that creates a Random object with seed 1000 and displays the first 50 random integers between 0 and 100 using the nextInt(100) method.运用RANDOM类,生成1000个数,显示前50个 0-100的整数

public class Exercise09_04 { }

(Stopwatch) Design a class named StopWatch. The class contains:

■ Private data fields startTime and endTime with getter methods.■ A no-arg constructor that initializes startTime with the current time.■ A method named start() that resets the startTime to the current time.■ A method named stop() that sets the endTime to the current time.■ A method named getElapsedTime() that returns the elapsed time for the stopwatch in milliseconds.

Draw the UML diagram for the class and then implement the class. Write a test program that measures the execution time of sorting 100,000 numbers using selection sort.

package homework; import java.util.Date; import java.util.Random; class StopWatch {

private long startTime; }

public static void main(String[] args) { Random rd=new Random(1000); for(int i=0;i<50;i++) { }

System.out.println(rd.nextInt(100));

}

date[5]=new Date(1000000000); date[6]=new Date(10000000000L); date[7]=new Date(100000000000L); for(int i=0;i<8;i++) { }

System.out.println(date[i]+\

private long endTime; StopWatch() {

Date dt=new Date();

this.setStartTime(dt.getTime()); } void start()

28

{

Date dt=new Date();

this.setStartTime(dt.getTime()); } void stop() {

Date dt=new Date();

this.setEndTime(dt.getTime()); }

long getElapsedTime() {

return(this.getEndTime()-this.getStartTime()); } public long getStartTime() { return startTime; }

public void setStartTime(long startTime) { this.startTime = startTime; }

public long getEndTime() { return endTime;

}

public void setEndTime(long endTime) {

this.endTime = endTime; }

}

public class Exercise09_06 {

public static void main(String[] args) { StopWatch watch=new StopWatch(); int n=100000; int[] num=new int[n]; Random rd=new Random(); for(int i=0;i

}

watch.start(); for(int i=0;i

for(int j=i+1;j

29

{ if(max

}

}

if(maxIndex!=i) { num[maxIndex]=num[i]; num[i]=max; }

}

watch.stop(); System.out.println();

System.out.println(watch.getStartTime()); System.out.println(watch.getEndTime()); System.out.println(watch.getElapsedTime());

}

}

30

本文来源:https://www.bwwdw.com/article/8x9t.html

Top