JavaS3&S4&S5

6-1 是否偶数 (10分)

本题要求实现一个函数,判盘输入的整数是否是偶数,如果是偶数,返回true,否则返回false。
函数接口定义:
public static boolean isOdd(int data)
说明:其中 data 是用户传入的参数。 data 的值不超过int的范围。函数须返回 true 或者 false。
裁判测试程序样例:

import java.util.Scanner;public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int data=in.nextInt();
		System.out.println(isOdd(data));
	}
	
	
	/* 请在这里给出isOdd(i)函数 */
	
	
}

输入样例:
8
输出样例:
true

public static boolean isOdd(int data) {
		return (data%2==0? true:false);
	}

7-1 字符串处理 (15分)

编写一个程序,用户输入任意一个字符串,显示它的长度和第一个字符。
输入格式:
输入任意一个字符串。
输出格式:
显示它的长度和第一个字符,其间用,分隔。
输入样例:
abc 4567
输出样例:
8,a

import java.util.Scanner;
public class Main{
	public static void main(String []args) {
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		System.out.println(str.length()+","+str.charAt(0));
	}
}

7-2 求最大值 (10分)
本题目要求读入2个整数A和B,然后输出两个数的最大值。
输入格式:
输入在一行中给出2个绝对值不超过1000的整数A和B。
输出格式:
对每一组输入,在一行中输出最大值。
输入样例:
在这里给出一组输入。例如:
18 -299
输出样例:
在这里给出相应的输出。例如:
18

import java.util.Scanner;
public class Main{
	public static void main(String []args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		System.out.println(a>=b?a:b);
	}
}

7-1 jmu-Java-01入门-取数字浮点数 (2分)

本题目要求读入若干以回车结束的字符串表示的整数或者浮点数,然后将每个数中的所有数字全部加总求和。
输入格式:
每行一个整数或者浮点数。保证在浮点数范围内。
输出格式:
整数或者浮点数中的数字之和。题目保证和在整型范围内。
输入样例:
-123.01
234
输出样例:
7
9

import java.util.Scanner;
public class Main{
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			int sum=0;
			String str=sc.nextLine();
			for(int i=0;i<str.length();i++) {
				if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
					sum+=str.charAt(i)-'0';
				}
			}
			System.out.println(sum);
		}
	}
}

7-2 求矩阵主对角线元素的和 (30分)

编写一个程序,求n x n的double类型矩阵中主对角线上所有数字的和。
输入格式:
第一行输入矩阵的阶数n(0<=n<=10)。 第二行输入矩阵的n x n个double类型的数值。
输出格式:
输出n x n的double类型矩阵中对角线上所有数字的和(double型数值)。
输入样例:
4
1 2 3 4 5 6.5 7 8 9 10 11 12 13 14 15 16
输出样例:
34.5

import java.util.Scanner;
public class Main{
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		double a[][]=new double[n][n];
		double sum=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				a[i][j]=sc.nextDouble();
				if(i==j) {
					sum+=a[i][j];
				}
				
			}
		}
		System.out.println(sum);
	}
}

7-3 jmu-Java-01入门-取数字 (2分)

本题目要求读入若干个代表整数的字符串,然后将其转化为整数。
如果该数>=10000且<=20000,则依次输出其对应的二进制字符串、八进制字符串、十六进制字符串。
否则将字符串中的每个数字抽取出来,然后将所有数字加总求和。
提示:参考jdk文档的Integer,
输入样例:
123
10000
-123
314159265
输出样例:
1 2 3 6
10011100010000,23420,2710
1 2 3 6
3 1 4 1 5 9 2 6 5 36

import java.util.Scanner;
public class Main{
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()) {
			String str=sc.nextLine();
			int a=Integer.parseInt(str);
			if(a>=10000&&a<=20000) {
				System.out.println(Integer.toBinaryString(a)+','+Integer.toOctalString(a)+','
						+Integer.toHexString(a));
			}else {
				int sum=0;
				for(int i=0;i<str.length();i++) {
					if(str.charAt(i)=='-') {
						continue;
					}
					sum=sum+str.charAt(i)-'0';
					System.out.print(str.charAt(i)+" ");
				}
				System.out.println(sum);
				
			}
		}
	}
}

7-4 学生类-构造函数 (10分)

定义一个有关学生的Student类,内含类成员变量: String name、String sex、int age,所有的变量必须为私有(private)。
1.编写有参构造函数:
能对name,sex,age赋值。
2.覆盖toString函数:
按照格式:类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式
3.对每个属性生成setter/getter方法
4.main方法中
•输入1行name age sex , 调用上面的有参构造函数新建对象。
输入样例:
tom 15 male
输出样例:
Student [name=‘tom’, sex=‘male’, age=15]

