GaAs:PTA中Java新手题目

1、求最大值(15分)

输入格式:
输入在一行中给出2个绝对值不超过1000的整数A和B。
输出格式:
对每一组输入,在一行中输出最大值。
输入样例:
在这里给出一组输入。例如:

18 -299

输出样例:

18

源代码:

public class Main {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		if(a>=b)
		System.out.println(a);	
		else
		System.out.println(b);
	}
}

2、统计符合条件元素的个数-hebust
输入格式:
输入值n的范围是 【1…1000】
输出格式:
奇数个数,偶数个数
输入样例:

5

输出样例:

10

源代码:

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
			int n = sc.nextInt();
			int i,sum=0,num=0;
				for(i=1;i<=n;i++){
					if(i%3==0){
						if(i%2==0)
							sum++;
						else
							num++;
			}
		}
		System.out.printf("%d,%d",num,sum);
	}
}

3、Time Difference
What is the time difference between 10:30 and 11:45?

Your program reads two time spots and prints the time difference between them, in terms of hours and minutes.
Input Format
Two time spots, in 24-hour, each is represented as two numbers, as “hour minute”. The second time spot is later than the first and both are within the same day.
Output Format
Two numbers represent the time difference. The first is the hours in the difference, while the second is the minutes.
Sample Input

10 30 11 45

Sample Output

1 15

源代码:

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
	Scanner coin = new Scanner(System.in);
	int m = coin.nextInt();
	int n = coin.nextInt();
	int a = coin.nextInt();
	int b = coin.nextInt();
	int j,t;
    	if(m>a){
        	t=m;
        		m=a;
        			a=t;
    } 	j=a-m;
        	int k=b-n;
        		if(k<0){
            		b=b+60;
            		a=a-1;
            		j=j-1;
            		k=b-n;
    }
		System.out.println(j+" "+k);
	}
}

4、java基本语法-整数四则运算
输入2个整数,输出它们的和、差、乘积和准确的商。
输入格式:
输入两个整数
输出格式:
每一行中依次输出四则运算的结果
输入样例:

70
16

输出样例:

86
54
1120
4.375

源代码:

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int m = sc.nextInt();
	int n = sc.nextInt();
	System.out.println(m+n);
	System.out.println(m-n);
	System.out.println(m*n);
	System.out.println((float)m/n);
	}
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值