-自测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自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌的更多相关文章

  1. PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808 #include <iost ...

  2. PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0  ...

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

    自测-3 数组元素循环右移问题  一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M≥0)个位置,即将A中的数据由(A0A1···A​N-1​​)变换为 ...

  4. 【PAT】1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...

  5. pat00-自测3. 数组元素循环右移问题 (20)

    00-自测3. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在 ...

  6. PAT (Basic Level) Practice 1008 数组元素循环右移问题

    个人练习 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N ...

  7. PAT 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...

  8. PAT乙级 1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  9. PAT (Basic Level) Practise:1008. 数组元素循环右移问题

    [题目连接] 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… A ...

随机推荐

  1. 纯CSS实现二级下拉导航菜单

    这是一款纯CSS菜单,二级下拉导航效果,是最简洁的CSS导航菜单,兼容性也很棒,IE7/8.火狐等都支持,而且它还是学习CSS菜单编写的典型教程,让你学会很多CSS技巧. 运行效果截图如下: < ...

  2. 在DrawingVisual上绘制圆形的进度条,类似于IOS系统风格。

    1.说明:在WPF中,文件下载时需要显示下载进度,由于系统自带的条型进度条比较占用空间,改用圆形的进度条,需要在DrawingVisual上呈现. 运行的效果如图: private Point Get ...

  3. 使用Git进行项目管理

    首先在https://git.oschina.net进行注册以及登陆 登陆进去之后,如果想要创建项目,可以在 点击加号按钮,进行项目创建 3.这里以创建私有项目为例: 输入完成后,点击“创建”,进入下 ...

  4. win10启动无法进入桌面

    情况: windows启动显示欢迎界面 无法进入桌面(可以win+E进入资源管理器,可以ctl+alt+delete进入任务管理器) 重启依然无法进入 解决: 重启 按f8 进入安全模式 再次重启OK ...

  5. javascript平时小例子⑧(导航置顶效果)

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  6. 浅谈WebLogic和Tomcat

    J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...

  7. Struts2中的ModelDriven机制及其运用

    所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2.actions; public cla ...

  8. 批量创建AD测试账号

    在现场中,有时候客户会要求做一下AD压力测试,需要批量创建很多AD用户.奉献此代码供各位参考.   1: <# 2:   3: .DESCRIPTION 4: 批量创建AD测试账号 5:   6 ...

  9. 10款最新流行的 jQuery 插件,值得你收藏

    10款最新流行的 jQuery 插件,值得你收藏 http://www.cnblogs.com/lhb25/p/10-new-popular-jquery-plugins-check.html 你应该 ...

  10. apache 自带的ab测试

    ab -c 20 -n 2000 http://192.168.1.110:8080/index.php