给定一个k位整数N = dk-110k-1 + … + d1101 + d0 (0<=di<=9, i=0,…,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

输入格式:

每个输入包含1个测试用例,即一个不超过1000位的正整数N。

输出格式:

对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。

输入样例:

100311

输出样例:

0:2
1:3
3:1

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

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String number = in.nextLine();
in.close();


int[] times = new int[10];
for (int i = 0; i < number.length(); i++) {
times[number.charAt(i) - '0']++;
}

for (int i = 0; i <= 9; i++) {
if (times[i] != 0) {
System.out.println(i + ":" + times[i]);
}
}
}
}

给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558。 现给定数字,请编写程序输出能够组成的最小的数。

输入格式:

每个输入包含1个测试用例。每个测试用例在一行中给出10个非负整数,顺序表示我们拥有数字0、数字1、……数字9的个数。整数间用一个空格分隔。10个数字的总个数不超过50,且至少拥有1个非0的数字。

输出格式:

在一行中输出能够组成的最小的数。

输入样例:

2 2 0 0 0 3 0 0 1 0

输出样例:

10015558

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

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] times = new int[10];
for (int i = 0; i < 10; i++) {
times[i] = in.nextInt();
}
in.close();

StringBuilder s = new StringBuilder();
for (int i = 1; i < 10; i++) {
if (times[i] != 0) {
s.append(i);
times[i]--;
break;
}
}


for (int j = 0; j < 10; j++) {
while (times[j] != 0) {
s.append(j);
times[j]--;
}
}

System.out.println(s);

}
}

令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。

输入格式:

输入在一行中给出M和N,其间以空格分隔。

输出格式:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

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
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int prime[1000000];

inline void getPrime() {

int m = 1000;
int i, j;

for (i = 2; i < 1000; i++) {
if (!prime[i]) {
for (j = i * i; j < 1000000; j += i)
prime[j] = 1;
}
}
j = 0;
for (i = 2; i < 1000000; i++)
if (!prime[i])
prime[j++] = i;
}


int main() {
memset(prime, 0, sizeof(prime));
int num = 0;
bool flag = false;
int M, N;
getPrime();
cin >> M >> N;

for (int i = M - 1; i < N; i++) {
if (num != 0 && num % 10 == 0) {
cout << endl;
flag = false;
}
if (flag)
cout << " ";
else
flag = true;
cout << prime[i];
num++;
}

return 0;
}

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:  

  • A1 = 能被5整除的数字中所有偶数的和;
  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4…;
  • A3 = 被5除后余2的数字的个数;
  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
  • A5 = 被5除后余4的数字中最大数字。

输入格式:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。 若其中某一类数字不存在,则在相应位置输出“N”。

输入样例1:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出样例1:

30 11 2 9.7 9

输入样例2:

8 1 2 4 5 6 7 9 16

输出样例2:

N 11 2 N 9

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
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <cstdio>

using namespace std;

void getA1(int *number, int n) {
int sum = 0;
bool isEnter = false;
for (int i = 0; i < n; i++) {
if (number[i] % 5 == 0 && number[i] % 2 == 0) {
sum += number[i];
isEnter = true;
}
}

if (!isEnter) {
cout << "N ";
} else {
cout << sum << " ";
}
}

void getA2(int *number, int n) {
int sign = 1;
int sum = 0;
bool isEnter = false;
for (int i = 0; i < n; i++) {
if (number[i] % 5 == 1) {
sum += sign * number[i];
sign = -sign;
isEnter = true;
}
}

if (isEnter == false) {
cout << "N ";
} else {
cout << sum << " ";
}
}

void getA3(int *number, int n) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (number[i] % 5 == 2) {
cnt++;
}
}

if (cnt == 0) {
cout << "N ";
} else {
cout << cnt << " ";
}
}

void getA4(int *number, int n) {
int cnt = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (number[i] % 5 == 3) {
sum += number[i];
cnt++;
}
}

