团体程序设计天梯赛-习题集部分题解(大牛勿喷)


Group program design ladder match - practice set.


L1-001. 打印沙漏

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

*****
 ***
  *
 ***
*****

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(<=1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

输入样例:

19 *

输出样例:

*****
 ***
  *
 ***
*****
2

代码如下:

 
  
import java.util.Scanner;

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

		while (in.hasNext()) {
			int n = in.nextInt();
			String ch = in.nextLine();

			int rem = 0;
			for (int i = 1; i <= n; i++) {
				int Sn = 2 * i * i - 1; 
				rem = n - Sn; 
				if (rem < 4 * i + 2) {
					break;
				}
			}
			int k = (int) Math.sqrt((n - rem + 1) / 2);
			for (int i = k; i >= 1; i--) {
				int p = 1;
				while (p <= k - i) {
					System.out.print(" ");
					p++;
				}
				int j = 1;
				while (j <= 2 * i - 1) {
					System.out.print(ch.substring(1));
					j++;
				}
				System.out.println();
			}
			for (int i = 2; i <= k; i++) {
				int p = 1;
				while (p <= k - i) {
					System.out.print(" ");
					p++;
                        }
				int j = 1;
				while (j <= 2 * i - 1) {
					System.out.print(ch.substring(1));
					j++;
				}
				System.out.println();
			}
			System.out.println(rem);
		}
		in.close();
	}
}

L1-002. 个位数统计

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + 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

代码如下:

import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			String str = in.nextLine();

			int len = str.length();
			int[] arr = new int[11];
			for (int i = 0; i < len; i++) {
				arr[str.charAt(i) - '0']++;
			}

			for (int i = 0; i < arr.length; i++) {
				if (arr[i] != 0)
					System.out.println(i + ":" + arr[i]);
			}
		}
		in.close();
	}
}
   
 
 

L1-003. 考试座位号

时间限制  200 ms    内存限制  65536 kB    代码长度限制  8000 B  

每个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
 
 
代码如下:
import java.util.Scanner;

public class Demo {

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

		while (in.hasNext()) {
			long[] idArr = new long[1001];
			int[] numArr = new int[1001];

			int N = in.nextInt();                                                      
			for (int i = 0; i < N; i++) {
				long id = in.nextLong();                                            
				int index = in.nextInt();
				int num = in.nextInt();

				idArr[index] = id;
				numArr[index] = num;
			}

			int M = in.nextInt();
			int[] arr = new int[M];
			for (int i = 0; i < M; i++) {
				arr[i] = in.nextInt();
				System.out.println(idArr[arr[i]] + " " + numArr[arr[i]]);
			}

		}
		in.close();
	}

}

L1-004. 念数字

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出“fu”字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

输入格式:

输入在一行中给出一个整数,如: 1234

提示:整数包括负数、零和正数。

输出格式:

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si

输入样例:
-600
输出样例:
fu liu ling ling

代码如下:

import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			String str = in.nextLine();

			char[] ch = str.toCharArray();
			String[] newStr = new String[ch.length];
			for (int i = 0; i < ch.length; i++) {
				if (ch[i] == '-') {
					newStr[i] = "fu";
				} else if (ch[i] - '0' == 0) {
					newStr[i] = "ling";
				} else if (ch[i] - '0' == 1) {
					newStr[i] = "yi";
				} else if (ch[i] - '0' == 2) {
					newStr[i] = "er";
				} else if (ch[i] - '0' == 3) {
					newStr[i] = "san";
				} else if (ch[i] - '0' == 4) {
					newStr[i] = "si";
				} else if (ch[i] - '0' == 5) {
					newStr[i] = "wu";
				} else if (ch[i] - '0' == 6) {
					newStr[i] = "liu";
				} else if (ch[i] - '0' == 7) {
					newStr[i] = "qi";
				} else if (ch[i] - '0' == 8) {
					newStr[i] = "ba";
				} else if (ch[i] - '0' == 9) {
					newStr[i] = "jiu";
				}
			}
			
			for (int i = 0; i < newStr.length; i++) {
				if (i == newStr.length - 1) {
					System.out.println(newStr[i]);
				} else {
					System.out.print(newStr[i] + " ");
				}
			}
		}
		in.close();

	}

}


