java第十五周练习题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
相同的数组。如果两个数组list1和list2的内容相同,那么就说它们是相同的。使用下面的程序可以判断两个数组是否相同,请填空补全程序。


import java.util.*;
public class Main {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      int size1 = input.nextInt();
      int[] list1 = new int[size1];
      // Enter values for list1
      for (int i = 0; i < list1.length; i++)
         list1[i] = input.nextInt();
      int size2 = input.nextInt();
      int[] list2 = new int[size2];
      // Enter values for list2
      for (int i = 0; i < list2.length; i++)
         list2[i] = input.nextInt();
      input.close();
      if (equals(list1,list2)) {
         System.out.println("Two lists are identical");
      } else {
         System.out.println("Two lists are not identical");
      }
   }
   public static boolean equals(int[] list1, int[] list2) {
      if (list1.length!=list2.length)
         return false;
      Arrays.sort(list1);
      Arrays.sort(list2);
      for (int i = 0; i < list1.length; i++)
         if (list1[i] != list2[i])
            return false;
      return true;
   }
}

7-1 对字符串进行排序输出 (10分)
给定一个字符串,对该字符串进行排序,请输出排好序的字符串。要求能够连续输入输出的字符串。

输入格式:
在一行输入一个字符串

输出格式:
输出排好序的字符串的序列

输入样例:
fecbad
输出样例:
abcdef
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        String s=input.next();
        char[] ss=new char[s.length()];
        char ch;
        int k=0;
        for(int i=0;i<s.length();++i)
            ss[i]=s.charAt(i);
        for(int i=0;i<s.length()-1;++i)
            for(int j=i+1;j<s.length();++j){
                if(ss[i]>ss[j]){
                  ch=ss[i];
                  ss[i]=ss[j];
                  ss[j]=ch;
                }

            }
        for(int i=0;i<s.length();++i)
            System.out.print(ss[i]);
       input.close();
    }
}

7-2 查找电话号码 (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.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        Map<String,String> map=new HashMap<String, String>();
        while(input.hasNext()){
            String s=input.next();
            if(s.equals("noname")){
                break;
            }
            else{
                String string=input.next();
                map.put(s, string);
            }
        }
        String tString=input.next();
        if(map.containsKey(tString)){
            System.out.println(map.get(tString));
        }
        else {
            System.out.println("Not found.");
        }
        input.close();
    }
}

7-4 Largest rows and columns (10分)
(Largest rows and columns) Write a program that input fills in 0s and 1s into an n-by-n matrix, prints the matrix, and finds the rows and columns with the most 1s. (Hint: Use two ArrayLists to store the row and column indices with the most 1s.) Here is a sample run of the program: Enter the array size n: 4 The random array is 0 0 1 1 0 0 1 1 1 1 0 1 1 0 1 0 The largest row index: 2 The largest column index: 2, 3

input style :
Enter the array size n and the n x n numbers that input 0s and 1s . The first line input the length n of the matrix . And input the value of the matrix the other n line.

output style:
Displays the rows and columns with the most 1s…

input sample:
4
0 0 1 1
0 0 1 1
1 1 0 1
1 0 1 0
output sample:
Highest row: [2]
Highest column: [2, 3]

代码如下:

import java.util.Scanner;
/*
4
0 0 1 1
0 0 1 1
1 1 0 1
1 0 1 0
 */
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int[][] arr=new int[n][n];
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                arr[i][j]=input.nextInt();
            }
        }
        int[] row=new int[n];
        int[] column=new int[n];
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                if(arr[i][j]==1){
                    row[i]+=1;
                    column[j]+=1;
                }
            }
        }
        System.out.print("Highest row: [");
        Find(row);
        System.out.print("Highest column: [");
        Find(column);
        input.close();
    }
    public static void Find(int arr[]){
        int max=arr[0];
        int count=0;
        for(int i=0;i<arr.length;++i){
            if(arr[i]>max){
                max=arr[i];
            }
        }
        for(int i=0;i<arr.length;++i){
            if(arr[i]==max){
                count+=1;
            }
        }
        for(int i=0;i<arr.length;i++){
            if(count==1 && arr[i]==max) 
                System.out.print(i);
            if(count>1 && arr[i]==max) {
                System.out.print(i+",");
                count-=1;}
        }
        System.out.println("]");
    }
}

7-5 编程从键盘输入一个整数,计算出阶乘并输出——用到BigInteger (2019-12-7) (10分)
编程从键盘输入一个整数,计算出阶乘并输出。

