Java实现 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题题题解
自测-1 打印沙漏
作者 陈越 单位 浙江大学
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
***** *** * *** *****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
***** *** * *** ***** 2
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
String val = scan.next();
int level = (int) Math.sqrt((num + 1) / 2);
int over = num +1-(int)Math.pow(level,2)*2;
for(int i =0;i<level;i++){
for(int j=0;j<i;j++)
System.out.print(" ");
for(int j=0;j<2*(level-i)-1;j++){
System.out.print(val);
}
System.out.println();
}
for(int i =2;i<=level;i++) {
for (int j = 0; j < level-i; j++)
System.out.print(" ");
for (int j = 0; j < 2 * i- 1; j++) {
System.out.print(val);
}
System.out.println();
}
System.out.println(over);
scan.close();
};
}
自测-2 素数对猜想
作者 陈越 单位 浙江大学
让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N。
输出格式:
在一行中输出不超过N的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int count=0;
for (int i=2;i<N;i++){
if( isPrime(i)==true&&isPrime(i+2)==true&&(i+2)<=N)
{
//System.out.print(i+" ");
count++;
};
}
System.out.print(count);
scan.close();
};
public static boolean isPrime(int n){
if(n1)
{
return n>1;
}
if((n%6!=1)&&(n%6!=5)){
return false;
}
for(int i = 5;i<=Math.sqrt(n);i+=6)
{
if(n%i==0||n%(i+2)==0){
return false;
}
}
return true;
}
}
自测-3 数组元素循环右移问题
作者 DS课程组 单位 浙江大学
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN−M−1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:
每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。
输出格式:
在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
6 2 1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
int []num = new int[N];
for(int i = 0;i<N;i++)
{
if((i+M)=N ){
num[(i+M)%N]=scan.nextInt();
}
}
for(int i = 0;i<N;i++)
{
if(i==N-1 ){
System.out.print(num[i]);
break;
}
System.out.print(num[i]+" ");
}
scan.close();
}
}
自测-4 Have Fun with Numbers
作者 陈越 单位 浙江大学
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes 2469135798
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
代码:
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str1 = scan.next();
BigInteger num1 = new BigInteger(str1);
BigInteger num2 = new BigInteger("2");
num2 = num2.multiply(num1);
String str2 = num2.toString();
System.out.print(order(str1).equals(order(str2))?"Yes"+'\n'+str2:"No"+'\n'+str2);
scan.close();
}
private static String order(String str){
char[] chars = str.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
}
自测-5 Shuffling Machine
作者 陈越 单位 浙江大学
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.
The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:
S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2
where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.
Sample Input:
2 36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
Sample Output:
S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Integer Repeat = scan.nextInt();
int []order = new int[54];
int []temp = new int[54];
for(int i=0;i<54;i++){
order[i]=scan.nextInt();
}
/*s1:1-13
h1:14-26
c1:27-39
D1:40-52
j1:53-54
* */
int [] poke = new int[54];
for(int i=0;i<54;i++){
poke[i]=i+1;
}
for(int i =0;i<Repeat;i++){
for(int j=0;j<54;j++){
int num = order[j]-1;
temp[num]= poke[j];
}
for(int k =0;k<54;k++){
poke[k] = temp[k];
}
}
for(int i = 0;i=1&&poke[i]=14&&poke[i]=27&&poke[i]=40&&poke[i]=53&&poke[i]<=54)
System.out.print(i!=53?"J"+(poke[i]-52)+" ":"J"+(poke[i]-52));
}
scan.close();
}
}
自测四与自测五的chatgpt翻译:
自测-4 与数字玩耍
分数:20
作者:陈越 单位:浙江大学
注意到数字123456789是一个9位数,由数字1到9组成,没有重复数字。将它翻倍得到246913578,这也是另一个9位数,由数字1到9组成,只是排列顺序不同。现在,请检查如果我们再次将其翻倍会得到什么结果!
现在,你要检查是否还有更多具有这种特性的数字。也就是说,对于一个具有k位数字的给定数字,你需要判断所得的数字是否仅由原始数字的数字排列组合而成。
输入规范: 每个输入包含一个测试用例。每个测试用例包含一个不超过20位数字的正整数。
输出规范: 对于每个测试用例,如果将输入数字翻倍得到的数字仅由原始数字的数字排列组合而成,则首先在一行中打印”Yes”,否则打印”No”。 接下来的一行中,打印翻倍后的数字。
示例输入:
1234567899
示例输出:
Yes 2469135798
自测-5 洗牌机器
分数:20
作者:陈越 单位:浙江大学
洗牌是用来打乱一副扑克牌的过程。由于标准的洗牌技术被认为不够安全,并且为了避免“内部勾结”——员工与赌徒合作通过不充分的洗牌进行作弊,许多赌场采用了自动洗牌机。你的任务是模拟一个洗牌机器。
该机器根据给定的随机顺序对一副54张牌进行洗牌,并重复一定次数。假设一副牌的初始状态如下:
S1, S2, …, S13, H1, H2, …, H13, C1, C2, …, C13, D1, D2, …, D13, J1, J2
其中 “S” 代表 “Spade”,”H” 代表 “Heart”,”C” 代表 “Club”,”D” 代表 “Diamond”,”J” 代表 “Joker”。给定的顺序是[1, 54]范围内不同整数的排列。如果第i个位置的数字是j,表示将第i个位置的牌移动到第j个位置。例如,假设我们只有5张牌:S3, H5, C1, D13 和 J2。给定的洗牌顺序是{4, 2, 5, 3, 1},那么结果将是:J2, H5, D13, S3, C1。如果我们要再次洗牌,结果将是:C1, H5, S3, J2, D13。
输入规范: 每个输入文件包含一个测试用例。对于每个案例,第一行包含一个正整数K(≤20),表示重复洗牌的次数。接下来一行包含给定的顺序。每行中的所有数字由空格分隔。
输出规范: 对于每个测试用例,将洗牌结果以一行打印出来。所有的牌用空格分隔,行末没有额外的空格。
示例输入:
2 36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
示例输出:
S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/424ac99927.html
