C语言-翁恺-PTA-41-80课后练习题-02


title: C语言-翁恺-PTA-41-80课后练习题-02
tags:

  • PTA
  • C
    description: ’ ’
    mathjax: true
    date: 2024-04-02 11:50:25
    categories:
  • PTA
  • C

第二个40题,还是

7-42 整除光棍

这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。

提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。

输入格式:

输入在一行中给出一个不以5结尾的正奇数x(<1000)。

输出格式:

在一行中输出相应的最小的sn,其间以1个空格分隔。

输入样例:

31

输出样例:

3584229390681 15

代码长度限制

错误原因

7-42这题也是一点思路都没有,这种是我最讨厌的题,没有思路,就不太会写。

这题数学题,我确实是笨蛋,一点点思路都没有

好吧,但是实际做起来,比我想象中的简单,也只要通过遍历即可,

11,111,1111,这样加上去,代码如下:

别人的代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
   
    int x, y = 1;
    scanf("%d", &x);    //读取X
    int s = 1;         //用来表示光棍数
    int n = 0;         //记录位数
    while (s < x) {
   
        s = s * 10 + 1;  // 先找出大于x的最小光棍数
        n++;
    }
    do {
   
        y = s % x;  // 储存余数
        printf("%d", s / x);
        s = y * 10 + 1;  // 更新光棍数
        n++;
    } while (y != 0);  // 如果余数为0,说明找到了光棍数字,直接结束循环
    printf(" %d", n);  // 输出位数
    return 0;
}

7-43 Shuffling Machine

看这个B题目,和看天书一样,我真的服我自己,英语学的别太烂

下面是原来的题目,我会通过翻译软件翻译一下

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

错误原因

不会看题,题目看不懂

Shuffling Machine

洗牌机

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:

该机器按照给定的随机顺序洗一副 54 张牌,并重复给定的次数。假设一副牌的初始状态按以下顺序排列:

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.

其中 "S "代表 “黑桃”,"H "代表 “红心”,"C "代表 “梅花”,"D "代表 “方块”,"J "代表 “小丑”。给定的顺序是[1, 54]中不同整数的排列。例如,假设我们只有 5 张牌: S3、H5、C1、D13 和 J2。给定一个洗牌顺序{4,2,5,3,1},结果将是 J2、H5、D13、S3、C1。如果我们再重复一次洗牌,结果将是 C1、H5、S3、J2、D13。

这里很恶心的这个B翻译软件,有一句话,没有翻译出来

If the number at the i-th position is j, it means to move the card from position i to position j.

如果第 i 个位置的数字是 j,则表示将牌从第 i 个位置移动到第 j 个位置。这一句话,可以不翻译的是吧,B翻译软件

做反而是好做的

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
   
    int card[55] = {
    0 }, pcard[55] = {
    0 }, random[55] = {
    0 },k=0;
    for (int i = 0; i < 55; ++i)   card[i] = i;
    scanf("%d", &k);
    for (int i = 1; i <= 54; ++i)
        scanf("%d", &random[i]);
    while (k) {
   
        for (int i = 1; i <= 54; ++i) {
   
            pcard[random[i]] = card[i];
        }
        for (int i = 1; i <= 54; ++i)
            card[i] = pcard[i];
        k--;
    }
    for (int i = 1; i <= 54; ++i) {
   
        if (card[i] >= 1 && card[i] <= 13) printf("S%d", card[i]);
        else if (card[i] > 13 && card[i] <= 26)   printf("H%d", card[i] - 13);
        else if (card[i] > 26 && card[i] <= 39)  printf("C%d", card[i] - 26);
        else if (card[i] > 39 && card[i] <= 52)  printf("D%d", card[i] - 39);
        else printf("J%d", card[i] - 52);
        if (i!=54)    printf(" ");
    }
    return 0;
}

7-49 Have Fun with Numbers

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 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

这题也是,题目看不太懂,但是比43题要好的多的多得多

逐句翻译

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication

请注意,数字 123456789 是一个 9 位数,正好由 1 到 9 的数字组成,没有重复的数字

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

将它加倍,我们将得到 246913578,这恰好是另一个 9 位数,完全由 1 到 9 的数字组成,只是排列方式不同而已

Check to see the result if we double it again!

现在,您需要检查是否有更多数字具有此属性

也就是说,将一个有 k 位数字的给定数字加倍,你要知道得到的数字是否只由原始数字中的数字排列而成。

错误原因

一直在解决那个运算大小的问题,这个我一直不清楚

错误代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int a[11] = {
    0 };
void adda(unsigned long long x) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值