输入格式:
输入 39

输出格式:
输出:20397882081197443358640281739902897356800000000

输入样例:
58
输出样例:
2350561331282878571829474910515074683828862318181142924420699914240000000000000

1.For循环中利用BigInteger

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        BigInteger a=BigInteger.valueOf(x);
        BigInteger b=BigInteger.ONE;
        BigInteger s=BigInteger.ONE;
      for(BigInteger i=BigInteger.ONE;i.compareTo(a)<=0;i=i.add(BigInteger.ONE)){
            s=s.multiply(i);
       }
        System.out.println(s);
        sc.close();
    }
}

2.for循环中利用int(超大范围下不满足)

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
       int n=input.nextInt();
        BigInteger m = BigInteger.ONE;
        for(BigInteger i=1;i<=n;i++){
            m=m.multiply(BigInteger.valueOf(i));

        }
        System.out.println(m);
        input.close();
    }
}

7-6 有重复的数据 (10分)
在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。

输入格式:
你的程序首先会读到一个正整数n,1<=n<=100000。然后是n个整数。

输出格式:
如果这些整数中存在重复的,就输出:

YES
否则,就输出:

NO
输入样例:
5
1 2 3 1 4
输出样例:
YES

用集合不用超限

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

class Input{
	StringTokenizer tok;
	BufferedReader buf;
	public Input() {
		buf = new BufferedReader(new InputStreamReader(System.in));
	}
	boolean hasNext()
	{
		while(tok==null||!tok.hasMoreElements())
		{
			try {
				tok = new StringTokenizer(buf.readLine());
			} catch (Exception e) {
				// TODO: handle exception
				return false;
			}
		}
		return true;
	}
	String next()
	{
		if(hasNext())
			return tok.nextToken();
		return null;
	}
	int nextInt()
	{
		return Integer.parseInt(next());
	}
}

public class Main {
    public static void main(String[] args){
        Input in = new Input();
        Set<Integer> s = new HashSet<>();
        int n = in.nextInt();
        for(int i=0;i<n;i++)
        	s.add(in.nextInt());
        if(s.size()==n)
        	System.out.println("NO");
        else
        	System.out.println("YES");
    }
}

7-7 编写程序,实现字符串大小写的转换并倒序输出。 (12分)
编写程序,实现字符串大小写的转换并倒序输出。

输入格式:
输入一行字符串

输出格式:
字符串大小写的转换,并倒序输出

输入样例:
Hello World!
输出样例:
!DLROw OLLEh

代码如下:


import java.util.Scanner;

/*
实现字符串大小写的转换并倒序输出。
 */
public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        StringBuffer str=new StringBuffer(sc.nextLine());
        StringBuffer xstr=new StringBuffer();

        if(str != null){
            for(int i=0; i < str.length(); i++){
                if(Character.isLowerCase(str.charAt(i))){
                    xstr.append(Character.toUpperCase(str.charAt(i)));
                }
                else if(Character.isUpperCase(str.charAt(i))){
                    xstr.append(Character.toLowerCase(str.charAt(i)));
                }
                else xstr.append(str.charAt(i));
            }
        }
        xstr=xstr.reverse();//逆序
        System.out.print(xstr);
        sc.close();
    }
}

7-8 Combine two lists then sorted it (10分)
(Combine two lists then sorted it.)
Write a method that returns the union of two array lists of integers using the following header:
public static ArrayList union( ArrayList list1, ArrayList list2)
For example, the union of two array lists {2,3,1,5} and {3,4,6} is {2, 3, 1, 5, 3, 4, 6}.
Write a test program that prompts the user to enter two lists, then sorted the union list, finally displays their sorted union. The numbers are separated by exactly one space in the output.
Write a test program that prompts the user to enter the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers for the two array. After sort theunion intergers and displays the sorted union list separated by exactly one space.

input style :

Input the two number m,n for the length of two arrays in the first line, and the next two line input m integer and n intergers .

output style:

Displays the sorted union list separated by exactly one space…

input sample:
3 4
23 44 32
12 43 32 44
output sample:
12 23 32 32 43 44 44 

代码如下:

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		int n = in.nextInt()+in.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++)
		{
			a[i] = in.nextInt();
		}
		Arrays.sort(a);
		for(int i=0;i<n;i++)
		{
			System.out.print(a[i]+" ");
		}
		
	}
 
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值