正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。 现给定A、DA、B、DB,请编写程序计算PA + PB。

输入格式:

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。

输出格式:

在一行中输出PA + PB的值。

输入样例1:

3862767 6 13530293 3

输出样例1:

399

输入样例2:

3862767 1 13530293 8

输出样例2:

0

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
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
String A = in.next();
int da = in.nextInt();
String B = in.next();
int db = in.nextInt();
in.close();

long pa = 0;
for (int i = 0; i < A.length(); i++) {
if (A.charAt(i) - '0' == da) {
pa = pa * 10 + (A.charAt(i) - '0');
}
}

long pb = 0;
for (int i = 0; i < B.length(); i++) {
if (B.charAt(i) - '0' == db) {
pb = pb * 10 + (B.charAt(i) - '0');
}
}

System.out.print(pa + pb);
}

}

给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的6174,这个神奇的数字也叫Kaprekar常数。 例如,我们从6767开始,将得到 7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 = 6174 7641 - 1467 = 6174 … … 现给定任意4位正整数,请编写程序演示到达黑洞的过程。

输入格式:

输入给出一个(0, 10000)区间内的正整数N。

输出格式:

如果N的4位数字全相等,则在一行内输出“N - N = 0000”;否则将计算的每一步在一行内输出,直到6174作为差出现,输出格式见样例。注意每个数字按4位数格式输出。

输入样例1:

6767

输出样例1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

输入样例2:

2222

输出样例2:

2222 - 2222 = 0000

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
import java.util.Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int number = in.nextInt();
in.close();

while (true) {
int d = desc(number);
int i = incr(number);
number = i - d;
if (number == 0) {
System.out.printf("%04d - %04d = %04d\n", i, d, number);
break;
}

if (number == 6174) {
System.out.printf("%04d - %04d = %04d\n", i, d, number);
break;
}
System.out.printf("%04d - %04d = %04d\n", i, d, number);
}
}

private static int desc(int number) {
char[] array = String.format("%04d", number).toCharArray();
Arrays.sort(array);
int temp = 0;
for (int i = 0; i < 4; i++) {
temp = temp * 10 + (array[i] - '0');
}
return temp;
}

private static int incr(int number) {
char[] array = String.format("%04d", number).toCharArray();
Arrays.sort(array);
int temp = 0;
for (int i = 3; i >= 0; i--) {
temp = temp * 10 + (array[i] - '0');
}
return temp;
}
}

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值: Z:0 1 2 3 4 5 6 7 8 9 10 M:1 0 X 9 8 7 6 5 4 3 2 现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入格式:

输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

输出格式:

按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出“All passed”。

输入样例1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出样例1:

12010X198901011234
110108196711301866
37070419881216001X

输入样例2:

2
320124198808240056
110108196711301862

输出样例2:

All passed

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
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int n = in.nextInt();
boolean isAllPassed = true;
for (int i = 0; i < n; i++) {
String number = in.next();
if (!check(number.substring(0, 17), number.charAt(17))) {
isAllPassed = false;
System.out.println(number);
}
}
in.close();

if (isAllPassed) {
System.out.println("All passed");
}
}

private static boolean check(String sub, char m) {

int[] value = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

int sum = 0;
for (int i = 0; i < sub.length(); i++) {
sum += value[i] * (sub.charAt(i) - '0');
}

char[] z = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

return m == z[sum % 11];
}

}

火星人是以13进制计数的:

  • 地球人的0被火星人称为tret。
  • 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
  • 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

例如地球人的数字“29”翻译成火星文就是“hel mar”;而火星文“elo nov”对应地球数字“115”。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:

输入第一行给出一个正整数N(<100),随后N行,每行给出一个[0, 169)区间内的数字 —— 或者是地球文,或者是火星文。 #### 输出格式: 对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

4
29
5
elo nov
tam

输出样例:

hel mar
may
115
13

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
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

// low digit.
final String[] lowDigit = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov",
"dec"};

// high digit.
final String[] highDigit = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer",
"jou"};

// string to number
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < lowDigit.length; i++) {
map.put(lowDigit[i], i);
}

