博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一、PTA 基本数据类型(Java)
阅读量:2055 次
发布时间:2019-04-28

本文共 3257 字,大约阅读时间需要 10 分钟。

一、PTA 基本数据类型

文章目录

1.求最大值 (15 分)

本题目要求读入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 x= sc.nextInt(); int y= sc.nextInt(); int z=x>y?x:y; System.out.println(z); }}

2. 编程题:统计符合条件元素的个数-hebust (15 分)

统计1…n的闭区间中,能够被3整除元素的奇数和偶数的个数

输入格式:

输入值n的范围是 【1…1000】

输出格式:

奇数个数,偶数个数

输入样例:

5

输出样例:

1,0

源代码:

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

3. Time Difference (20 分)

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 sc = new Scanner(System.in); int h1 = sc.nextInt(); int m1 = sc.nextInt(); int h2 = sc.nextInt(); int m2 = sc.nextInt(); int h,m; if(m2>=m1){
h=h2-h1; m=m2-m1; } else{
h=h2-1-h1; m=m2+60-m1; } System.out.println(h + " " + m); }}

4. java基本语法-整数四则运算 (20 分)

输入2个整数,输出它们的和、差、乘积和准确的商。

输入格式:

输入两个整数

输出格式:

每一行中依次输出四则运算的结果

输入样例:

7016

输出样例:

865411204.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(); int a=m+n; int b=m-n; int c=m*n; double d=(double)m/(double)n; System.out.println(a); System.out.println(b); System.out.println(c); System.out.printf("%.3f",d); }}

5.计算两个数的和 (15 分)

计算两个数的和。 通过键盘为变量a和b赋值,然后计算变量a与b的和,并将和赋值给变量sum,最终输出变量sum的值;

输入格式:

输入两个整数

输出格式:

两个数的和

输入样例:

2 8

输出样例:

10

源代码:

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); }}

6.求一个三位正整数各位数字之和 (15 分)

求一个三位正整数各位数字之和

输入格式:

输入一个三位的正整数

输出格式:

输出百十个位上各位数字的和

输入样例:

678

输出样例:

21

源代码:

import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int m= sc.nextInt(); int a=m/100; int b=m%100/10; int c=m%10; System.out.println(a+b+c); }}

转载地址:http://ijnlf.baihongyu.com/

你可能感兴趣的文章
win10将IE11兼容ie10
查看>>
checkbox设置字体颜色
查看>>
第一篇 HelloWorld.java重新学起
查看>>
ORACLE表空间扩张
查看>>
orcal 循环执行sql
查看>>
web.xml配置监听器,加载数据库信息配置文件ServletContextListener
查看>>
结构型模式之桥接模式(Bridge)
查看>>
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>
C++/C 宏定义(define)中# ## 的含义 宏拼接
查看>>
Git安装配置
查看>>
linux中fork()函数详解
查看>>
C语言字符、字符串操作偏僻函数总结
查看>>
Git的Patch功能
查看>>
分析C语言的声明
查看>>