L1-005. 求整数段和

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

给定两个整数A和B,输出从A到B的所有整数以及这些数的和。

输入格式:

输入在一行中给出2个整数A和B,其中-100<=A<=B<=100,其间以空格分隔。

输出格式:

首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字占5个字符宽度,向右对齐。最后在一行中输出全部数字的和。

输入样例:
-3 8
输出样例:
   -3   -2   -1    0    1
    2    3    4    5    6
    7    8
Sum = 30

代码如下:

import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int beginNum = in.nextInt();
			int endNum = in.nextInt();

			int sum = 0;
			int k = 0;
			for (int i = beginNum; i <= endNum; i++) {
				sum += i;
				System.out.printf("%5d", i);
				k++;
				if (k % 5 == 0) {
					System.out.println();
				} else if (i == endNum) {
					System.out.println();
				}
			}
			System.out.println("sum = " + sum);
		}
		in.close();
	}

}


L1-006. A-B

时间限制  100 ms    内存限制  65536 kB    代码长度限制  8000 B

本题要求你计算A-B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A-B。

输入格式:

输入在2行中先后给出字符串A和B。两字符串的长度都不超过104,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。

输出格式:

在一行中打印出A-B的结果字符串。

输入样例:
I love GPLT!  It's a fun game!
aeiou
输出样例:
I lv GPLT!  It's  fn gm!

代码如下:

import java.util.Scanner;

public class Main {

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

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		while (bf.ready()) {
			String s = bf.readLine();
			String ss = bf.readLine();

			System.out.println(s.replaceAll("[" + ss + "]", ""));
		}
		cin.close();
	}

}


L1-007. 跟奥巴马一起画方块(1)

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

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

输入格式:

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

输出格式:

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

输入样例:
10 a
输出样例:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
代码如下:
import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int n = in.nextInt();
			String ch = in.next();

			String newStr = "";
			for (int i = 0; i < n; i++) {
				newStr += ch;
			}

			for (int i = 0; i < (float) n / (float) 2; i++) {
				System.out.println(newStr);
			}
		}
		in.close();

	}
}


L1-008. 查验身份证

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

一个合法的身份证号码由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

代码如下:

import java.util.Scanner;

public class Main {

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

		// 前17位数的权重
		int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		// 校验码M的值
		char[] M = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

		while (in.hasNext()) {
			int N = in.nextInt();

			boolean flag = true;
			for (int i = 0; i < N; i++) {
				String str = in.next();
				int sum = 0;
				int is_wor = 0;
				for (int j = 0; j < 17; j++) {
					if (str.charAt(j) - '0' < 0 || str.charAt(j) - '0' > 9) {
						System.out.println(str);
						flag = false;
						is_wor = 1;
						break;
					}
					// 对前17位数字加权求和
					sum += ((str.charAt(j) - '0') * weight[j]);
				}
				// 11取模得到值
				int right = sum % 11;
				char m = M[right];
				// 检查前17位是否全为数字且最后1位校验码计算准确
				if (str.charAt(17) != m && is_wor == 0) {
					flag = false;
					System.out.println(str);
				}
			}
			// 如果所有号码都正常
			if (flag) {
				System.out.println("All passed");
			}
		}
		in.close();
	}

}


L1-009. 大笨钟

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉。不过由于笨钟自己作息也不是很规律,所以敲钟并不定时。一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那个整点数;如果过了整点,就敲下一个整点数。另外,虽然一天有24小时,钟却是只在后半天敲1~12下。例如在23:00敲钟,就是“当当当当当当当当当当当”,而到了23:01就会是“当当当当当当当当当当当当”。在午夜00:00到中午12:00期间(端点时间包括在内),笨钟是不敲的。

下面就请你写个程序,根据当前时间替大笨钟敲钟。

输入格式:

输入第一行按照“hh:mm”的格式给出当前时间。其中hh是小时,在00到23之间;mm是分钟,在00到59之间。

输出格式:

根据当前时间替大笨钟敲钟,即在一行中输出相应数量个“Dang”。如果不是敲钟期,则输出:

Only hh:mm.  Too early to Dang.

其中“hh:mm”是输入的时间。