import java.util.Scanner;
 
class Student{
    private String name;
    private String sex;
    private int age;
 
    public String toString() {
        String str="Student [name='"+name+"', sex='"+sex+"', age="+age+"]";
        return str;
    }
 
    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public int getAge() {
        return age;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int age=sc.nextInt();/*Integer.valueOf(sc.next());*/            //直接sc.nextInt()就可以
        String sx=sc.next();
        Student student = new Student(name, sx, age);
        System.out.println(student);
    }
}

7-1 jmu-Java-02基本语法-03-身份证排序 (4分)

  1. 输入n,然后连续输入n个身份证号。
  2. 然后根据输入的是sort1还是sort2,执行不同的功能。输入的不是sort1或sort2,则输出exit并退出。
    输入sort1,将每个身份证的年月日抽取出来,按年-月-日格式组装,然后对组装后的年-月-日升序输出。
    输入sort2,将所有身份证按照里面的年月日升序输出。
    注意:处理输入的时候,全部使用Scanner的nextLine()方法,以免出错。
    输入样例:
    6
    410425198309308225
    320203197206115011
    431227196108033146
    330226196605054190
    34080019810819327X
    320111197112301539
    sort1
    sort2
    e
    输出样例:
    1961-08-03
    1966-05-05
    1971-12-30
    1972-06-11
    1981-08-19
    1983-09-30
    431227196108033146
    330226196605054190
    320111197112301539
    320203197206115011
    34080019810819327X
    410425198309308225
    exit
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String id[] = new String[n];
		String birth[] = new String[n];
		sc.nextLine();
		for(int i = 0; i < n; i ++)
			id[i] = sc.nextLine();
		
		String t = sc.nextLine();
		
		while(true)
		{
			if(t.equals("sort1"))
			{
				for(int i = 0; i < n; i ++)
					birth[i] = id[i].substring(6, 10) + "-" + id[i].substring(10, 12) + "-" + id[i].substring(12, 14);
				Arrays.sort(birth);
				for(int i = 0; i < n; i ++)
					System.out.println(birth[i].toString());
			}else if(t.equals("sort2")) {
					for(int i = 0; i < n; i ++)
						birth[i] = id[i].substring(6, 14);
                        Arrays.sort(birth);
					for(int i = 0; i < n; i ++)
						for(int j = 0; j < n; j++)
							if(id[j].contains(birth[i]))	System.out.println(id[j].toString());
			}else if(t.equals("e")) {
				System.out.println("exit");
				sc.close();
				break;
			}
			t = sc.nextLine();
		}
		
	}

}

7-2 表达式计算 (5分)
Write a program that reads an expression in a line as input and prints out the result. Only non-negative integers and operators below are allowed in the expression:

      • / % ( )
        输入格式:
        A line of expression.
        输出格式:
        The result.
        输入样例:
        (2+32)/2-6
        输出样例:
        11
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int num[] = new int[100]; 
		char str1[] = new char[100];
		char str2[] = new char[100];
		int top1 = 0, top2 = 0, cnt = 0; 
		String s = sc.nextLine();
		for(int i=0;i<s.length();i++)
		{
			if(s.charAt(i)>='0'&&s.charAt(i)<='9')	str2[cnt++] = s.charAt(i);
			else
			{
				if(cnt!=0)
				{
					int ans = 0;
					for(int j=0;j<cnt;j++)	ans = ans*10+str2[j]-'0';
					num[top2++]=ans;
					cnt=0;
				}
				if(top1==0)	str1[top1++] = s.charAt(i);
				else if(s.charAt(i)=='+'||s.charAt(i)=='-')
				{
					while(top1!=0&&str1[top1-1]!='(')
					{
						if(top2>=2)
						{
							int x,y,z;
							y=num[top2-1]; top2--;
							x=num[top2-1]; top2--;
							z=fun(x,y,str1[top1-1]);
							num[top2++]=z;
						}
						top1--;
					}
					str1[top1++]=s.charAt(i);
				}
				else if(s.charAt(i)=='*'||s.charAt(i)=='/'||s.charAt(i)=='%')
				{
					while(top1!=0&&str1[top1-1]!='('&&str1[top1-1]!='+'&&str1[top1-1]!='-')
					{
						if(top2>=2)
						{
							int x,y,z;
							y=num[top2-1]; top2--;
							x=num[top2-1]; top2--;
							z=fun(x,y,str1[top1-1]);
							num[top2++]=z;
						}
					}
					str1[top1++]=s.charAt(i);
				}
				else if(s.charAt(i)=='(') str1[top1++] = s.charAt(i);
				else if(s.charAt(i)==')') 
				{
					while(top1!=0&&str1[top1-1]!='(')
					{
						if(top2>=2)
						{
							int x,y,z;
							y=num[top2-1]; top2--;
							x=num[top2-1]; top2--;
							z=fun(x,y,str1[top1-1]);
							num[top2++]=z;
						}
						top1--;
					}
					top1--;
				}
			}
		}
		if(cnt!=0)
		{
			int ans = 0;
			for(int j=0;j<cnt;j++)
			{
				ans=ans*10 + str2[j]-'0';
			}
			num[top2++]=ans;
			cnt = 0;
		}
		while(top1!=0)
		{
			if(top2>=2)
			{
				int x,y,z;
				y=num[top2-1]; top2--;
				x=num[top2-1]; top2--;
				z=fun(x,y,str1[top1-1]);
				num[top2++]=z;
			}
			top1--;
		}
		System.out.println(num[top2-1]);
	}
	public static int fun(int x, int y, char z) {
		switch(z) {
		case '+': return x+y;
		case '-': return x-y; 
		case '*': return x*y;
		case '/': return x/y;
		case '%': return x%y;
		default:
			return 0;
		}
	}
}

