1-1 打印沙漏
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
***** *** * *** *****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
***** *** * *** ***** 2
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
思路:确定上三角行数 1 + 3 + 5 +(2 * n - 1) = n * n,则2 * n * n - 1 <= N,行数即为sqrt( (n + 1) / 2 )。
注意:倒三角的符号格式要求为什么是2 * m - 2 * i + 1?
正三角的空格格式要求为什么是 m - i?
答:对称性分析即可,保证上三角和下三角对应格式要求相加 / 2 = m
即可。
//数学题。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
char c;
cin >> c;
int m = sqrt( (n + 1) / 2 );//m为行数;一定要用int单拎出来,直接算会导致数据错误。
//倒三角
for (int i = 1; i < m; i ++ ){
for (int j = 1; j <= i - 1; j ++ )
cout << " ";
for (int j = 1; j <= 2 * m - 2 * i + 1; j ++ )
cout << c;
puts("");
}
//正三角
for (int i = 1; i <= m; i ++ ){
for (int j = 1; j <= m - i ; j ++ )
cout << " ";
for (int j = 1; j <= 2 * i - 1; j ++ )
cout << c;
puts("");
}
int ans = n - (m * m * 2 - 1);//一定要用int单拎出来,直接算会导致数据错误。
cout << ans;
return 0;
}
1-2 素数对猜想
让我们定义dn为:dn=p(n+1)−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数
N
(<105),请计算不超过N
的满足猜想的素数对的个数。输入格式:
输入在一行给出正整数
N
。输出格式:
在一行中输出不超过
N
的满足猜想的素数对的个数。输入样例:
20
输出样例:
4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
思路:
判断素数,遍历判断 。
(数据小,打表即可,但空间可能会超)
#include<bits/stdc++.h>
using namespace std;
int judge(int p){
if(p == 1) return 0;
for(int i = 2;i * i <=p;i ++ ){
if(p % i == 0) return 0;
}
return 1;
}
int main(){
int n;
cin >> n;
int ans = 0;
for(int i = 2 ;i <= n - 2 ; i ++ )
if( judge(i) && judge(i + 2) ) ans ++ ;
cout << ans;
return 0;
}
1-3 数组元素循环右移问题
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(A(N−M)⋯A(N−1)A0A1⋯A(N−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
思路分析:反转数组
#include <bits/stdc++.h>
using namespace std;
const int n = 1e2 + 10;
void re(int a[n],int l,int r){
int t;
while(l <= r){
t = a[r];
a[r -- ] = a[l];
a[l ++ ] = t;
}
}
int main(){
int N ,M;
cin >> N >> M;
int a[n];
for(int i = 0;i < N;i ++ ) cin >> a[i];
M = M % N;
re(a , 0 , N - 1);re(a , 0 , M - 1);re(a , M , N - 1);//反转实现。
for(int i = 0;i < N;i ++ ) {
if(i == N - 1) cout << a[i];//防止输入最后一个空格。
else cout << a[i] << " ";
}
return 0;
}
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.
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
翻译:有那么一种数,将其加倍(乘以2)后的数字仍然是原数字组合而成,现在就是要让你从控制台输入一个数字,然后你需要判断它是否满足上述条件,满足:输出Yes+加倍后的数字,不满足:输出No+加倍后的数字
思路分析:
1.先%s输入,再统计输入了几位数 。
2.对于操作后的数组进位。
3.储存两数组数字出现的次数。
4.比对次数,有一个比对不上即不符合。
5.输出操作后的数组。
#include <bits/stdc++.h>
using namespace std;
const int n = 21;
int main(){
char num[n];
scanf("%s", num);
int a[n], b[n];
int count = 0;//输入了几位数。
for (int i = 0 ;num[i] != '\0'; i ++ ){
a[i] = num[i] - '0';
b[i] = a[i] * 2;
count ++ ;
}
for (int i = count - 1; i > 0; i--){
if (b[i] >= 10){
b[i] -= 10;
b[i-1] += 1;//进位。
}
}
int cnt1[10]{0}, cnt2[10]{0};//储存数字出现的次数。(千万别忘记赋初值!!)
for (int i = 1; i <= 9; i ++ ){
for(int j = 0; j < count; j ++ ){
if (a[j] == i) cnt1[i] ++ ;
if (b[j] == i) cnt2[i] ++ ;
}
}
int flag = 0;//比对数字出现的次数。
for(int i = 0; i < 10; i ++ ) if (cnt1[i] != cnt2[i]) flag = 1;
if (flag == 1) cout << "No" <<endl;
else cout << "Yes" <<endl;
for (int i = 0; i < count; i ++ ) cout << b[i];
return 0;
}
1-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
翻译:有一副牌,从第1到第54张牌依次是黑桃1~13、红桃1~13、梅花1~13、方片1~13和大小王,记为S1~13,H1~13,C1~13,D1~13,J1,J2,要求你写一段代码模拟自动洗牌机,给定洗牌次数和洗后牌的位置。例如S3, H5, C1, D13,J2. 洗牌顺序{4, 2, 5, 3, 1}, 洗后的结果就是 J2, H5, D13, S3, C1. 如果再以上面顺序重复洗牌一次,结果就是 C1, H5, S3, J2, D13。
思路分析:
1. b[ order[j] - 1] = a[j]:对应洗牌顺序。
2.别忘了洗完牌之后要更新一下字典,将新卡牌顺序作为下次循环前的默认顺序。
3.最后一个数据没有空格。
#include<bits/stdc++.h>
using namespace std;
const int n = 54;
int main(){
string a[n]={"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13","H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13","C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13","D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","J1","J2" };
string b[n];
int t;
cin >> t;
int order[n];
for(int i = 0;i < n;i ++ ) cin >> order[i];
for(int i = 0;i < t;i ++ ){
for(int j = 0;j < n;j ++ )
b[ order[j] - 1] = a[j];//洗牌,移动卡片位置
for(int j = 0;j < n;j ++ ){
a[j] = b[j];//将新卡牌顺序作为下次循环前的默认顺序
}
}
for(int i = 0;i < n;i ++ ) {
cout << b[i] ;
if ( i < 53 ) cout << " ";
}
return 0;
}