输入样例1:
19:05
输出样例1:
DangDangDangDangDangDangDangDang
输入样例2:
07:05
输出样例2:
Only 07:05.  Too early to Dang.

代码如下:


import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			String str = in.next();

			int hh = (str.charAt(0) - '0') * 10 + str.charAt(1) - '0';
			int mm = (str.charAt(3) - '0') * 10 + str.charAt(4) - '0';

			if (hh <= 12 && mm == 0) {
				System.out.println("Only " + str + ".  Too early to Dang.");
			} else {
				for (int i = 0; i < hh - 12; i++) {
					System.out.print("duang");
				}
				if (mm > 0) {
					System.out.println("duang");
				}
			}
		}
		in.close();
	}
}


L1-010. 谁先倒

时间限制  400 ms    内存限制  65536 kB    代码长度限制  8000 B

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就输了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的酒量(最多能喝多少杯不倒)和划拳记录,请你判断两个人谁先倒。

输入格式:

输入第一行先后给出甲、乙两人的酒量(不超过100的非负整数),以空格分隔。下一行给出一个正整数N(<=100),随后N行,每行给出一轮划拳的记录,格式为:

甲喊 甲划 乙喊 乙划

其中“喊”是喊出的数字,“划”是划出的数字,均为不超过100的正整数(两只手一起划)。

输出格式:

在第一行中输出先倒下的那个人:A代表甲,B代表乙。第二行中输出没倒的那个人喝了多少杯。题目保证有一个人倒下。注意程序处理到有人倒下就终止,后面的数据不必处理。

输入样例:
1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16
输出样例:
A
1


代码如下:

import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int Achol = in.nextInt();
			int Bchol = in.nextInt();

			int jia = Achol, yi = Bchol;
			int n = in.nextInt();
			for (int i = 0; i < n; i++) {
				int Asay = in.nextInt();
				int Aaction = in.nextInt();
				int Bsay = in.nextInt();
				int Baction = in.nextInt();
				if (Aaction == Asay + Bsay && Baction != Asay + Bsay) {
					Achol--;
				}
				if (Aaction != Asay + Bsay && Baction == Asay + Bsay) {
					Bchol--;
				}
				if (Achol == -1) {
					System.out.println("A\n" + (yi - Bchol));
					break;
				}
				if (Bchol == -1) {
					System.out.println("B\n" + (jia - Achol));
					break;
				}
			}
		}
		in.close();
	}
}


L1-011. 帅到没朋友

时间限制  200 ms    内存限制  65536 kB    代码长度限制  8000 B

当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。

输入格式:

输入第一行给出一个正整数N(<=100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(<=1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。

输出格式:

按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出“No one is handsome”。

注意:同一个人可以被查询多次,但只输出一次。

输入样例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
输出样例2:
No one is handsome

代码如下:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {

			Map<Integer, Integer> map = new HashMap<>();
			int n = in.nextInt();
			int key = 0;
			for (int i = 0; i < n; i++) {
				int ni = in.nextInt();
				for (int j = 0; j < ni; j++) {
					key++;
					int id = in.nextInt();
					map.put(key, id);
				}
			}
			boolean flag = true;
			int m = in.nextInt();

			ArrayList<Integer> list = new ArrayList<>();
			for (int i = 0; i < m; i++) {
				int checkId = in.nextInt();
				if (!map.containsValue(checkId)) {
					flag = false;
					if (!list.contains(checkId)) {
						list.add(checkId);
					}
				}
			}
			for (int i = 0; i < list.size(); i++) {
				if (i < list.size() - 1)
					System.out.print(list.get(i) + " ");
				else
					System.out.println(list.get(i));
			}
			if (flag) {
				System.out.println("No one is handsome");
			}
		}
		in.close();
	}
}


L1-012. 输出GPLT

时间限制  150 ms    内存限制  65536 kB    代码长度限制  8000 B

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

输入格式:

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

输出格式:

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