if (cnt == 0) {
cout << "N ";
} else {
printf("%.1f ", sum * 1.0 / cnt);
}
}

void getA5(int *number, int n) {
int max = 0;
for (int i = 0; i < n; i++) {
if (number[i] % 5 == 4) {
if (max < number[i]) {
max = number[i];
}
}
}

if (max == 0) {
cout << "N";
} else {
cout << max;
}
}

int main() {
int n;
cin >> n;

int *number = new int[10001];
for (int i = 0; i < n; i++) {
cin >> number[i];
}

getA1(number, n);
getA2(number, n);
getA3(number, n);
getA4(number, n);
getA5(number, n);

delete[] number;
}

请编写程序,找出一段给定文字中出现最频繁的那个英文字母。

输入格式:

输入在一行中给出一个长度不超过1000的字符串。字符串由ASCII码表中任意可见字符及空格组成,至少包含1个英文字母,以回车结束(回车不算在内)。

输出格式:

在一行中输出出现频率最高的那个英文字母及其出现次数,其间以空格分隔。如果有并列,则输出按字母序最小的那个字母。统计时不区分大小写,输出小写字母。

输入样例:

This is a simple TEST. There ARE numbers and other symbols 1&2&3………..

输出样例:

e 7

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);

// use string to hold the input
String string = in.nextLine();

// create a array which length is 26
int[] array = new int[26];
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);

if (c >= 'a' && c <= 'z') {
array[c - 'a']++;
} else if (c >= 'A' && c <= 'Z') {
array[c - 'A']++;
} else {
//except character
//like number, coin sign and other letters.
}
}

in.close();

int maxIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > array[maxIndex]) {
maxIndex = i;
}
}

System.out.println((char) (maxIndex + 'a') + " " + array[maxIndex]);
}

}

输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。

输入格式:

输入在一行中依次给出3个整数A、B和D。

输出格式:

输出A+B的D进制数。

输入样例:

123 456 8

输出样例:

1103

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

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int a = in.nextInt();
int b = in.nextInt();
int d = in.nextInt();

in.close();
String aString = "";
do {
aString += a % d;
a /= d;
} while (a != 0);

String bString = "";
do {
bString += b % d;
b /= d;
} while (b != 0);

// We add a with b and print it.
StringBuilder result = new StringBuilder();
if (aString.length() > bString.length()) {
int temp = 0;
for (int i = 0; i < bString.length(); i++) {
result.append((temp + aString.charAt(i) - '0' + bString.charAt(i) - '0') % d);
temp = (temp + aString.charAt(i) - '0' + bString.charAt(i) - '0') / d;
}

if (temp != 0) {
for (int i = bString.length(); i < aString.length(); i++) {
result.append((aString.charAt(i) - '0' + temp) % d);
temp = (aString.charAt(i) - '0' + temp) / d;
}

if (temp != 0) {
result.append(temp);
}
}

} else {

int temp = 0;
for (int i = 0; i < aString.length(); i++) {
result.append((temp + aString.charAt(i) - '0' + bString.charAt(i) - '0') % d);
temp = (temp + aString.charAt(i) - '0' + bString.charAt(i) - '0') / d;
}

if (temp != 0) {
for (int i = aString.length(); i < bString.length(); i++) {
result.append((aString.charAt(i) - '0' + temp) % d);
temp = (aString.charAt(i) - '0' + temp) / d;
}

if (temp != 0) {
result.append(temp);
}
}
}

System.out.println(result.reverse());

}

}

要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数f的运行时间,我们只要在调用f之前先调用clock(),获得一个时钟打点数C1;在f执行完成后再调用clock(),获得另一个时钟打点数C2;两次获得的时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数,再除以常数CLK_TCK,就得到了以秒为单位的运行时间。 这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

输入格式:

输入在一行中顺序给出2个整数C1和C1。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 107]。

输出格式:

在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式输出;不足1秒的时间四舍五入到秒。