for (int i = 0; i < highDigit.length; i++) {
map.put(highDigit[i], 13 + i);
}

int n = in.nextInt();

for (int i = 0; i < n; i++) {
int numInteger = 0;
String numString = null;

try {
numInteger = in.nextInt();
} catch (InputMismatchException inputMismatchException) {
numString = in.nextLine();
} finally {

if (numString == null) {
// the input is numbers.
if (numInteger < 13) {
System.out.println(lowDigit[numInteger]);
} else {
if (numInteger % 13 == 0) {
System.out.println(highDigit[numInteger / 13]);
} else {
System.out.println(highDigit[numInteger / 13] + " " + lowDigit[numInteger % 13]);
}
}
} else {
// the input is characters.
String[] tempString = numString.split(" "); // split the
// input

// if the length of tempString is 2, it must combine be high
// and low
if (tempString.length == 2) {
System.out.println((map.get(tempString[0]) - 13) * 13 + map.get(tempString[1]));
} else {
// maybe high or low

// map.get(Object) >= 13 is high
if (map.get(tempString[0]) >= 13) {
System.out.println((map.get(tempString[0]) - 13) * 13);
} else {
System.out.println(map.get(tempString[0]));
}
}
}
}
}
in.close();
}
}

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按“PATestPATest….”这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按PATest的顺序打印,直到所有字符都被输出。

输入格式:

输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。

输出格式:

在一行中按题目要求输出排序后的字符串。题目保证输出非空。

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

输出样例:

PATestPATestPTetPTePePee

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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
String string = in.nextLine();
in.close();

List<Character> PList = new ArrayList<>();
List<Character> AList = new ArrayList<>();
List<Character> TList = new ArrayList<>();
List<Character> eList = new ArrayList<>();
List<Character> sList = new ArrayList<>();
List<Character> tList = new ArrayList<>();

for (int i = 0; i < string.length(); i++) {
switch (string.charAt(i)) {
case 'P':
PList.add(string.charAt(i));
break;
case 'A':
AList.add(string.charAt(i));
break;
case 'T':
TList.add(string.charAt(i));
break;
case 'e':
eList.add(string.charAt(i));
break;
case 's':
sList.add(string.charAt(i));
break;
case 't':
tList.add(string.charAt(i));
break;

}
}

int maxSize = PList.size();
if (AList.size() > maxSize) {
maxSize = AList.size();
}
if (TList.size() > maxSize) {
maxSize = TList.size();
}
if (eList.size() > maxSize) {
maxSize = eList.size();
}
if (sList.size() > maxSize) {
maxSize = sList.size();
}
if (tList.size() > maxSize) {
maxSize = tList.size();
}
for (int i = 0; i < maxSize; i++) {

if (i < PList.size()) {
System.out.print(PList.get(i));
}
if (i < AList.size()) {
System.out.print(AList.get(i));
}
if (i < TList.size()) {
System.out.print(TList.get(i));
}
if (i < eList.size()) {
System.out.print(eList.get(i));
}
if (i < sList.size()) {
System.out.print(sList.get(i));
}
if (i < tList.size()) {
System.out.print(tList.get(i));
}
}
}

}

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱P和他实付的钱A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:

输入在1行中分别给出P和A,格式为“Galleon.Sickle.Knut”,其间用1个空格分隔。这里Galleon是[0, 107]区间内的整数,Sickle是[0, 17)区间内的整数,Knut是[0, 29)区间内的整数。

输出格式:

在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。

输入样例1:

10.16.27 14.1.28

输出样例1:

3.2.1

输入样例2:

14.1.28 10.16.27

输出样例2:

-3.2.1

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
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Coin p = new Coin(in.next().split("[.]"));
Coin a = new Coin(in.next().split("[.]"));
in.close();