输入样例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
GPLTGPLTGLTGLGLL
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		while (bf.ready()) {
			String str = bf.readLine();
			String newStr = str.toUpperCase();

			int GCount = 0, PCount = 0, LCount = 0, TCount = 0;
			for (int i = 0; i < newStr.length(); i++) {
				if (newStr.charAt(i) == 'G') {
					GCount++;
				} else if (newStr.charAt(i) == 'P') {
					PCount++;
				} else if (newStr.charAt(i) == 'L') {
					LCount++;
				} else if (newStr.charAt(i) == 'T') {
					TCount++;
				}
			}

			while (GCount > 0 || PCount > 0 || LCount > 0 || TCount > 0) {
				if (GCount > 0) {
					System.out.print("G");
					GCount--;
				}
				if (PCount > 0) {
					System.out.print("P");
					PCount--;
				}
				if (LCount > 0) {
					System.out.print("L");
					LCount--;
				}
				if (TCount > 0) {
					System.out.print("T");
					TCount--;
				}
			}
		}
		bf.close();
	}

}

L1-013. A+B和C 

时间限制 1000 ms      内存限制 32768 KB      代码长度限制 100 KB 

题目描述

给定区间[-2的31次方, 2的31次方]内的3个整数A、B和C,请判断A+B是否大于C。
输入描述:
输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。
输出描述:
对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。
输入例子:
4

1 2 3

2 3 4

2147483647 0 2147483646

0 -2147483648 -2147483647
输出例子:
Case #1: false

Case #2: true

Case #3: true

Case #4: false

代码如下:

import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
 
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] a = new int[n];
            // 输入n组三个整数
            int i = 0;
            while (n > 0) {
                // 输入A,B,C
                BigInteger A = in.nextBigInteger();
                BigInteger B = in.nextBigInteger();
                BigInteger C = in.nextBigInteger();
                // 判断A+B==C
                if (A.add(B).compareTo(C) > 0) {
                    a[i] = 1;
                } else {
                    a[i] = 0;
                }
                i++;
                n--;
            }
            // 输出结果
            for (int j = 0; j < a.length; j++) {
                if (a[j] == 1) {
                    System.out.println("Case #" + (j + 1) + ": true");
                } else {
                    System.out.println("Case #" + (j + 1) + ": false");
                }
            }
 
        }
        in.close();
    }
 
}

L1-014. 数字分类 

时间限制 1000 ms      内存限制 32768 KB      代码长度限制 100 KB 

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下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”。
输入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出例子:
30 11 2 9.7 9

代码如下:
import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int N = in.nextInt();
			int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
			int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
			for (int i = 1; i <= N; i++) {
				int n = in.nextInt();
				if (n % 5 == 0 && n % 2 == 0) {
					A1 += n;
					count1++;
				} else if (n % 5 == 1) {
					if ((count2 + 2) % 2 == 0) {
						A2 += n;
					} else {
						A2 -= n;
					}
					count2++;
				} else if (n % 5 == 2) {
					A3++;
					count3++;
				} else if (n % 5 == 3) {
					A4 += n;
					count4++;
				} else if (n % 5 == 4) {
					if (n > A5)
						A5 = n;
					count5++;
				}
			}
			float A4Sum = (float) A4 / (float) count4;
			String aver = String.format("%.1f", A4Sum);
			System.out.print(count1 == 0 ? "N" : A1);
			System.out.print(" " + (count2 == 0 ? "N" : A2));
			System.out.print(" " + (count3 == 0 ? "N" : A3));
			System.out.print(" " + (count4 == 0 ? "N" : aver));
			System.out.println(" " + (count5 == 0 ? "N" : A5));
		}
		in.close();
	}
}


L1-015. 数素数

时间限制 1000 ms     内存限制 32768 KB      代码长度限制 100 KB

题目描述

令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出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

代码如下:
import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int M = in.nextInt();
			int N = in.nextInt();
			int[] primeArr = new int[N - M + 1];
			int count = 0;
			int i = 0;
			// 获得第M到第N个素数
			for (int j = 2;; j++) {
				if (i == (N - M) + 1) {
					break;
				} else {
					if (prime(j)) {
						count++;
						if (count >= M) {
							primeArr[i] = j;
							i++;
						}
					}
				}
			}
			// 输出数组
			int k = 1;
			for (int j = 0; j < i; j++) {
				if (k % 10 == 0) {
					System.out.println(primeArr[j]);
				} else if (j == i - 1) {
					System.out.print(primeArr[j]);
				} else {
					System.out.print(primeArr[j] + " ");
				}
				k++;
			}
		}
		in.close();

	}

	/**
	 * 功能:判断是否为素数
	 * 
	 * @param 需判断的参数
	 * @return 返回boolean值
	 */
	private static boolean prime(int j) {
		for (int i = 2; i <= j / 2; i++) {
			if (j % i == 0)
				return false;
		}
		return true;
	}

}

