PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌
-自测1. 打印沙漏()
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印 *****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。 给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。 输入格式: 输入在一行给出1个正整数N(<=)和一个符号,中间以空格分隔。 输出格式: 首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。 输入样例:
*
输出样例:
*****
***
*
***
*****
#include<stdio.h>
#include<math.h>
int main(void)
{
int n, half, i, j;
char c; scanf("%d %c", &n, &c);
half = sqrt((n+)/2.0);
for(i = half; i >= ; i--)
{
for(j = ; j <= half - i; j++)
putchar(' ');
for(j = ; j <= *i - ; j++)
putchar(c);
putchar('\n');
}
for(i = ; i <= half; i++)
{
for(j = ; j <= half - i; j++)
putchar(' ');
for(j = ; j <= *i - ; j++)
putchar(c);
putchar('\n');
}
printf("%d\n", n - (*half*half - ));
return ;
} /*
注意到,我们设所有的正三角部分的符号共占据n 行,那么符号总数为
1 + 2·3 + 2·5 + 2·7 + 2·(2n-1) = 2(1+3+5+...+2n-1) - 1 = 2n^2 - 1.
因此,n 的值应该是(给定的符号总数+1)/2 向下取整的值。
*/
-自测2. 素数对猜想 ()
让我们定义 dn 为:dn = pn+ - pn,其中 pi 是第i个素数。显然有 d1= 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。 现给定任意正整数N (< ),请计算不超过N的满足猜想的素数对的个数。 输入格式:每个测试输入包含1个测试用例,给出正整数N。 输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。 输入样例: 输出样例:
// 最容易想到的方法当然是暴力求解了:~ 11MS
#include<stdio.h>
#include<math.h>
int IsPrime(int num);
int main(void)
{
int n, count, i; scanf("%d", &n); count = ;
for(i = ; i < n-; i++)
if(IsPrime(i) && IsPrime(i+))
count++; printf("%d\n", count); return ;
} int IsPrime(int num)
{
int i, bound = sqrt(num);
for(i = ; i <= bound; i++)
if(num % i == )
return ; return ;
}
// 升级版:素数筛,1MS
#include<stdio.h>
#include<string.h>
#define MAXN 100000
int arr[MAXN+];
int prime[MAXN+];
int main(void)
{
int n, i, j, count; memset(arr, , sizeof(arr));
scanf("%d", &n);
for(i = ; i*i <= n; i++)
if(arr[i] == )
for(j = i*i; j <= n; j += i)
arr[j] = ; j = ;
count = ;
for(i = ; i <= n; i++)
if(arr[i] == )
prime[j++] = i;
for(i = ; i < j; i++)
if(prime[i] - prime[i-] == )
count++;
printf("%d\n", count);
return ;
}
-自测3. 数组元素循环右移问题 ()
一个数组A中存有N(N>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=)个位置,即将A中的数据由(A0 A1……AN-)变换为(AN-M …… AN- A0 A1……AN-M-)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法? 输入格式:每个输入包含一个测试用例,第1行输入N ( <=N<=)、M(M>=);第2行输入N个整数,之间用空格分隔。 输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。 输入样例: 输出样例:
// 注意到循环一圈的话,相当于数组元素没有移动,因此可以利用模运算来尽可能的减少数据移动的次数
#include<stdio.h>
int main(void)
{
int arr[];
int n, m, i, j, temp, cir; scanf("%d%d", &n, &m);
for(i = ; i < n; i++)
scanf("%d", &arr[i]); cir = m % n;
for(i = ; i < cir; i++)
{
temp = arr[n-];
for(j = n-; j >= ; j--)
arr[j+] = arr[j];
arr[] = temp;
} for(i = ; i < n; i++)
{
if(i == )
printf("%d", arr[i]);
else
printf(" %d", arr[i]);
} return ;
}
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
#include<stdio.h>
#include<string.h>
void bbsort(char[], int);
int main(void)
{
char str[], dest[], temp[];
int length, i, c, change; scanf("%s", str);
length = strlen(str);
for(i = ; i < length / ; i++)
{
change = str[i];
str[i] = str[length--i];
str[length--i] = change;
} c = ;
for(i = ; i < length; i++)
{
dest[i] = (str[i]-'')* + c;
c = dest[i] / ;
dest[i] = dest[i] % + '';
}
if(c != )
{
dest[i] = c + '';
dest[i+] = '\0';
printf("No\n");
}
else
{
dest[i] = '\0';
i = ;
while((temp[i] = dest[i]) != '\0')
i++;
bbsort(str, length);
bbsort(temp, length);
for(i = ; i < length; i++)
if(temp[i] != str[i])
break;
if(i == length)
printf("Yes\n");
else
printf("No\n");
} length = strlen(dest);
for(i = length - ; i >= ; i--)
putchar(dest[i]);
return ;
} void bbsort(char s[], int length)
{
int temp, outer, inner; for(outer = length - ; outer > ; outer--)
for(inner = ; inner < outer; inner++)
if(s[inner] < s[inner+])
{
temp = s[inner];
s[inner] = s[inner+];
s[inner+] = temp;
} }
/*
思路:
1.读入字符数组,因为存储形式是从高位到低位,所以要做一下反转
2.加倍,注意这里我采用了字符的形式存储数字,因为单纯用数字存储的话,0是一个问题,可能引起麻烦,因为它和'\0'相等
3.如果有进位,直接输出"No",否则,分别对加倍之前和加倍之后的数组排序,接下来逐一比较即可
*/
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
#include<stdio.h>
int main(void)
{
int arr[], temp[], dest[];
int n, i; scanf("%d", &n);
for(i = ; i <= ; i++)
scanf("%d", &arr[i]);
for(i = ; i <= ; i++)
dest[i] = i;
while(n--)
{
for(i = ; i <= ; i++)
temp[arr[i]] = dest[i]; for(i = ; i <= ; i++)
dest[i] = temp[i];
}
for(i = ; i <= ; i++)
{
if(dest[i] == )
printf("J1");
else if(dest[i] == )
printf("J2");
else if(dest[i] <= )
printf("S%d", dest[i]);
else if( < dest[i] && dest[i] <= )
printf("H%d", dest[i]-);
else if( < dest[i] && dest[i] <= )
printf("C%d", dest[i] - );
else
printf("D%d", dest[i] - ); if(i != )
printf(" "); }
return ;
}
/*
思路:
1.开两个数组,一个顺序存储1-54,每个数字代表相应的牌,另一个用于存储给出的洗牌序列
2.根据给定的洗牌次数进行洗牌,再开一个数组,存储每一论洗牌的中间结果,最后再复制到源数组中
3.根据洗完牌后的数组数字代表的相应的牌进行输出
*/ (END_XPJIANG)
PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌的更多相关文章
- PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808 #include <iost ...
- PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 ...
- PTA自测-3 数组元素循环右移问题
自测-3 数组元素循环右移问题 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M≥0)个位置,即将A中的数据由(A0A1···AN-1)变换为 ...
- 【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...
- pat00-自测3. 数组元素循环右移问题 (20)
00-自测3. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在 ...
- PAT (Basic Level) Practice 1008 数组元素循环右移问题
个人练习 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN ...
- PAT 1008. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...
- PAT乙级 1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- PAT (Basic Level) Practise:1008. 数组元素循环右移问题
[题目连接] 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… A ...
随机推荐
- HDU 2069 Coin Change(完全背包变种)
题意:给你5种银币,50 25 10 5 1,问你可以拼成x的所有可能情况个数,注意总个数不超过100个 组合数问题,一看就是完全背包问题,关键就是总数不超过100个.所有我们开二维dp[k][j], ...
- 单色半透明-兼容IE7
background: #000; width: 100%;height: 100%; filter: alpha(opacity=30); opacity: 0.3;
- Spring Boot 部署与后台运行服务配置
http://blog.csdn.net/catoop/article/details/50588851
- self.automaticallyAdjustsScrollViewInsets
导航视图内Push进来的以“TableView”(没有ScrollView截图,就将就一下)为主View的视图,本来我们的cell是放在(0,0)的位置上的,但是考虑到导航栏.状态栏会挡住后面的主视图 ...
- Theano:LSTM源码解析
最难读的Theano代码 这份LSTM代码的作者,感觉和前面Tutorial代码作者不是同一个人.对于Theano.Python的手法使用得非常娴熟. 尤其是在两重并行设计上: ①LSTM各个门之间并 ...
- MongoDB命令及SQL语法对比
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- BOM,文档宽高及窗口事件小析
(一)BOM:Browser Object Model(浏览器对象模型)页面上所有的变量都是window的属性 一.方法:1. open(,)打开一个新窗口(页面)一参为页面地址url,二参为打开方式 ...
- iOS开发之GCD
GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制.它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务. GCD中的FIFO队列称为 ...
- ubuntu segmentation fault 段错误
1.段错误 http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 2.段错误的调试方法 2.1使用printf输出信息 2 ...
- C语言
HTML的学习早已落下帷幕,我们已经进入了C语言的学习,这段时间时间主要学了运算符.表达式.循环语句以及数组和字符串,感觉到了一种朦朦胧胧懂得尴尬. 运算符主要包括:算术运算符.赋值运算符.关系运算符 ...