Java期末练习(二)

7-1 找出最大的对象 (10 分)

(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下: public static Object max(Comparable[] a) public static Object max(Comparable[] a)

import java.util.*; public class Main{ public static Object max(Comparable[] a) { /// 请填写此部分内容 }

 public static void main(String[] args){
      String[] sArray = new String[5];
      Integer[] intArray = new Integer[5];
      Scanner input = new Scanner(System.in);
      for(int i=0;i

}

所有对象都是Comparable接口的实例。对象在数组中的顺序是由compareTo方法决定的。 编写测试程序,从键盘输入5个字符串和5个整数,创建一个由5个字符串构成的数组、一个由5个整数构成的数组。找出数组中最大的字符串、整数并输出。

请在这里描述输出格式。例如:对每一组输入,在一行中输出A+B的值。

输入样例:

在这里给出一组输入。例如:
Xi'an (输入5个字符串,每行一个)
Beijing
ShangHai
GuangZhou
ShenZhen
8 9 12 7 6 (输入5个整数,以空格分隔)

输出样例:

在这里给出相应的输出。例如:
Max string is Xi'an (输出最大的字符串)
Max integer is 12 (输出最大的整数)
import java.util.*;
public class Main {
public static Object max(Comparable[] a)
{
        Arrays.sort(a);
        return a[4];
}
public static void main(String[]args)
{
    Scanner in=new Scanner(System.in);
    Comparable[] a=new Comparable[5];
    Comparable[] b=new Comparable[5];
    for(int i=0;i<5;i++)
        a[i]=in.next();
    for(int i=0;i<5;i++)
        b[i]=in.nextInt();
    System.out.println("Max string is "+max(a));
    System.out.println("Max integer is "+max(b));
    in.close();
    }
}

7-2 两个巨大素数(质数)的乘积 (10 分)

得到两个巨大素数(质数)的乘积是简单的事,但想从该乘积分解出这两个巨大素数却是国际数学界公认的质因数分解难题。这种单向的数学关系,是不对称加密RSA算法的基本原理。 本题给出两个大素数(128bit位)的乘积和其中一个素数,请你编程求出另一个素数。

输入格式:

44022510695404470886511586569647292146578314354528108825807522926455663589709 (大素数的乘积) 189193782774204832019945226750213439577 (其中一个大素数)

输出格式:

232684764001698545563067004009755869717 (另一个素数)

输入样例:

60883665878129858935918958333091530420746054622405737630613777684610994823161
271963475875372143777333694041058521413

输出样例:

223867067745633357281812540202957589797
import java.math.*;
import java.util.*;

public class Main 
{
	
	public static void main(String []args) 
	{
		Scanner cin = new Scanner(System.in);
		BigInteger a = cin.nextBigInteger();
		BigInteger b = cin.nextBigInteger();
		
		System.out.print(a.divide(b));
		cin.close();
    }
}

7-3 对字符串进行排序输出 (10 分)

给定一个字符串,对该字符串进行排序,请输出排好序的字符串。要求能够连续输入输出的字符串。

输入格式:

在一行输入一个字符串

输出格式:

输出排好序的字符串的序列

输入样例:

fecbad

输出样例:

abcdef
#include
#include
#include 
using namespace std;

int main()
{
	string a;
	getline(cin,a);
	sort(a.begin(),a.end());
	cout << a << endl;
}

7-4 给定一个字符串,判定是否是数字 (10 分)

输出格式:

如果是数字就输出true , 不是数字就输出false.

输入样例:

3.145

输出样例:

true
import java.math.*;
import java.util.*;

public class Main 
{
	public static void main(String []args) 
	{
		Scanner cin = new Scanner(System.in);
		String str = cin.next();
		char[] str_ch = str.toCharArray();
		boolean DotFlag = false; //没有点
		boolean JudegFlag = true; //最终判决
		if( str_ch[0]>'9' ||  str_ch[0]<'0')
			System.out.print("false");
		else
		{
			for(int i=1; i='0') || str_ch[i]=='.' ) //是数字或小数点
				{
					if( DotFlag==false && str_ch[i]=='.' ) //如果出现第一个小数点
					{
						DotFlag = true;
						continue;
					}
					else if( DotFlag==true && str_ch[i]=='.' ) //已经有小数点,又来小数点
					{
						JudegFlag = false;
						break;
					}
				}
				else
				{
					JudegFlag = false;
					break;
				}
			}
			if(JudegFlag == true)
				System.out.print("true");
			else
				System.out.print("false");
		}
		cin.close();
    }
}

7-5 查找电话号码 (10 分)

文件phonebook1.txt中有若干联系人的姓名和电话号码。 高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found. 由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。

输入格式:

高富帅 13312342222 白富美 13412343333 孙悟空 13512345555 唐三藏 13612346666 猪悟能 13712347777 沙悟净 13812348888 noname (表示结束) 唐三藏 (需要查找此人的电话号码)

输出格式:

13612346666 (输出对应的电话号码)

输入样例:

白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精

输出样例:

Not found.
import java.util.*;

public class Main 
{
	public static void main(String []args) 
	{
		Scanner cin = new Scanner(System.in);
		String[] name = new String[1000];
		String[] number = new String[1000];
		int count=0;
		/*if(cin.next().equals("noname"))
			System.out.print(1);
		else
			System.out.print(2);*/
		name[0] = cin.next();
		for( int i=0; false==name[i].equals("noname"); i++ )
		{
			number[i] = cin.next();
			name[i+1] = cin.next();
			//System.out.print(name[i]);
			//System.out.print(number[i]);
			count = i;
		}
		String searchName = cin.next();
		//System.out.print(searchName);
		
		boolean flag=false;
		for( int i=0; i<=count; i++ )
		{
			if(name[i].equals(searchName) == true)
			{
				flag = true;
				System.out.print(number[i]);
				break;
			}
		}
		if( flag == false )
			System.out.print("Not found.");
		cin.close();
    }
}

7-6 求几何形状的面积之和 (10 分)

(求几何形状的面积之和)编写一个方法,求数组中所有几何形状对象的面积之和。方法签名如下: public static double sumArea(shape[] a) 编写测试程序,继承抽象类shape得到圆形类Circle和矩形类Rectangle。 abstract class shape {// 抽象类 /抽象方法 求面积 / public abstract double getArea(); / 抽象方法 求周长 / public abstract double getPerimeter(); } 创建四个对象(两个圆和两个矩形)的数组,然后使用sumArea方法求出它们的总面积。(保留4位小数)

输入格式:

输入 1.1 (第1个圆形的半径) 1.8 (第2个圆形的半径) 2.3 3.8 (第1个矩形的宽和高) 5.9 16.8 (第2个矩形的宽和高)

输出格式:

The total area is 121.8401 (总面积,保留4位小数)

输入样例:

2.18
3.16
2.9 5.76
4.8 9.23

输出样例:

The total area is 107.3088
import java.util.*;
import java.text.DecimalFormat;
public class Main 
{
	public static void main(String []args) 
	{
		Scanner cin = new Scanner(System.in);
		double cir1,cir2,tri1Wid,tri1Hig,tri2Wid,tri2Hig;
		double area;
		cir1 = cin.nextDouble();
		cir2 = cin.nextDouble();
		tri1Wid = cin.nextDouble();
		tri1Hig = cin.nextDouble();
		tri2Wid = cin.nextDouble();
		tri2Hig = cin.nextDouble();
		area =(cir1*cir1*Math.PI)+(cir2*cir2*Math.PI)+tri1Wid*tri1Hig+tri2Wid*tri2Hig;
		DecimalFormat df = new DecimalFormat("0.0000");
		
		System.out.print("The total area is "+df.format(area));
		cin.close();
    }
}

7-7 查找成绩并折算后输出 (10 分)

文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。 Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。

输入格式:

Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 noname (表示结束) Bill

输出格式:

Not found.

输入样例:

Smith  67
Anderson  75
Lewis  83
Cook  58
David  96
noname
Lewis

输出样例:

17.43
import java.util.*;

public class Main 
{
	public static void main(String []args) 
	{
		Scanner cin = new Scanner(System.in);
		String[] name = new String[1000];
		int[] number = new int[1000];
		int count=0;
		name[0] = cin.next();
		for( int i=0; false==name[i].equals("noname"); i++ )
		{
			number[i] = cin.nextInt();
			name[i+1] = cin.next();
			count = i;
		}
		String searchName = cin.next();
		
		boolean flag=false;
		for( int i=0; i<=count; i++ )
		{
			if(name[i].equals(searchName) == true)
			{
				flag = true;
				System.out.print(number[i]*0.21);
				break;
			}
		}
		if( flag == false )
			System.out.print("Not found.");
		cin.close();
    }
}

7-9 求阶乘factorial (10 分)

编程从键盘输入一个整数,计算出阶乘并输出。

输入格式:

输入 39

输出格式:

输出:20397882081197443358640281739902897356800000000

输入样例:

58

输出样例:

2350561331282878571829474910515074683828862318181142924420699914240000000000000
import java.util.Scanner;
import java.math.BigInteger;
public class Main 
{
	public static BigInteger compute(BigInteger number)  
    {  
		BigInteger result=new BigInteger("1");
		BigInteger one=new BigInteger("1");
        for(BigInteger i=number; i.intValue()>0; i=i.subtract(one)) 
            result = result.multiply(i);  
        return result;  
    }  

    public static void main(String[] args)  
    {  
    	Scanner input = new Scanner(System.in);
    	BigInteger val=input.nextBigInteger();
        System.out.println(compute(val)); 
        input.close();
    }  
}

7-10 找素数 (10 分)

请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。

输入格式:

第一个整数为m,第二个整数为n;中间使用空格隔开。例如: 103 3

输出格式:

从小到大输出找到的等于或大于m的n个素数,每个一行。例如: 103 107 109

输入样例:

9223372036854775839 2

输出样例:

9223372036854775907
9223372036854775931
import java.math.BigInteger;
import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner cin = new Scanner(System.in);
        BigInteger begin = cin.nextBigInteger();
        int sum = cin.nextInt();
        int count = 0;
        while(count

7-11 使用公历类GregorianCalendar (10 分)

使用公历类 GregorianCalendar,公历类 GregorianCalendar有方法setTimeInMillis(long);可以用它来设置从1970年1月1日算起的一个特定时间。请编程从键盘输入一个长整型的值,然后输出对应的年、月和日。例如输入:1234567898765,输出:2009-1-14

输入格式:

输入 1234567898765 (毫秒数)

输出格式:

输出 2009-1-14 (输出年、月和日,实际应该是2月,因为Java API 从0开始计算月份)

输入样例:

1450921070108

输出样例:

2015-11-24
import java.util.*;
public class Main
{
    public static void main(String[]args)
    {
        Scanner in=new Scanner(System.in);
        GregorianCalendar g=new GregorianCalendar();
        g.setTimeInMillis(in.nextLong());
        System.out.println(g.get(Calendar.YEAR)+"-"+g.get(Calendar.MONTH)+"-"+g.get(Calendar.DAY_OF_MONTH));
         in.close();
    }
}

7-12 找出最大的对象 (10 分)

(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下: public static Object max(Comparable[] a) 所有对象都是Comparable接口的实例。对象在数组中的顺序是由compareTo方法决定的。 编写测试程序,从键盘输入5个字符串和5个整数,创建一个由5个字符串构成的数组、一个由5个整数构成的数组。找出数组中最大的字符串、整数并输出。

输入格式:

输入 Xi'an (输入5个字符串,每行一个) Beijing ShangHai GuangZhou ShenZhen 8 9 12 7 6 (输入5个整数,以空格分隔)

输出格式:

输出 Max string is Xi'an (输出最大的字符串) Max integer is 12 (输出最大的整数)

输入样例:

France
Japan
German
China
India
6 34 89 168 53

输出样例:

Max string is Japan
Max integer is 168
import java.util.*;
public class Main {
public static Object max(Comparable[] a)
{
        Arrays.sort(a);
        return a[4];
}
public static void main(String[]args)
{
    Scanner in=new Scanner(System.in);
    Comparable[] a=new Comparable[5];
    Comparable[] b=new Comparable[5];
    for(int i=0;i<5;i++)
        a[i]=in.next();
    for(int i=0;i<5;i++)
        b[i]=in.nextInt();
    System.out.println("Max string is "+max(a));
    System.out.println("Max integer is "+max(b));
    in.close();
    }
}

7-13 求解给定字符串的前缀 (10 分)

求解给定字符串的前缀。

输入格式:

输入数目不定的多对字符串,每行两个,以空格分开。 例如: filename filepath Tom Jack

输出格式:

返回两个字符串的最大前缀,例如: The common prefix is file No common prefix

输入样例:

filename filepath
Tom Jack

输出样例:

The common prefix is file
No common prefix
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)  
    {  
    	Scanner input = new Scanner(System.in);
    	while(true)
    	{
    		
    		String str1 = input.next();
        	String str2 = input.next();
        	StringBuffer Buffer_str1 = new StringBuffer(str1);
        	StringBuffer Buffer_str2 = new StringBuffer(str2);
        	
        	CharSequence result = "1";
        	for(int i=1,j=1; i

7-15 找出一个给定字符串某个字符出现的次数 (10 分)

编写一个类,类中有如下的方法,找出一个在给定字符串的某个字符的出现的次数,方法的声明如下: public static int count(String str, char a) 例如, count("Welcome", 'e') returns 2. 编写一个测试程序,特使用户输入字符串和一个字符,按照提示输出其出现的次数。 下面是输入和输出的样例,请严格按照输入输出格式进行。

输入格式:

please input the string and the character.

输出格式:

Then output the the number of occurrences of a specified character in the string.

输入样例:

Welcome e

输出样例:

The number of occurrences is 2.

import java.util.Scanner;

public class Main {
	static public void main(String []args)
	{
		Scanner scan = new Scanner(System.in);	//System.in.read(a);
		String a = scan.next();
		String b = scan.next();
		char[] c=a.toCharArray();
		char[] d =b.toCharArray();
		int count=0;
		for(int i=0;i

7-16 求解给定两个字符串的后缀 (10 分)

给定两个字符串,求解给定字符串的后缀。按照如下的格式进行输出

输入格式:

输入为一对字符串,以空格分开。 例如:

father mather 

输出格式:

返回两个字符串的最大后缀,例如:

The common suffix is ather. 

又如:

输入样例:

Tom Jack

输出样例:

No common suffix.
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)  
    {  
    	Scanner input = new Scanner(System.in);
    	
    	String str1 = input.next();
    	String str2 = input.next();

    	StringBuffer Buffer_str1 = new StringBuffer(str1);
    	StringBuffer Buffer_str2 = new StringBuffer(str2);
    	
    	CharSequence result = "1";
    	for(int i=str1.length()-1,j=str2.length()-1; i>=0&&j>=0; i--,j--)
    		if (  Buffer_str1.subSequence(i, str1.length()).equals(Buffer_str2.subSequence(j, str2.length())) )
    			result = Buffer_str1.subSequence(i, str1.length());
    	if( result.equals("1") )
    		System.out.print("No common suffix.");
    	else
    		System.out.print("The common suffix is " + result + ".");
        input.close();
    }
}

 

你可能感兴趣的:(Java/Android)