• PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌


    00-自测1. 打印沙漏(20)
    本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
    
    *****
     ***
      *
     ***
    *****
    所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
    
    给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
    
    输入格式:
    
    输入在一行给出1个正整数N(<=1000)和一个符号,中间以空格分隔。
    
    输出格式:
    
    首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
    
    输入样例:
    19 *
    输出样例:
    *****
     ***
      *
     ***
    *****
    2
    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int n, half, i, j;
        char c;
        
        scanf("%d %c", &n, &c);
        half = sqrt((n+1)/2.0);
        for(i = half; i >= 1; i--)
        {
            for(j = 1; j <= half - i; j++)
                putchar(' ');
            for(j = 1; j <= 2*i - 1; j++)
                putchar(c);
            putchar('
    ');
        }
        for(i = 2; i <= half; i++)
        {
            for(j = 1; j <= half - i; j++)
                putchar(' '); 
            for(j = 1; j <= 2*i - 1; j++)
                putchar(c);
            putchar('
    ');
        }
        printf("%d
    ", n - (2*half*half - 1)); 
        return 0;
    }
    
    /*
    注意到,我们设所有的正三角部分的符号共占据n 行,那么符号总数为
    1 + 2·3 + 2·5 + 2·7 + 2·(2n-1) = 2(1+3+5+...+2n-1) - 1 = 2n^2 - 1.
    因此,n 的值应该是(给定的符号总数+1)/2 向下取整的值。
    */
    00-自测2. 素数对猜想 (20)
    让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
    
    现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。
    
    输入格式:每个测试输入包含1个测试用例,给出正整数N。
    
    输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。
    
    输入样例:
    20
    输出样例:
    4
    // 最容易想到的方法当然是暴力求解了:~ 11MS
    #include<stdio.h>
    #include<math.h>
    int IsPrime(int num);
    int main(void)
    {
        int n, count, i;
        
        scanf("%d", &n);
        
        count = 0;
        for(i = 2; i < n-1; i++)
            if(IsPrime(i) && IsPrime(i+2))
                count++;
        
        printf("%d
    ", count);
    
        return 0;
    }
    
    int IsPrime(int num)
    {
        int i, bound = sqrt(num);
        for(i = 2; i <= bound; i++)
            if(num % i == 0)
                return 0;
        
        return 1;
    }
    // 升级版:素数筛,1MS
    #include<stdio.h>
    #include<string.h>
    #define MAXN 100000
    int arr[MAXN+10];
    int prime[MAXN+10];
    int main(void)
    {
        int n, i, j, count;
        
        memset(arr, 0, sizeof(arr));
        scanf("%d", &n);
        for(i = 2; i*i <= n; i++)
            if(arr[i] == 0)
                for(j = i*i; j <= n; j += i)
                    arr[j] = 1;
        
        j = 0;
        count = 0;
        for(i = 2; i <= n; i++)
            if(arr[i] == 0)
                prime[j++] = i;
        for(i = 1; i < j; i++)
            if(prime[i] - prime[i-1] == 2)    
                count++;    
        printf("%d
    ", count);
        return 0;
    }
    00-自测3. 数组元素循环右移问题 (20)
    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
    
    输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。
    
    输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
    
    输入样例:
    6 2
    1 2 3 4 5 6
    输出样例:
    5 6 1 2 3 4
    // 注意到循环一圈的话,相当于数组元素没有移动,因此可以利用模运算来尽可能的减少数据移动的次数
    #include<stdio.h>
    int main(void)
    {
        int arr[100];
        int n, m, i, j, temp, cir;
        
        scanf("%d%d", &n, &m);
        for(i = 0; i < n; i++)
            scanf("%d", &arr[i]);
        
        cir = m % n;
        for(i = 0; i < cir; i++)
        {
            temp = arr[n-1];
            for(j = n-2; j >= 0; j--)
                arr[j+1] = arr[j];
            arr[0] = temp;
        }
        
        for(i = 0; i < n; i++)
        {
            if(i == 0)
                printf("%d", arr[i]);
            else
                printf(" %d", arr[i]);
        }
        
        return 0;
    }
    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[25], dest[25], temp[25];
        int length, i, c, change;
        
        scanf("%s", str);
        length = strlen(str);
        for(i = 0; i < length / 2; i++)
        {
            change = str[i];
            str[i] = str[length-1-i];
            str[length-1-i] = change;
        }
        
        c = 0;
        for(i = 0; i < length; i++)
        {
            dest[i] = (str[i]-'0')*2 + c;
            c = dest[i] / 10;
            dest[i] = dest[i] % 10 + '0';
        }         
        if(c != 0)
        {
            dest[i] = c + '0';
            dest[i+1] = '';
            printf("No
    ");
        }
        else
        {
            dest[i] = '';
            i = 0;
            while((temp[i] = dest[i]) != '')
                i++;
            bbsort(str, length);
            bbsort(temp, length);
            for(i = 0; i < length; i++)
                if(temp[i] != str[i])
                    break;
            if(i == length)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
    
        length = strlen(dest);
        for(i = length - 1; i >= 0; i--)
            putchar(dest[i]);
        return 0;
    }
    
    void bbsort(char s[], int length)
    {
        int temp, outer, inner;
        
        for(outer = length - 1; outer > 0; outer--)
            for(inner = 0; inner < outer; inner++)
                if(s[inner] < s[inner+1])
                {
                    temp = s[inner];
                    s[inner] = s[inner+1];
                    s[inner+1] = temp;
                }
                            
    }
    /*
    思路:
    1.读入字符数组,因为存储形式是从高位到低位,所以要做一下反转
    2.加倍,注意这里我采用了字符的形式存储数字,因为单纯用数字存储的话,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[55], temp[55], dest[55];
        int n, i;
        
        scanf("%d", &n);
        for(i = 1; i <= 54; i++)
            scanf("%d", &arr[i]);
        for(i = 1; i <= 54; i++)
            dest[i] = i;
        while(n--)
        {
            for(i = 1; i <= 54; i++)
                temp[arr[i]] = dest[i];
                
            for(i = 1; i <= 54; i++)
                dest[i] = temp[i];
        }
        for(i = 1; i <= 54; i++)
        {
            if(dest[i] == 53)
                printf("J1");
            else if(dest[i] == 54)
                printf("J2");
            else if(dest[i] <= 13)
                printf("S%d", dest[i]);
            else if(13 < dest[i] && dest[i] <= 26)
                printf("H%d", dest[i]-13);
            else if(26 < dest[i] && dest[i] <= 39)
                printf("C%d", dest[i] - 26);
            else
                printf("D%d", dest[i] - 39);
            
            if(i != 54)
                printf(" ");
                
        }
        return 0;
    }
    /*
    思路:
    1.开两个数组,一个顺序存储1-54,每个数字代表相应的牌,另一个用于存储给出的洗牌序列
    2.根据给定的洗牌次数进行洗牌,再开一个数组,存储每一论洗牌的中间结果,最后再复制到源数组中
    3.根据洗完牌后的数组数字代表的相应的牌进行输出
    */



    (END_XPJIANG)
  • 相关阅读:
    解决知乎pc端频繁展现视频问题
    (转)求树的直径
    (转)海量数据处理专题
    const用法小结
    codeblocks中更改gnometerminal终端调试方法
    谷歌收购摩托罗拉移动
    (转)详解sizeof
    (转)位计算的诸多算法(计算二进制1的个数)
    (转)glibc中字符串函数源码分析
    (转)typedef的用法
  • 原文地址:https://www.cnblogs.com/xpjiang/p/4556826.html
Copyright © 2020-2023  润新知