PAT_(数学问题)汇总 -3 1096 1059 B1017 1023 1024

目录

1096 Consecutive Factors (20分)

1059 Prime Factors (25分)

B1017 A除以B (20分)

1023 Have Fun with Numbers (20分)

1024 Palindromic Number (25分)


 

质因子分解

1096 Consecutive Factors (20分)

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<2^​31​​).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
5*6*7

思路:暴力枚举 连续的乘积因子,维护一个最大长度的乘积因子

#include <iostream>
#include <cmath>
using namespace std;
long int num, temp;
int main(){
    cin >> num;
    int first = 0, len = 0, maxn = sqrt(num*1.0) + 1;

    for (int i = 2; i <= maxn; i++) { //暴力枚举 连续的乘积因子
        int j; 
        temp = 1;
        for (j = i; j <= maxn; j++) {
            temp *= j;
            if (num % temp != 0) break;
        }
        if (j - i > len) {
            len = j - i;
            first = i;
        }
    }
    if (first == 0) {
        cout << 1 << endl << num;
    } else {
        cout << len << endl;
        for (int i = 0; i < len; i++) {
            cout << first + i;
            if (i != len - 1) cout << '*';
        }
    }
    return 0;
}

 

1059 Prime Factors (25分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​^k​1​​​​×p​2^​​​k​2​​​​×⋯×p​m​​​^k​m​​​​.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int (即:int)

Output Specification:

Factor N in the format N = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

分析:根号int的最大值不超过50000,先建立个50000以内的素数表,然后从2开始一直判断是否为它的素数,如果是就将a=a/i继续判断i是否为a的素数,判断完成后输出这个素数因子和个数,用state判断是否输入过因子,输入过就要再前面输出“*” 

#include <cstdio>
#include <vector>
using namespace std;
vector<int> prime(50000, 1);
int main() {
    for(int i = 2; i * i < 50000; i++)
        for(int j = 2; j * i < 50000; j++)
            prime[j * i] = 0;
    long int a;
    scanf("%ld", &a);
    printf("%ld=", a);
    if(a == 1) printf("1");
    bool state = false;
    for(int i = 2; i < 50000 && a >= 2; i++) {
        int cnt = 0, flag = 0;
        while(prime[i] == 1 && a % i == 0) {
            cnt++;
            a = a / i;
            flag = 1;
        }
        if(flag) {
            if(state) printf("*");
            printf("%d", i);
            state = true;
        }
        if(cnt >= 2) printf("^%d", cnt);
    }
    if (a > 1) printf("%s%ld", state ? "*" : "", a);
    return 0;
}

大整数运算

B1017 A除以B (20分)

本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。

输入格式:

输入在一行中依次给出 A 和 B,中间以 1 空格分隔。

输出格式:

在一行中依次输出 Q 和 R,中间以 1 空格分隔。

输入样例:

123456789050987654321 7

输出样例:

17636684150141093474 3

分析:模拟手动除法的过程,每次用第一位去除以B,如果得到的商不是0就输出,否则就*10+下一位,直到最后的数为余数~

#include <iostream>
using namespace std;
int main() {
    string s;
    int a, t = 0, temp = 0;
    cin >> s >> a;
    int len = s.length();
    t = (s[0] - '0') / a;
    if ((t != 0 && len > 1) || len == 1)
        cout << t;
    temp = (s[0] - '0') % a;
    for (int i = 1; i < len; i++) {
        t = (temp * 10 + s[i] - '0') / a;
        cout << t;
        temp = (temp * 10 + s[i] - '0') % a;
    }
    cout << " " << temp;
    return 0;
}

 

1023 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 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 <cstdio>
#include <string.h>
using namespace std;
int book[10];
int main() {
    char num[22];
    scanf("%s", num);
    int flag = 0, len = strlen(num);
    for(int i = len - 1; i >= 0; i--) {
        int temp = num[i] - '0';
        book[temp]++;
        temp = temp * 2 + flag;
        flag = 0;
        if(temp >= 10) {
            temp = temp - 10;
            flag = 1;
        }
        num[i] = (temp + '0');
        book[temp]--;
    }
    int flag1 = 0;
    for(int i = 0; i < 10; i++) {
        if(book[i] != 0)
            flag1 = 1;
    }
    printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
    if(flag == 1) printf("1"); //注意点 下面例子
    printf("%s", num);
    return 0;
}
/**
9987654
No
19975308
*/

 

 

 

1024 Palindromic Number (25分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number.   All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations.

First, the non-palindromic number is reversed and the result is added to the original number.

If the result is not a palindromic number, this is repeated until it gives a palindromic number.

For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K,

where N (≤10​^10​​) is the initial numer and K (≤100) is the maximum number of steps.

The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

分析:

1、会超出long int类型(会有两个点溢出错误),所以用字符串存储,大整数相加
2、可以通过对字符串翻转后比较来判断是否为回文串

#include <iostream>
#include <algorithm>
using namespace std;
string s;
void add(string t) {
    int len = s.length(), carry = 0;
    for(int i = 0; i < len; i++) {
        s[i] = s[i] + t[i] + carry - '0';
        carry = 0;
        if(s[i] > '9') {
            s[i] = s[i] - 10;
            carry = 1;
        }
    }
    if(carry) s += '1';
    reverse(s.begin(), s.end());
}
int main() {
    int cnt, i;
    cin >> s >> cnt;
    for(i = 0; i <= cnt; i++) {
        string t = s;
        reverse(t.begin(), t.end());
        if(s == t || i == cnt) break;
        add(t);
    }
    cout << s << endl << i;
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
REGISTER ADDRESS REGISTER DATA(1) HEX 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 0 LVDS_ RATE_2X 0 0 0 0 0 0 0 0 0 0 0 0 0 GLOBAL_ PDN 2 PAT_MODES_FCLK[2:0] LOW_ LATENCY_E N AVG_EN SEL_PRBS_ PAT_ FCLK PAT_MODES SEL_PRBS_ PAT_GBL OFFSET_CORR_DELAY_FROM_TX_TRIG[5:0] 3 SER_DATA_RATE DIG_GAIN_ EN 0 OFFSET_CORR_DELAY _FROM_TX_TRIG[7:6] DIG_ OFFSET_ EN 0 0 0 1 0 0 0 0 4 OFFSET_ REMOVA L_SELF OFFSET_ REMOVAL_ START_ SEL OFFEST_ REMOVAL_ START_ MANUAL AUTO_OFFSET_REMOVAL_ACC_CYCLES[3:0] PAT_ SELECT_ IND PRBS_ SYNC PRBS_ MODE PRBS_EN MSB_ FIRST DATA_ FORMAT 0 ADC_RES 5 CUSTOM_PATTERN 7 AUTO_OFFSET_REMOVAL_VAL_RD_CH_SEL 0 0 0 0 0 0 0 0 0 0 CHOPPER_EN 8 0 0 AUTO_OFFSET_REMOVAL_VAL_RD B 0 0 0 0 EN_ DITHER 0 0 0 0 0 0 0 0 0 0 0 D GAIN_ADC1o 0 OFFSET_ADC1o E GAIN_ADC1e 0 OFFSET_ADC1e F GAIN_ADC2o 0 OFFSET_ADC2o 10 GAIN_ADC2e 0 OFFSET_ADC2e 11 GAIN_ADC3o 0 OFFSET_ADC3o 12 GAIN_ADC3e 0 OFFSET_ADC3e 13 GAIN_ADC4o 0 OFFSET_ADC4o 14 GAIN_ADC4e 0 OFFSET_ADC4e 15 PAT_PRB S_LVDS1 PAT_PRBS_ LVDS2 PAT_PRBS_ LVDS3 PAT_PRBS_ LVDS4 PAT_LVDS1 PAT_LVDS2 HPF_ ROUND_ EN_ADC1-8 HPF_CORNER_ADC1-4 DIG_HPF_ EN_ADC1-4 17 0 0 0 0 0 0 0 0 PAT_LVDS3 PAT_LVDS4 0 0 18 0 0 0 0 PDN_ LVDS4 PDN_ LVDS3 PDN_ LVDS2 PDN_ LVDS1 0 0 0 0 INVERT_ LVDS4 INVERT_ LVDS3 INVERT_ LVDS2 INVERT_ LVDS1 19 GAIN_ADC5o 0 OFFSET_ADC5o 1A GAIN_ADC5e 0 OFFSET_ADC5e 1B GAIN_ADC6o 0 OFFSET_ADC6o 1C GAIN_ADC6e 0 OFFSET_ADC6e 1D GAIN_ADC7o 0 OFFSET_ADC7o 1E GAIN_ADC7e 0 OFFSET_ADC7e 1F GAIN_ADC8o 0 OFFSET_ADC8o 20 GAIN_ADC8e 0 OFFSET_ADC8e 21 PAT_PRB S_LVDS5 PAT_PRBS_ LVDS6 PAT_PRBS_ LVDS7 PAT_PRBS_ LVDS8 PAT_LVDS5 PAT_LVDS6 0 HPF_CORNER_ADC5-8 DIG_HPF_ EN_ADC5-8 23 0 0 0 0 0 0 0 0 PAT_LVDS7 PAT_LVDS8 0 0
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值