Coin result = new Coin();
if (p.galleon > a.galleon) {
System.out.print("-");

//p minus a
if (p.kunt < a.kunt) {
p.kunt += 29;
p.sickle--;
}
result.kunt = p.kunt - a.kunt;

if (p.sickle < a.sickle) {
p.sickle += 17;
p.galleon--;
}
result.sickle = p.sickle - a.sickle;
result.galleon = p.galleon - a.galleon;
} else {
//a minus p
if (p.kunt > a.kunt) {
a.kunt += 29;
a.sickle--;
}
result.kunt = a.kunt - p.kunt;

if (p.sickle > a.sickle) {
a.sickle += 17;
a.galleon--;
}
result.sickle = a.sickle - p.sickle;
result.galleon = a.galleon - p.galleon;
}


System.out.print(result.galleon + "." + result.sickle + "." + result.kunt);
}

}

class Coin {
int galleon;
int sickle;
int kunt;

public Coin() {
this.galleon = this.sickle = this.kunt = 0;
}

public Coin(String[] coin) {
this.galleon = Integer.parseInt(coin[0]);
this.sickle = Integer.parseInt(coin[1]);
this.kunt = Integer.parseInt(coin[2]);

}
}

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

输入格式:

输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。

输出格式:

输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。

输入样例:

10 a

输出样例:

aaaaaaaaaa
a a
a a
a a
aaaaaaaaaa

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
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int n = in.nextInt();
char c = in.next().charAt(0);
in.close();

int col = (int) (n / 2.0 + 0.5);
for (int i = 0; i < col; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || j == n - 1 || i == col - 1) {
System.out.print(c);
} else {
System.out.print(' ');
}
}
System.out.println();
}
}

}

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。 下面给出甲、乙两人的划拳记录,请你统计他们最后分别喝了多少杯酒。

输入格式:

输入第一行先给出一个正整数N(<=100),随后N行,每行给出一轮划拳的记录,格式为: 甲喊 甲划 乙喊 乙划 其中“喊”是喊出的数字,“划”是划出的数字,均为不超过100的正整数(两只手一起划)。

输出格式:

在一行中先后输出甲、乙两人喝酒的杯数,其间以一个空格分隔。

输入样例:

5
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15

输出样例:

1 2

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
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int n = in.nextInt();
int jia = 0, yi = 0;
for (int i = 0; i < n; i++) {
int jwhj = in.nextInt();
int jwhw = in.nextInt();
int yihj = in.nextInt();
int yihw = in.nextInt();

int sum = jwhj + yihj;
if (jwhw == sum && yihw != sum) {
yi++;
} else if (jwhw != sum && yihw == sum) {
jia++;
}
}
in.close();

System.out.println(jia + " " + yi);
}

}

编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。 现给定所有队员的比赛成绩,请你编写程序找出冠军队。

输入格式:

输入第一行给出一个正整数N(<=10000),即所有参赛队员总数。随后N行,每行给出一位队员的成绩,格式为:“队伍编号-队员编号 成绩”,其中“队伍编号”为1到1000的正整数,“队员编号”为1到10的正整数,“成绩”为0到100的整数。

输出格式:

在一行中输出冠军队的编号和总成绩,其间以一个空格分隔。注意:题目保证冠军队是唯一的。

输入样例:

6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61

输出样例:

11 176

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main() {
int n;
cin >> n;
int *team = new int[1001];
int max = 0;
for (int i = 0; i < n; i++) {
int index, value, teamMember;
scanf("%d-%d %d", &index, &teamMember, &value);
team[index] += value;

if (team[index] > team[max]) {
max = index;
}
}

cout << max << " " << team[max];
return 0;
}

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

输入格式:

输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。

输出格式:

按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有1个坏键。

输入样例:

7_This_is_a_test
_hs_s_a_es

输出样例:

7TI

此处,谢谢@xiaorong61提供的代码 将两次的输入都转化为大写的,使用字符串的indexOf快速查找。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String should = in.nextLine().toUpperCase();
String reality = in.nextLine().toUpperCase();
in.close();
String result = "";
for (int i = 0; i < should.length(); i++)
if (reality.indexOf(should.charAt(i)) == -1 && result.indexOf(should.charAt(i)) == -1)
result += should.charAt(i);
System.out.println(result);
}
}
0%