L1-016. 部分A+B 

时间限制 1000 ms      内存限制 32768 KB      代码长度限制 100 KB 

题目描述

正整数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的值。
输入例子:
3862767 6 13530293 3
输出例子:
399

代码如下:
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int A = cin.nextInt();
		int DA = cin.nextInt();
		int B = cin.nextInt();
		int DB = cin.nextInt();
		int count_A = 0;
		int count_B = 0;
		int res_A = 0;
		int res_B = 0;
		while (A != 0) {
			if (DA == A % 10) {
				count_A++;
			}
			A /= 10;
		}
		while (B != 0) {
			if (DB == B % 10) {
				count_B++;
			}
			B /= 10;
		}
		for (int i = 0; i < count_A; i++) {
			res_A = res_A * 10 + DA;
		}
		for (int i = 0; i < count_B; i++) {
			res_B = res_B * 10 + DB;
		}
		System.out.println(res_A + res_B);
	}
}


L1-017. 锤子剪刀布 

时间限制 1000 ms      内存限制 32768 KB      代码长度限制 100 KB     

题目描述

大家应该都会玩“锤子剪刀布”的游戏:

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入描述:
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代

表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
输出描述:
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯

一,则输出按字母序最小的解。
输入例子:
10

C J

J B

C B

B B

B C

C C

C B

J B

B C

J J
输出例子:
5 3 2

2 3 5

B B
代码如下:
import java.util.Scanner;

public class Main {

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

		while (in.hasNext()) {
			int n = in.nextInt();
			int rel = 0;
			int AWin = 0, BWin = 0;
			int AWinByC = 0, AWinByB = 0, AWinByJ = 0;
			int BWinByC = 0, BWinByB = 0, BWinByJ = 0;
			for (int i = 0; i < n; i++) {
				String AShow = in.next(); // 甲
				String BShow = in.next(); // 乙
				// 1.双方打平
				if (AShow.equals(BShow)) {
					rel++;
				}
				// 2.甲出锤子时
				else if (AShow.equals("C")) {
					// 甲胜
					if (BShow.equals("J")) {
						AWin++;
						AWinByC++;
					} else {
						// 乙胜
						BWin++;
						BWinByB++;
					}
				}
				// 3.甲出剪刀时
				else if (AShow.equals("J")) {
					// 甲胜
					if (BShow.equals("B")) {
						AWin++;
						AWinByJ++;
					} else {
						// 乙胜
						BWin++;
						BWinByC++;
					}
				}
				// 4.甲出剪刀时
				else if (AShow.equals("B")) {
					// 甲胜
					if (BShow.equals("C")) {
						AWin++;
						AWinByB++;
					} else {
						// 乙胜
						BWin++;
						BWinByJ++;
					}
				}

			}
			// 输出甲乙的战绩分析表
			System.out.println(AWin + " " + rel + " " + (n - AWin - rel));
			System.out.println(BWin + " " + rel + " " + (n - BWin - rel));
			// 排序通过哪个赢的机率
			int AWinCountBy = sort(AWinByC, AWinByJ, AWinByB);
			int BWinCountBy = sort(BWinByC, BWinByJ, BWinByB);

			// 甲赢的绝招
			if (AWinCountBy == AWinByB) {
				System.out.print("B ");
			} else if (AWinCountBy == AWinByC) {
				System.out.print("C ");
			} else {
				System.out.print("J ");
			}
			// 乙赢的绝招
			if (BWinCountBy == BWinByB) {
				System.out.println("B");
			} else if (BWinCountBy == BWinByC) {
				System.out.println("C");
			} else {
				System.out.println("J");
			}
		}
		in.close();
	}

	/**
	 * 获得三个数的最大值
	 * 
	 * @param aWinByC
	 * @param aWinByJ
	 * @param aWinByB
	 * @return 返回最大值
	 */
	private static int sort(int aWinByC, int aWinByJ, int aWinByB) {
		int max = 0;
		if (aWinByJ > aWinByC)
			max = aWinByJ;
		else
			max = aWinByC;
		if (aWinByB > max)
			max = aWinByB;
		return max;
	}

}


