PAT-B-1006~1010试题代码(Java)

1006. 换个格式输出整数 (15)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.hym.PAT_B;

import java.util.Scanner;

/**
* Created by ymhou on 2016/11/10.
* 通过全部测试点,答案正确
*/

public class PAT_B_1006 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int a,b,c;
String A="",B="",C="";
a=num/100;b=(num%100)/10;c=num%10;
for(int i=0;i<a;i++){
A=A+"B";
}
for(int i=0;i<b;i++){
B=B+"S";
}
for(int i=1;i<=c;i++){
C=C+i;
}
System.out.println(A+B+C);

}
}

1007. 素数对猜想 (20)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.hym.PAT_B;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Created by ymhou on 2016/11/14.
* 两种方案
* 最后一个测试点内存超限,得18分
*/

public class PAT_B_1007 {
public static void main(String[] args) {
plan1();
}

public static void plan1(){
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int num=1;
int count=0;
while(GetnPrime(num) < N){
if(GetnPrime(num+1) > N){
break;
}
if(GetnPrime(num+1)-GetnPrime(num)==2){
count++;
}
num++;
}
System.out.println(count);
}

public static void plan2() {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int i=0;
int count=0;
while(i<GetNPrimeList(N).size()){
if(i+1 >= GetNPrimeList(N).size()){
break;
}
if(GetNPrimeList(N).get(i+1)-GetNPrimeList(N).get(i) == 2){
count++;
}
i++;
}
System.out.println(count);
}

public static int GetnPrime(int count){
List<Integer> list = new ArrayList<Integer>();
int startNumber = 1;
while(list.size() < count){
if(IsPrime(startNumber,list)){
list.add(startNumber);
}
startNumber++;
}
return --startNumber;
}

public static boolean IsPrime(int number,List<Integer> list){
if(number == 1){
return false;
}
int max = (int)Math.sqrt(number);
for (int n:list) {
if(number % n ==0){
return false;
}
if(n > max){
break;
}
}
return true;
}

public static List<Integer> GetNPrimeList(int N){
List<Integer> list = new ArrayList<>();
int startNumber = 1;
list.add(2);
while(list.get(list.size()-1) <= N){
startNumber++;
if(startNumber > N){
break;
}
if (IsPrime(startNumber,list)){
list.add(startNumber);
}

}
return list;
}
}

1008. 数组元素循环右移问题 (20)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.hym.PAT_B;

import java.util.Scanner;

/**
* Created by ymhou on 2016/11/14.
* 主要思路:
* 定义一个长度为2N的数组进行数组数据的复制
*
* 通过全部测试点,答案正确
*/

public class PAT_B_1008 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int M = scanner.nextInt();

int num[] = new int[2*N];
for(int i=0; i<N; i++){
num[i] = scanner.nextInt();
num[N+i] = num[i];
}

M = M%N;

for(int i=0; i<N; i++){
System.out.print(num[N-M+i]);
if(i == N-1){
break;
}
System.out.print(" ");
}
}
}

1009. 说反话 (20)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hym.PAT_B;

import java.util.Scanner;

/**
* Created by ymhou on 2016/11/21.
* 通过全部测试点,答案正确
*/

public class PAT_B_1009 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String stringArray[] = line.split(" ");

for(int i=0; i<stringArray.length; i++){
System.out.print(stringArray[stringArray.length-i-1]);
if(i == stringArray.length-1){
break;
}
System.out.print(" ");
}
}
}

1010. 一元多项式求导 (25)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.hym.PAT_B;

import java.util.Scanner;

/**
* Created by ymhou on 2016/11/21.
*通过全部测试点,答案正确
*/

public class PAT_B_1010 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String numArray[] = line.split("\\s+");

if(numArray.length==2 && numArray[1].equals("0")){
System.out.print("0 0");
}else {
String outstring="";
for(int i=0; i<numArray.length; i=i+2){
int a = Integer.parseInt(numArray[i])*Integer.parseInt(numArray[i+1]);
int b = Integer.parseInt(numArray[i+1])-1;
if(a==0) continue;
outstring = outstring+a+" "+b+" ";
}

System.out.print(outstring.substring(0,outstring.length()-1));
}
}
}

坚持原创技术分享,您的支持将鼓励我继续创作!