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 ...
随机推荐
- 同一台机子上用多个git 账号
Step 1 - Create a New SSH Key We need to generate a unique SSH key for our second GitHub account. 1 ...
- CodeForces 519B A and B and Compilation Errors
B. A and B and Compilation Errors time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【贪心】POJ 1065
头一次接触POJ,然后写了自己比较擅长的贪心. 解题思路大概就是从小排(这个很重要,然后用cmp随便长度或者重量的排序,选择最小的开始) 直到所有比他weight大的,没有符合条件的了.就代表要再加一 ...
- java.lang.NoClassDefFoundError:
异常信息:十一月 10, 2016 5:20:15 下午 org.apache.catalina.core.StandardContext loadOnStartup严重: Servlet /mgr ...
- ZeroMQ接口函数之 :zmq_proxy – 开始ZMQ内置代理
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq-proxy zmq_proxy(3) ØMQ Manual - ØMQ/4.1.0 Nam ...
- Node.js exports与module.exports的关系
今天搜索module.exports时看到CNode社区上发的Hack Sparrow一篇相关文章的链接 Node.js Module – exports vs module.exports 一篇5年 ...
- 【Alpha】Daily Scrum Meeting第七次
一.本次Daily Scrum Meeting主要内容 各队员的任务完成情况 文件选择器布局只是暂时使用,后期会改方式,放在后面解决. 接下去都要做什么 二.项目进展 学号尾数 今日已完成任务 接下去 ...
- javascript面向对象(一):封装
本文来自阮一峰 学习Javascript,最难的地方是什么? 我觉得,Object(对象)最难.因为Javascript的Object模型很独特,和其他语言都不一样,初学者不容易掌握. 下面就是我的学 ...
- SharedPreferences介绍,用来做数据存储
sharedPreferences是通过xml文件来做数据存储的. 一般用来存放一些标记性的数据,一些设置信息. *********使用sharedPreferences存储数据 public sta ...
- c# winform编程之多线程ui界面资源修改总结篇
单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello World!";就搞定了,但是如果在一个新线程中这么 ...