L1-018. 月饼 

时间限制 1000 ms     内存限制 32768 KB      代码长度限制 100 KB 

题目描述

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需

求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、

72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得

 72 + 45/2 = 94.5(亿元)。
输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数

D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿

元为单位)。数字间以空格分隔。
输出描述:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
输入例子:
3 20

18 15 10

75 72 45
输出例子:
94.50
代码如下:
import java.util.Arrays;
import java.util.Scanner;

//设置类用于存储月饼的总价、库存量,以及计算其单价
class moCake implements Comparable<moCake>{
	int totalValue;
	int repertories;
	double price;
	@Override
	public int compareTo(moCake m) {
		//设置月饼的排序为单价从大到小的顺序
		return this.price>m.price ? -1:1;
	}
}

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

		Scanner in = new Scanner(System.in);
		//月饼种类数量,以及需求数量
		int num = in.nextInt();
		int requirement = in.nextInt();

		moCake[]  mc = new moCake[num];
		for (int i = 0; i < mc.length; i++) {
			//对对象操作之前,先将其实例化
			mc[i] = new moCake();
			mc[i].repertories = in.nextInt();
		}
		for (int i = 0; i < mc.length; i++) {
			mc[i].totalValue = in.nextInt();
		}
		in.close();
		//计算每个月饼的单价
		for (int i = 0; i < mc.length; i++) {
			mc[i].price = (double) mc[i].totalValue / mc[i].repertories;
		}
		//对月饼数组进行排序
		Arrays.sort(mc);
		
		double earning = 0.0;
		//从月饼单价最大开始遍历,直接满足需求
		for(int i = 0; i < mc.length; i++) {
			if (requirement > mc[i].repertories) {
				earning += mc[i].totalValue;
				requirement -= mc[i].repertories;
			}else {
				earning += ((double)requirement/mc[i].repertories) * mc[i].totalValue;
				break;
			}
		}
		System.out.printf("%.2f\n",earning);//格式化输出
	}

}


L1-019. D进制的A+B 

时间限制 1000 ms      内存限制 32768 KB     代码长度限制 100 KB 

题目描述

输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。
输入描述:
输入在一行中依次给出3个整数A、B和D。
输出描述:
输出A+B的D进制数。
输入例子:
123 456 8
输出例子:
1103

代码如下:
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		while (in.hasNext()) {
			BigInteger A = in.nextBigInteger();
			BigInteger B = in.nextBigInteger();
			BigInteger change = in.nextBigInteger();

			BigInteger[] rem = new BigInteger[100];

			int i = 0;
			BigInteger temp = A.add(B);
			while (temp.compareTo(BigInteger.ZERO) > 0) {
				i++;
				rem[i] = temp.remainder(change);
				temp = temp.divide(change);
			}

			for (int j = i; j > 0; j--) {
				System.out.print(rem[j]);
			}
		}
		in.close();
	}

}


L1-020. 跟奥巴马一起编程(2)

时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB

题目描述

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算

机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
输入描述:
输入在一行中给出正方形边长N(3<=N<=20)和组成正方形边的某种字符C,间隔一个空格。
输出描述:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%

(四舍五入取整)。
输入例子:
10 a
输出例子:
aaaaaaaaaa

a a

a a

a a

aaaaaaaaaa
代码如下:
import java.util.Scanner;

public class Main {
    
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String c = in.next();
		double m;
		double m1 = n / 2.0;
		int m2 = n / 2;
		if ((m1 - m2) >= 0.5) {
			m = m2 + 1;
		} else {
			m = m2;
		}
		//
		for (int i = 0; i < n; i++) {
			System.out.print(c);
		}
		System.out.println();
		for (int i = 0; i < m - 2; i++) {
			System.out.print(c);
			for (int j = 0; j < n - 2; j++) {
				System.out.print(" ");
			}
			System.out.print(c);
			System.out.println();
		}
		for (int i = 0; i < n; i++) {
			System.out.print(c);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_38931949/article/details/79750358