PTA A1001&A1002

从今天起每天刷1-2题PAT甲级

A1001 A+B Format (20 分)

题目内容

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where ?106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

单词

commas

n. [语] 逗号(comma的复数)

题目分析

输入两个数,计算结果后以标准格式输出,标准格式为每隔三个数字加一个逗号(这里插一句,个人感觉这里说的不太严谨,因为并没有具体说每隔三个数字是从末尾开始还是从开头开始,我一开始以为是从开头算,如2000就是,200,0,但是这题实际上是要从结尾算,2000应该是2,000,而且输入输出样例也无法看出到底是从开头算还是结尾算,导致我一直AC不了很难受)。
如果题目没有像我一样误读的话,这题应该有两种方法,一种是处理数字,结合取余取整运算符慢慢输出,我第一次做时用的是这种方法。第二种是处理字符串,用sprintf把数字转化为字符串,然后从末尾每隔三个插入一个‘,‘,类似于顺序表插入,要分正负情况,因为负数时第一个符号是-。

具体代码

#include
#include
#define MAXSIZE 20
int main(void)
{
    int a, b;
    scanf("%d %d", &a, &b);
    char C[MAXSIZE];
    sprintf(C, "%d", a + b);
    int M = 0;
    while (C[M] != ‘\0‘)M++;
    int N = M;
    if (a + b > 0)
    {
        for (int n = 1; (M - 3 * n) > 0; n++)
        {
            for (int m = N - 1; m >= M - 3 * n; m--)
                C[m + 1] = C[m];
            C[M - 3 * n] = ‘,‘; N++;
        }
    }
    else
    {
        for (int n = 1; (M - 3 * n) > 1; n++)
        {
            for (int m = N - 1; m >= M - 3 * n; m--)
                C[m + 1] = C[m];
            C[M - 3 * n] = ‘,‘; N++;
        }
    }
    C[N] = ‘\0‘;
    printf("%s", C);
    system("pause");
}

1002 A+B for Polynomials (25 分)

题目内容

Input Specification:

ach input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2... NKaNK
where K is the number of nonzero terms in the polynomial, Niand aNi(i=1,2,?,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<?<N2<N1≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

单词

polynomial

英 /,p?l?‘n??m??l/ 美 /,pɑl?‘nom??l/

n. 多项式;多词拉丁学名;表示任何多项数之和
adj. 多项的,多词的;多项式的

exponent

英 /?k‘sp??n?nt; ek-/ 美 /?k‘spon?nt/

n. [数] 指数;典型;说明者,说明物

n. 倡导者,鼓吹者,代表者,拥护者
adj. 说明的

coefficient

英 /,k???‘f??(?)nt/ 美 /ko???f???nt/

n. [数] 系数;率;协同因素
adj. 合作的;共同作用的

decimal

英 /‘des?m(?)l/ 美 /‘d?s?ml/

n. 小数
adj. 小数的;十进位的

题目分析

多项式相加,由于指数范围不大,所以可以构造一个数组,用下标代表指数,对应元素为系数,这样的话可以把代码简化非常多。
一开始我用的是一种略笨的方法,做了三个结构体数组,并且默认其指数是以递减的方式输入的,所以一直有两个测试点过不了,现在把错误代码也一并贴出。

具体代码

正确

#include
#include
#define MAXSIZE 1001
int n;
double m[MAXSIZE];
int k=0;

int main(void)
{
    scanf("%d", &n);
    for (int i = 0; i = 0; i--)
    {
        if (m[i] != 0)
        {
            printf(" %d %.1f",i,m[i]);
        }
    }
}

错误

#include
#include
#define maxsize 10

struct a {
int e;
float c;
};

struct a A[maxsize];
struct a B[maxsize];
struct a C[maxsize];

int M, N, K ;

int main(void)
{
int M, N;
scanf("%d", &M);
for (int i = 0; i B[q].e)
{
C[K].c = A[p].c;
C[K].e = A[p].e;
K++;
p++;
}
else if (A[p].e == B[q].e)
{
C[K].c = A[p].c + B[q].c;
C[K].e = A[p].e;
K++;
p++;
q++;
}
else if (A[p].e

原文地址:https://www.cnblogs.com/z-y-k/p/11517248.html

时间: 2024-10-13 09:16:38

PTA A1001&A1002的相关文章

ERROR&lt;53761&gt; - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Directory/6.3 B2008.0311.0224 (32-bit) starting up[04/May/2016:21:10:39 +0800] - Listening on all interfaces port 11111 for LDAP requests[04/May/2016:21:10

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

PTA Huffman Codes

题目重现 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am

PTA 10-排序6 Sort with Swap(0, i) (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given any permutation of the numbers {0, 1, 2,..., N-1N?1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

PTA 09-排序2 Insert or Merge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort rem

PTA 10-排序5 PAT Judge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Spe

PTA 09-排序1 排序 (25分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序   (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大排序后的结果. 本题旨在测试各种不同的排序算法在各种数据情况下的表现.各组测试数据特点如下: 数据1:只有1个元素: 数据2:11个不相同的整数,测试基本正确性: 数据3:103个随机整数: 数据4:104个随机整数: 数据5:105个随机整数: 数据6:105个顺序整数: 数据7:105个逆序整数

PTA 09-排序3 Insertion or Heap Sort (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s