# java测验-1 题解

## 1:反向输出一个三位数

`100`

`001`

```import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
Scanner cin=new Scanner(System.in);
String string=cin.nextLine();
for(int i=string.length()-1;i>=0;i--)
{
System.out.print(string.charAt(i));
}
}
}```

## 2:素数对

`100`

```3 5
5 7
11 13
17 19
29 31
41 43
59 61
71 73```

(isprime(i)==1)&&(isprime(i+2)==1)&&((i+2)<=n)

```import java.util.Scanner;
public class Main {
public static int isprime(int m)//判断是否为素数
{
int k=(int)(Math.sqrt((double)m));
int i=2;
while(m%i!=0&&i<=k)  i++;
if(i>k) return 1;
else return 0;
}
public static void  main(String[] args){
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
int flag=0;
for(int i=2;i<=n;i++){
if((isprime(i)==1)&&(isprime(i+2)==1)&&(i+2)<=n) {
flag=1;
System.out.println(i + " " + (i + 2));
}
}
if(flag==0){
System.out.println("empty");
}
cin.close();
}
}```

## 3:明明的随机数

```10
20 40 32 67 40 20 89 300 400 15
```

```8
15 20 32 40 67 89 300 400```

```import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public  class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
while (cin.hasNext()){
int n = cin.nextInt();
Set<Integer> teSet=new TreeSet<Integer>();
for (int i =0;i<n;i++){
}
System.out.println(teSet.size());
for(Integer i:teSet) {
System.out.print(i+" ");
}
}
}
}```

## 4:合影效果

```6
male 1.72
male 1.78
female 1.61
male 1.65
female 1.70
female 1.56```

`1.65 1.72 1.78 1.70 1.61 1.56`

` girlsSet = ((TreeSet<Double>) girlsSet).descendingSet();`

```import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public  class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
while (cin.hasNext()){
int n = cin.nextInt();
Set<Double> boySet=new TreeSet<Double>();
Set<Double> girlsSet=new TreeSet<Double>();
girlsSet = ((TreeSet<Double>) girlsSet).descendingSet();
for (int i =0;i<n;i++){
String input=cin.next();
if(input.equals("male"))
if(input.equals("female"))
}

for(Double i:boySet) {
System.out.printf("%.2f ",i);
}
for(Double i:girlsSet) {
System.out.printf("%.2f ",i);
}
}
}

}```

```import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
List<Double> list1=new ArrayList<Double>();
List<Double> list2=new ArrayList<Double>();
int n=sc.nextInt();
for(int i=0;i<n;i++){
String sex=sc.next();
double shengao=sc.nextDouble();
if("male".equals(sex)){
}
else{
}
}
Collections.sort(list1);
Collections.sort(list2,new Comparator<Double>(){

@Override
public int compare(Double arg0, Double arg1) {
// TODO Auto-generated method stub
return arg1.compareTo(arg0);
}

});

for(Double d:list1){
System.out.printf("%.2f ",d);
}
for(Double d:list2){
System.out.printf("%.2f ",d);
}

}
}
```

## 5:确定进制

6*9 = 42 对于十进制来说是错误的，但是对于13进制来说是正确的。即, 6(13) * 9(13) = 42(13)， 而 42(13) = 4 * 131+ 2 * 130= 54(10)。 你的任务是写一段程序读入三个整数p、q和 r，然后确定一个进制 B(2<=B<=16) 使得 p * q = r. 如果 B有很多选择, 输出最小的一个。例如： p = 11, q = 11, r = 121. 则有 11(3) * 11(3) = 121(3) 因为 11(3) = 1 * 31+ 1 * 30= 4(10) 和 121(3) = 1 * 32+ 2 * 31+ 1 * 30= 16(10)。 对于进制 10,有 11(10) * 11(10) = 121(10)。这种情况下，应该输出 3。如果没有合适的进制，则输出 0。

`6 9 42`

`13`

```import java.util.Scanner;
public class Main {
public static void  main(String[] args){
Scanner cin=new Scanner(System.in);
String p=cin.next();
String q=cin.next();
String r=cin.next();
int out=0,i=1;
for(i=2;i<=32;i++)
{
Integer a=Integer.parseInt(p,i);
Integer b=Integer.parseInt(q,i);
Integer c=Integer.parseInt(r,i);
if(a*b==c)
break;
}
if(i<=32)
System.out.println(i);
else System.out.println("0");
}
}```

```import java.util.Scanner;

public class Main{
public static void  main(String[]args) {
Scanner cin=new Scanner(System.cin);
int a=cin.nextInt(),b=cin.nextInt(),c=cin.nextInt();
boolean flag=true;
for(int i=2;i<=16;i++) {
if((judge(a,i)*judge(b,i))==judge(c,i)) {
System.out.println(i);
flag=false;
break;
}
}
if(flag)
System.out.println(0);
}
public static  int judge(int x,int B) {//进制转换函数
int value=1,num=0;
while(x!=0) {
if((x%10)>=B) {
return 99999;
}
num+=((x%10)*value);
value*=B;
x/=10;
}
return num;
}

}```

```import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException  {
Scanner sc=new Scanner(System.in);
String p=sc.next();
String  q=sc.next();
String  r=sc.next();
for(int i=2;i<=16;i++){
try {
int p1=Integer.parseInt(p,i);
int q1=Integer.parseInt(q,i);
int r1=Integer.parseInt(r,i);
if(p1*q1==r1){
System.out.println(i);return;
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block

}
}
System.out.println(0);
}
}```