今天华为机试三道题:
一、【30,60】之间完美数的个数,完美数字定义为能同时被2,3,5整除。
答:很简单直接代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
/*try{
InputStream in = new FileInputStream(new File("C:\\Users\\zj\\Desktop\\1.txt"));
Scanner s = new Scanner(in);
while(s.hasNextLine()){
System.out.println(s.nextLine());
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
}*/
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
s.close();
int count = 0;
if(a <= b && a < 10000 && a >= 0 && b < 10000 && b >= 0){
for(int m = a; m <= b; m++){
if(m%2 == 0 && m%3 == 0 && m%5 == 0){
count++;
}
}
}else{
System.out.print("输入非法");
}
System.out.print(count);
}
}
二、输入2个数字,反转数字相加,如果一个为8,另一个为21