1001. 害死人不偿命的(3n+1)猜想 (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.hym.PAT_B;
import java.util.Scanner;
/**
* Created by dell on 2016/11/2.
* 通过全部测试点,答案正确
*/
public class PAT_B_1001 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int step=0;
while(N!=1){
if(N%2==0){
N = N/2;
}else{
N = (3*N+1)/2;
}
step++;
}
System.out.print(step);
}
}
1002. 写出这个数 (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
33package com.hym.PAT_B;
import java.util.Scanner;
/**
* Created by dell on 2016/11/2.
* 通过全部测试点,答案正确
*/
public class PAT_B_1002 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String n = scanner.next();
int Num[] = new int[n.length()];
int sum=0;
for(int i=0; i<n.length(); i++){
Num[i] = (int)(n.charAt(i))-48;
sum = sum + Num[i];
}
String NumPy[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
String sumStr = Integer.toString(sum);
for(int i=0; i<sumStr.length(); i++){
if(i<sumStr.length()-1){
System.out.print(NumPy[(int)(sumStr.charAt(i))-48]+" ");
}else{
System.out.print(NumPy[(int)(sumStr.charAt(i))-48]);
}
}
}
}
1003. 我要通过!(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
52package com.hym.PAT_B;
import java.util.Scanner;
/**
* 这题主要难在题目的解读,如果将题目读懂这题就不是很难,这里我把题目作简单的分析。
1、对于第一个条件,我们可以知道所输入字符串只能包括P,A,T三个字母,我们可以根据设置3个变量来统计这3个字母的数量如果3个字母数量之和不等于字符串长度那就有可能包含其它字母。
2、条件2告诉我们的是在PAT左右两边的字符必须相等或为空或为A字符
3、条件3是比较好理解的,意思就是说P,T之间如果增加一个字符A那么在T的右边就要添加与P左边相等的字符,这个字符的数量要根据P左边的字符来定。
通过全部测试点,答案正确
*/
public class PAT_B_1003 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String str[] = new String[n];
for(int i=0; i<n; i++){
str[i] = scanner.next();
}
for(int i=0; i<n; i++){
System.out.println(StrTrueOrFalse(str[i]));
}
}
public static String StrTrueOrFalse(String s){
int NumA=0,NumP=0,NumT=0;
int PosP=0,PosT=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='A'){
NumA++;
}
if(s.charAt(i)=='P'){
NumP++;
PosP=i;
}
if(s.charAt(i)=='T'){
NumT++;
PosT=i;
}
}
if(NumA+NumP+NumT==s.length() && NumA!=0 && NumP==1 && NumT==1 && PosT-PosP>1 && PosP*(PosT-PosP-1)==s.length()-PosT-1 ){
return "YES";
}else {
return "NO";
}
}
}
1004. 成绩排名 (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
41package com.hym.PAT_B;
import java.util.Scanner;
/**
* Created by dell on 2016/11/2.
* 通过全部测试点,答案正确
*/
public class PAT_B_1004 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String strLine[][] = new String[n][3];
int Num[] = new int[n];
int i;
for( i=0; i<n; i++){
for(int j=0; j<3; j++){
strLine[i][j] = scanner.next();
}
Num[i] = Integer.parseInt(strLine[i][2]);
}
int max,min,PosMax,PosMin;
PosMax=PosMin=0;
max=min=Num[0];
for( i=0;i<n;i++){
if(Num[i]>max){
max=Num[i];
PosMax=i;
}
if(Num[i]<min){
min=Num[i];
PosMin=i;
}
}
System.out.println(strLine[PosMax][0]+" "+strLine[PosMax][1]);
System.out.println(strLine[PosMin][0]+" "+strLine[PosMin][1]);
}
}
1005. 继续(3n+1)猜想 (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
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
59package com.hym.PAT_B;
import java.util.*;
/**
* Created by ymhou on 2016/11/2.
* 通过全部测试点,答案正确
*/
public class PAT_B_1005 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int Num[] = new int[n];
for(int i=0; i<n; i++){
Num[i] = scanner.nextInt();
}
Set<Integer> set = new HashSet<Integer>();
int numtemp[] = new int[n];
for(int i=0; i<n; i++){
numtemp[i]=Num[i];
if(set.contains(numtemp[i])){
continue;
}
while(numtemp[i]!=1){
if(numtemp[i]%2==0){
numtemp[i] = numtemp[i]/2;
}else {
numtemp[i] = (3*numtemp[i]+1)/2;
}
if(set.contains(numtemp[i])){
break;
}
set.add(numtemp[i]);
}
}
int outNum[] = new int[n];
int count=0;
for(int i=0;i<n; i++){
if(set.contains(Num[i])){
continue;
}else {
count++;
outNum[i]=Num[i];
}
}
Arrays.sort(outNum);
for(int i=n-1; i>n-count-1; i--){
System.out.print(outNum[i]);
if(i==n-count){
break;
}
System.out.print(" ");
}
}
}