7-28 整除判断 (10分)
输入一个整数,判断是否被2,3整除。如果能被2和3同时整除,则输出“n is divisible by 2 and 3”(n是输入的整数)。如果只能被2或3整除,则输出“n is divisible by 2 or 3, but not both”,如果都不能被2和3同时整除,则输出“n is not divisible by 2 and 3”
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n%2 == 0) {
if (n%3 == 0) {
System.out.println(n+" is divisible by 2 and 3");
}else {
System.out.println(n+" is divisible by 2 or 3, but not both");
}
}else if (n%3 == 0) {
if (n%2 == 0) {
System.out.println(n+" is divisible by 2 and 3");
}else {
System.out.println(n+" is divisible by 2 or 3, but not both");
}
}else {
System.out.println(n+" is not divisible by 2 and 3");
}
}
}
7-29 求最大公约数 (10分)
输入两个正整数,输出这两个整数的最大公约数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n1 = input.nextInt();
int n2 = input.nextInt();
int gcd = 1 ;
int k = 2 ;
while (k <= n1 && n2 % k <= n2) {
if (n1 % k == 0 && n2 % k == 0) {
gcd = k ;
}
k++ ;
}
System.out.print(gcd);
}
}
7-30 BMI计算 (10分)
身体质量指数(BMI)是基于体重和身高计算的健康测量。可以通过以千克为单位的体重除以以米为单位的身高的平方,得到BMI的值。 输入体重(磅)和身高(英寸),求身体BMI的指标。1磅=0.45359237,1英寸=0.0254米。
BMI
说明
BMI<18.5
Underweight
18.5<=BMI<25.0
Normal
25.0<=BMI<30.0
Overweight
30.0<=BMI
Obese
import java.util.Scanner;
public class Main{
public static void main(String []args) {
Scanner input=new Scanner(System.in);
double n=input.nextDouble();
double m=input.nextDouble();
double b=n*0.45359237/m/0.0254/m/0.0254;
if(b<18.5)System.out.print("Underweight");
else if(b<25)System.out.print("Normal");
else if(b<30)System.out.print("Overweight");
else System.out.print("Obese");
}
}
7-31 三个整数排序 (5分)
输入三个整数 ,然后从小到大的打印出来
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
int t = 0 ;
if (x >= y) {
t = x ;
x = y ;
y = t;
}
if (x >= z) {
t = x;
x = z;
z = t;
}
if (y >= z) {
t = y;
y = z;
z = t;
}
System.out.print(x+" "+y+" "+z);
}
}
超市奖票兑换 (10分)
某家超市有一种促销手段,购买金额超过一定金额就给一张随机编号奖票。编号是一个1到100之间的数字。当收集到连续编号的三张贴花时,就可以换一个小礼物。兑换完礼物后,该奖票就作废。
小明经常去某家超市购物,积攒了不少奖票,你帮他看看,能换多少小礼物。
输入格式:
首先是一个正整数N(1
输出格式:
输出一个数字,表示小明可以换的小礼物的数量。如果不能换小礼物,就输出0。
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a[] = new int[n];
for( int i = 0 ; i < n ; i ++){
a[i] = input.nextInt();
}
Arrays.sort(a);
int num = jp( a, n);
System.out.print(num);
}
public static int jp(int a[], int n){
int count = 0;
for (int i = 0; i < n-2 ; i++){
for(int j = i+1; j < n ; j++){
for(int k = j+1; k < n; k++){
if(a[k] == a[i] + 2 && a[j] == a[i] + 1 && a[i] != 0){
count++;
a[i] = 0;
a[k] = 0;
a[j] = 0;
}else{
continue;
}
}
}
}
return count;
}
}
大数相加 (10分)
在Java语言中,整数相加受到了位数的限制,请实现两个超大整数的加法。
输入格式:
两个超大整数,由于超出了范围,所以是以字符串的方式存储的。
输出格式:
输出两个超大整数相加的结果。
import java.util.Scanner;
public class Main {
public static void main(String[] args){
String tempa, tempb;
Scanner input = new Scanner(System.in);
tempa = input.nextLine();
tempb = input.nextLine();
int c = Math.max(tempa.length(), tempb.length());
int[] a = new int[c] ;
int[] b = new int[c];
int[] result = new int[c+1];
Transform(tempa, tempb, a, b);
result = BigNumAdd(a,b);
StringBuilder str = new StringBuilder();
for (int i = 0; i < result.length; i++) {
str.append(result[i]);
}
System.out.print(tempa+"+"+tempb+""+"="+str);
}
private static int[] BigNumAdd(int[] a, int[] b) {
int l = a.length > b.length ? a.length : b.length;
int[] c = new int[l];
for (int i = l-1; i >= 0; i--) {
c[i] = c[i] + a[i] + b[i];
if ((c[i] >= 10) && (i != 0)) {
c[i] = c[i] - 10;
c[i-1]++;
}
}
return c;
}
private static void Transform(String tempa, String tempb, int[] a, int[] b) {
Transform(tempa,a);
Transform(tempb,b);
}
private static void Transform(String source, int[] output) {
int l = output.length;
for(int i = source.length() - 1 ; i >= 0; i--) {
if(source.charAt(i) >= '0' && source.charAt(i) <= '9') {
output[--l] = source.charAt(i) - '0';
}
else {
output[--l] = 0;
}
}
}
}
消费税计算 (10分)
营业税为消费额的6%,用户输入消费金额,程序计算出营业税,计算结果四舍五入保留小数点两位。当输入金额为负数是,系统输出“Invalid input”。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a = in.nextDouble();
double b = 0.0;
if (a<0) {
System.out.print("Invalid input");
}else {
b = a*0.06;
System.out.println(String.format("%.2f",b));
}
}
}
计算器 (10分)
实现一个简单的四则运算器,用户依次输入第一个运算数,运算符,和第二个运算数,程序给出相应答案。
输入格式:
整型 字符型 整型
输出格式:
字符串
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
String f = input.next();
int b = input.nextInt();
int sum = 0;
switch (f) {
case "+":
sum = a+b;
break;
case "-":
sum = a-b;
break;
case "*":
sum = a*b;
break;
case "/":
sum = a/b;
break;
default:
break;
}
System.out.print(a+" "+f+" "+b+" = "+sum);
}
}
十六进制转十
一.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String str16 = in.next();
long str10 = Long.parseLong(str16,16);
System.out.println(str10);
in.close();
}
}
二.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String hex = input.nextLine();
System.out.println(hexToDecimal(hex.toUpperCase()));
input.close();
}
private static int hexToDecimal(String upperCase) {
int l = upperCase.length();
double sum=0;
for(int i=0;i
switch (upperCase.charAt(i)) {
case '0':sum=sum+0*Math.pow(16,l-i-1);
break;
case '1':sum=sum+1*Math.pow(16,l-i-1);
break;
case '2':sum=sum+2*Math.pow(16,l-i-1);
break;
case '3':sum=sum+3*Math.pow(16,l-i-1);
break;
case '4':sum=sum+4*Math.pow(16,l-i-1);
break;
case '5':sum=sum+5*Math.pow(16,l-i-1);
break;
case '6':sum=sum+6*Math.pow(16,l-i-1);
break;
case '7':sum=sum+7*Math.pow(16,l-i-1);
break;
case '8':sum=sum+8*Math.pow(16,l-i-1);
break;
case '9':sum=sum+9*Math.pow(16,l-i-1);
break;
case 'A':sum=sum+10*Math.pow(16,l-i-1);
break;
case 'B':sum=sum+11*Math.pow(16,l-i-1);
break;
case 'C':sum=sum+12*Math.pow(16,l-i-1);
break;
case 'D':sum=sum+13*Math.pow(16,l-i-1);
break;
case 'E':sum=sum+14*Math.pow(16,l-i-1);
break;
case 'F':sum=sum+15*Math.pow(16,l-i-1);
break;
default:
break;
}
}
return (int)sum;
}
}
十进制转十六
一.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Integer n = in.nextInt();
in.close();
System.out.println(Integer.toHexString(n).toUpperCase());
}
}
二.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long number = sc.nextLong();
int remainder = 0;
int len;
StringBuilder st = new StringBuilder();
do{
remainder = (int) (number % 16);
number = number / 16;
switch (remainder) {
case 10:
st.append("A");
break;
case 11:
st.append("B");
break;
case 12:
st.append("C");
break;
case 13:
st.append("D");
break;
case 14:
st.append("E");
break;
case 15:
st.append("F");
break;
default:
st.append(remainder);
break;
}
}while(number >= 1);
len = st.toString().length();
char [] arry = st.toString().toCharArray();
st = new StringBuilder();
for(int i = len - 1; i >= 0; i--){
st.append(arry[i]);
}
System.out.println(st);
}
}