输入样例:

123 4577973

输出样例:

12:42:59

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

public class Main {
public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int c1 = in.nextInt();
int c2 = in.nextInt();
in.close();

int seconds = (int) ((c2 - c1) * 1.0 / 100 + 0.5);
System.out.printf("%02d:%02d:%02d", seconds / 3600, seconds / 60 % 60, seconds % 60);
}
}

每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。

输入格式:

输入第一行给出一个正整数N(<=1000),随后N行,每行给出一个考生的信息:“准考证号 试机座位号 考试座位号”。其中准考证号由14位数字组成,座位从1到N编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。 考生信息之后,给出一个正整数M(<=N),随后一行中给出M个待查询的试机座位号码,以空格分隔。

输出格式:

对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用1个空格分隔。

输入样例:

4
10120150912233 2 4
10120150912119 4 1
10120150912126 1 3
10120150912002 3 2
2
3 4

输出样例:

10120150912002 2
10120150912119 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine());
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
String[] strings = bufferedReader.readLine().split(" ");
students[i] = new Student(strings[0], strings[1], strings[2]);
}

int m = Integer.parseInt(bufferedReader.readLine());
String[] test = bufferedReader.readLine().split(" ");
for (int i = 0; i < m; i++) {
String findTestSite = test[i];

for (int j = 0; j < n; j++) {
if (students[j].testSite.equals(findTestSite)) {
System.out.println(students[j].id + " " + students[j].correctSite);
}
}
}
bufferedReader.close();
}

}

class Student {
String id;
String testSite;
String correctSite;

public Student(String id, String testSite, String correctSite) {
this.id = id;
this.testSite = testSite;
this.correctSite = correctSite;
}
}

设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。)

输入格式:

以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 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
import java.util.Scanner;

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

boolean isHaveOutput = false;
while (in.hasNext()) {
int expon = in.nextInt();
int coef = in.nextInt();

if (expon * coef != 0) {
if (isHaveOutput) {
System.out.print(" ");
} else {
isHaveOutput = true;
}

System.out.print(expon * coef + " " + (coef - 1));
}
}
in.close();

if (!isHaveOutput) {
System.out.print("0 0");
}
}
}

大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母’D’,代表星期四;第2对相同的字符是’E’,那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母’s’出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。

输入格式:

输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。

输出格式:

在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。

输入样例:

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

输出样例:

THU 14:04

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

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
String one = in.nextLine();
String two = in.nextLine();
String three = in.nextLine();
String four = in.nextLine();
in.close();

boolean isDay = false;
boolean isHour = false;
for (int i = 0; i < one.length() && i < two.length(); i++) {

if (one.charAt(i) == two.charAt(i)) {
if (((one.charAt(i) >= 'A' && one.charAt(i) <= 'N') || Character.isDigit(one.charAt(i))) && !isHour
&& isDay) {
isHour = true;
if (one.charAt(i) >= '0' && one.charAt(i) <= '9') {
System.out.print("0" + one.charAt(i));
} else {
System.out.print(one.charAt(i) - 'A' + 10);
}
}

if (one.charAt(i) >= 'A' && one.charAt(i) <= 'G' && !isDay) {
isDay = true;
switch (one.charAt(i)) {
case 'A':
System.out.print("MON ");
break;
case 'B':
System.out.print("TUE ");
break;
case 'C':
System.out.print("WED ");
break;
case 'D':
System.out.print("THU ");
break;
case 'E':
System.out.print("FRI ");
break;
case 'F':
System.out.print("SAT ");
break;
case 'G':
System.out.print("SUN ");
break;
}
}

}
}

for (int i = 0; i < three.length() && i < four.length(); i++) {
if ((Character.isUpperCase(three.charAt(i)) || Character.isLowerCase(three.charAt(i)))
&& three.charAt(i) == four.charAt(i)) {
System.out.printf(":%02d", i);
}
}

}

}
0%