-自测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. 【算法杂谈】LJX的迪杰斯特拉算法报告

    迪杰斯特拉(di jie qi)算法 这里有一张图: 假设要求从1号节点到5号节点的最短路.那么根据迪杰斯特拉算法的思想,我们先看: 节点1,从节点1出发的一共有3条路,分别是1-6.1-3.1-2. ...

  2. React的双向绑定

    以前对于双向绑定概念来自于Angular.js,现在我用我感兴趣的react.js来实现这样的方式.有2种方式分析,1:不用插件,2:用插件 (引入react.js操作省略...) 不用插件: 先创建 ...

  3. maven ClassNotFoundException: org.springframework.web.context.ContextLoaderL

    信息: Starting Servlet Engine: Apache Tomcat/6.0.32 2012-3-31 9:39:40 org.apache.catalina.core.Standar ...

  4. Leetcode Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  5. SpringMVC核心分发器DispatcherServlet分析[附带源码分析]

    目录 前言 DispatcherServlet初始化过程 DispatcherServlet处理请求过程 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不 ...

  6. ActiveMQ集群应用

    ActiveMQ集群 ActiveMQ具有强大和灵活的集群功能,但在使用的过程中会发现很多的缺点,ActiveMQ的集群方式主要由两种:Master-Slave和Broker Cluster. 1.M ...

  7. [IOS] 利用@IBInspectable

    某些uiview中设置 这个关键字 IBInspectable 可以让其设置的属性,在右侧的属性栏目里面进行直接设置, 这是最近看了一下wwdc的一个视频学习到的,可以方便的进行 UI的测试,

  8. HTML 5 拖放(Drag 和drop)

    浏览器支持 Internet Explorer 9.Firefox.Opera 12.Chrome 以及 Safari 5. 1.把标签 draggable 属性设置为 true. 2.向标签添加on ...

  9. 2016huasacm暑假集训训练四 DP_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/M 题意:有N件物品和一个容量为V的背包.第i件物品的费用是体积c[i],价值是w[ ...

  10. 使用XML文件记录操作日志,并从后往前读取操作日志并在richTextBox1控件中显示出来

    #region 获取本地程序操作记录日志 /// <summary> /// 获取本地程序更新日志信息(由后往前读取) /// </summary> private void ...