自测-1 打印沙漏(20 分)
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
*****
***
*
***
*****
2
题解
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 读取最多总数N
String symbol = sc.next(); // 读取符号
int n = (int) Math.sqrt((N + 1) / 2);
/*
假设一个完整三角形n行,那么总数为
1+2*3+2*5+...+2*(2n-1)=2n^2-1<=N
n<=sqrt((N+1)/2) 向下取整
*/
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(' ');
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(symbol);
}
System.out.println();
} // 第一个完整倒三角形
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(' ');
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(symbol);
}
System.out.println();
} // 第二个缺顶点正三角形
System.out.print(N - 2 * n * n + 1); // 输出余数
}
}
自测-2 素数对猜想(20 分)
00-自测2. 素数对猜想 (20)
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。
输入格式:每个测试输入包含1个测试用例,给出正整数N。
输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
import java.util.Scanner;
public class Main {
public static boolean IsPrime(int num) {
int bound = (int) Math.sqrt(num);
for (int i = 2; i <= bound; i++)
if (num % i == 0) {
return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int count = 0;
for (int i = 2; i < N - 1; i++)
if (IsPrime(i) && IsPrime(i + 2)) {
count++;
}
System.out.println(count);
}
}
00-自测3. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。
输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int array[] = new int[100];
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 0; i < n; i++)
array[i] = sc.nextInt();
int circle = m % n;
//注意到循环一圈的话,相当于数组元素没有移动,
//因此可以利用模运算来尽可能的减少数据移动的次数
for (int i = 0; i < circle; i++) { //移动次数
int temp = array[n - 1];//temp为数组最后一个数
for (int j = n - 2; j >= 0; j--)
array[j + 1] = array[j]; //从后向前移动一次
array[0] = temp;
}
for (int i = 0; i < n; i++) {
if (i == 0)
System.out.print(array[i]);
else
System.out.print(" " + array[i]);
}
}
}
00-自测4. Have Fun with Numbers (20)
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 file 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
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String numbers = sc.next();
int n = numbers.length();
int num[] = new int[n];
for (int i = 0; i < n; i++) {
num[i] = numbers.charAt(i) - 48; //按顺序读取字符,-48得到值
}
// System.out.println(num[0]);
int newnum[] = new int[n]; // 创建新数组,和原来一样大小,如果超出,额外输出1
boolean addone = false;
boolean top = false;
int tmp;
for (int i = n - 1; i >= 0; i--) {
if (addone) {
tmp = num[i] * 2 + 1;
addone = false;
} else {
tmp = num[i] * 2;
}
if (tmp >= 10) {
addone = true;
newnum[i] = tmp - 10;
if (i == 0) {
top = true;
}
} else {
newnum[i] = tmp;
}
}
if (top) { //超出的情况
System.out.println("No");
System.out.print(1);
for (int i = 0; i < n; i++) {
System.out.print(newnum[i]);
}
} else { //两个数组先排序再一次比较
boolean flag = true;
int copynewnum[]=newnum.clone(); //保留原数组
Arrays.sort(num);
Arrays.sort(newnum);
for (int i = 0; i < n; i++) {
if(num[i]!=newnum[i]) {
flag = false;
}
}
if(flag) {
System.out.println("Yes");
}else {
System.out.println("No");
}
for (int i = 0; i < n; i++) {
System.out.print(copynewnum[i]);
}
}
}
}
00-自测5. Shuffling Machine (20)
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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int repeat = sc.nextInt(); // 读取重复数
int rule[] = new int[55];
int tmp[] = new int[55]; // 临时数组
for(int i = 1; i <= 54; i++) {
rule[i] = sc.nextInt(); // 读取规则列表,0号不用
}
int init[] = new int[55];
for(int i = 1; i <= 54; i++) {
init[i] = i; // 初始值,未乱序之前
}
while(repeat!=0) { //开始乱序
for(int i=1; i<=54;i++) {
tmp[rule[i]] = init[i]; //临时数组的 等于初始值的第i位置
}
for(int i=1; i<=54; i++) {
init[i] =tmp[i]; // 将临时数组复制给初始数组
}
repeat--;
}
for(int i=1; i<=54; i++) {
if(init[i]==54)System.out.print("J2");
if(init[i]==53)System.out.print("J1");
if(init[i]>=1&&init[i]<=13)System.out.print("S"+init[i]);
if(init[i]>=14&&init[i]<=26)System.out.print("H"+(init[i]-13));
if(init[i]>=27&&init[i]<=39)System.out.print("C"+(init[i]-26));
if(init[i]>=40&&init[i]<=52)System.out.print("D"+(init[i]-39));
if(i!=54)System.out.print(" ");
}
}
}
网友评论