7-3 单词替换 (20分)
设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
输入格式:
首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
输出格式:
完成查找替换后的完整字符串
输入样例:
在这里给出一组输入。例如:
Although I am without you, I will always be ou you
ou
with
输出样例:
在这里给出相应的输出。例如:
Although I am without you, I will always be with you

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str,str1,str2;
		str = sc.nextLine();
		str1 = sc.nextLine();
		str2 = sc.nextLine();
		String t = str.replace(str1+' ',str2+' ');
		System.out.println(t);
	}
}

7-4 图书价格汇总 (10分)

图书价格汇总
输入格式:
假设某图书馆中图书记录的格式为“Java程序设计: 34;Web程序设计: 56;JSP程序设计:20”(每本书的价格是整数,每本书的价格与下一本书的名字之间有一个中文;)。
输出格式:
编写一个类的方法,能够接受键盘录入的符合上述格式的图书内容字符串,输出图书记录中所有书目的总价格。
输入样例:
Java程序设计:34 ;Web程序设计: 56;JSP程序设计:20
输出样例:
Java程序设计:34
Web程序设计: 56
JSP程序设计:20
总价格为110

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str;
		str=sc.nextLine();
		String s[]=str.split(";");         //通过分号分开字符串,注意分号是中文的
		for(int i=0;i<s.length;i++) {
			System.out.println(s[i].toString());
		}
		int sum=0;
		for(int i=0;i<s.length;i++) {
			String s1[]=s[i].split(":");                  //通过冒号分开字符串,注意冒号是中文的
			s1[1]=s1[1].replace(" ", "");               //去除多余的空格
			int a=Integer.valueOf(s1[1]);                 //将字符串转为int类型
			sum=sum+a;
		}
		System.out.println("总价格为"+sum);
	}

}

7-5 String Builder (10分)
You are going to read four numbers: n, a, b and c, like this:
12 2 5 3
First, n is used to build up a string from 0 to n, like this:
0123456789101112
is a string build up for n=12.
Then, in al the digits from index a to index b, count the appearence of c.
For the string above, 2 5 is:
2345
Thus the appearence of 3 is 1.
Input Format:
Four positive numbers, n, a, b and c, where a Output Format:
One number represnets the length of the generated string. One number represents the apprence of c. There is a space between the two numbers.
Sample Input:
12 2 5 3
Sample Output:
16 1

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n;
		int a;
		int b;
		String c;
		n=sc.nextInt();
		a=sc.nextInt();
		b=sc.nextInt();
		c=sc.next();
		StringBuilder str=new StringBuilder("");
		for(int i=0;i<=n;i++) {
			str=str.append(i);
		}
		int count=0;                  //我本来想用lastIndexof(c)来算的 但是有两个点一直过不了,不知道是为什么
		for(int i=a;i<=b;i++) {
			if(str.charAt(i)==c.charAt(0)) {
				count++;	
			}
		}
		System.out.println(str.length()+" "+count);
	}
}

你可能感兴趣的:(#,PTAJava题目集)