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
输出样例:
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 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);
}
}