中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题

自测-1 打印沙漏

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

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

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

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

输入格式

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

输出格式

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

输入样例

19 *

输出样例

*****
 ***
  *
 ***
*****
2
#include<iostream>
using namespace std;
void draw(int layer,char c);
int count(int &n);
int main(){
	int n;
	char c;
	cin>>n>>c;

	int layer=count(n);//数层数
	draw(layer,c);//画漏斗

	cout<<n<<endl;//输出剩下没用掉的符号数
	return 0;
}
int count(int &n){//直接传n的地址,经过处理后n的值就是剩下没用掉的符号数。
	n--;//先减去中间那一层
	int i;//i就是层数
	for(i=1;n-2*(i*2+1)>=0;i++){
		n-=2*(i*2+1);//每个循环里n减去两个奇数。
	}
	return i;
}
void draw(int layer,char c){
	for(int i=layer;i>=1;i--){//打印漏斗的上半部分。
		for(int j=0;j<layer-i;j++){//每行打印(层数-i)个空格
			cout<<' ';
		}
		for(int j=0;j<i*2-1;j++){//打印(2i-1)个符号
			cout<<c;
		}
		cout<<endl;
	}
	for(int i=2;i<=layer;i++){//打印漏斗的下半部分,不含瓶颈(i从2开始)。循环体跟上面那个一样。
		for(int j=0;j<layer-i;j++){
			cout<<' ';
		}
		for(int j=0;j<i*2-1;j++){
			cout<<c;
		}
		cout<<endl;
	}
}

自测-2 素数对猜想

让我们定义 d n d_n dn​​为: d n = p n + 1 − p n d_n=p_n+1-p_n dn=pn+1pn​​ ,其中 p i p_i pi是第 i i i个素数。显然有 d 1 = 1 d_1=1 d1=1,且对于 n > 1 n>1 n>1 d n d_n dn是偶数。
“素数对猜想”认为“存在无穷多对相邻且差为 2 2 2的素数”。
现给定任意正整数 N ( < 1 0 5 ) N(<10^5) N(<105),请计算不超过 N N N的满足猜想的素数对的个数。

输入格式
输入在一行给出正整数N。
输出格式
在一行中输出不超过N的满足猜想的素数对的个数。

输入样例

20

输出样例

4
#include <iostream>
using namespace std;
bool isPrime(int n);
int main(){
	int N;
	cin>>N;
	bool idx=0;
	int tmp[2]={2};//第一个素数是2
	int cnt=0;
	int i=0;
	while(i<=N){
		for(i=tmp[idx]+1;i<=N;i++){//从上一个已知素数的下一个数开始,找素数。
			if(isPrime(i)){
				tmp[!idx]=i;//给数组的另一个元素赋值
				idx=!idx;//更新下标
				if(tmp[idx]-tmp[!idx]==2){//这次找到的素数跟上一个素数的差是2
					cnt++;
				}
				break;
			}
		}
	}
	cout<<cnt<<endl;
}

自测-3 数组元素循环右移问题

一个数组 A A A中存有 N ( > 0 ) N(>0) N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移 M ( > 0 ) M(>0) M(>0)个位置,即将 A A A中的数据由 ( A 0 A 1 ⋯ A N − 1 ) (A_0A_1\cdots A_{N-1}) (A0A1AN1)变换为 ( A N − M ⋯ A N − 1 A 0 A 1 ⋯ A N − M − 1 ) (A_{N-M}\cdots A_{N-1}A_0A_1\cdots A_{N-M-1}) (ANMAN1A0A1ANM1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式

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

输出格式

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

输入样例

6 2
1 2 3 4 5 6

输出样例

5 6 1 2 3 4
#include<iostream>
using namespace std;
int main(){
	int N,M;
	cin>>N>>M;
	int arr[N],arr_shifted[N];
	for(int i=0;i<N;i++){
		cin>>arr[i];
	}
	for(int i=0;i<N;i++){//建立了个新数组,直接赋值。
		arr_shifted[(i+M)%N]=arr[i];
	}
	for(int i=0;i<N-1;i++){
		cout<<arr_shifted[i]<<' ';
	}
	cout<<arr_shifted[N-1];
}

自测-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.

输入格式

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

输出格式

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.

输入样例

1234567899

输出样例

Yes
2469135798
#include<iostream>
#include<cstring>
using namespace std;
void format(int *dst,const char *src,int size);//字符串转数字
void multiple(int *num,int mul,int size);//乘法
void count(const int *num,int stat[10],int size);//统计
bool compare(int stat[2][10]);//比较
int main(){
	char input[21]={0};
	cin>>input;
	int size=strlen(input);//获得输入的位数,便于以后操作

	int num[22]={0};
	format(num,input,size);//把输入的字符串转成从低位到高位的数字

	int stat[2][10]={{0}};//存放统计数据
	count(num,stat[0],size);//统计每个数字的出现次数

	multiple(num,2,size);//乘以2
	count(num,stat[1],size+!!num[size]);//如果num[size]不为0则说明乘2之后的数字多了一位

	cout<<(compare(stat)?"Yes":"No")<<endl;
	for(int i=size-!num[size];i>=0;i--){
		cout<<num[i];
	}
	return 0;
}
void count(const int *num,int stat[10],int size){
	for(int i=0;i<size;i++){
		stat[num[i]]++;
	}
}
bool compare(int stat[2][10]){
	for(int i=0;i<10;i++){
		if(stat[0][i]!=stat[1][i]){
			return false;
		}
	}
	return true;
}
void multiple(int *num,int mul,int size){
	for(int i=0;i<size;i++){
		num[i]*=mul;
	}
	for(int i=0;i<size+1;i++){//进位
		if(num[i]>=10){
			num[i+1]+=num[i]/10;
			num[i]%=10;
		}
	}
}
void format(int *dst,const char *src,int size){
	for(int i=0;src[i];i++){
		dst[i]=src[size-i-1]-'0';
	}
}

自测-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.

输入格式

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.

输出格式

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.

输入样例

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
#include<iostream>
using namespace std;
int finalIdx(const int *order,int idx,int times){//根据题意,找出第idx张卡牌经过times次洗牌后的位置。
	if(times==0) return idx;
	else return finalIdx(order, order[idx], times-1);
}
void shuffle(int *card,const int *order,int K){
	for(int i=1;i<=54;i++){	
		int idx=finalIdx(order,i,K);
		card[idx]=i;//把i处的卡牌移动到order[i]处。在一开始,i处的卡牌编号就是i。
	}
}
int main(){
	int K;
	cin>>K;
	int order[55];
	int card[55];
	for(int i=1;i<=54;i++){
		cin>>order[i];//输入洗牌规则
	}
	shuffle(card,order,K);//按照规则洗牌K次
	for(int i=1;i<=54;i++){//输出
		char suit[5]={'S','H','C','D','J'};//根据题意,相同花色的卡牌在一开始是连续存储的。
		cout<<suit[(card[i]-1)/13]<<(card[i]-1)%13+1;//因为编号不是从0开始的,所以要-1。
		if(i!=54)cout<<' ';
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值