PTA A1001&A1002

Starting today Brush Grade 1-2 PAT questions every day

A1001 A+B Format (20 分)

Title Contents

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

word

commas

n. [Language] commas (comma complex)

Topic analysis

Inputs two numbers, the result of the calculation to the standard output, a comma standard format (here, a plug, personal feeling here that every three digits is less stringent, particularly because not every three numbers from the beginning or the end from the beginning, I thought it was from the beginning considered, as is the 2000, 200,0, but this problem is actually counted from the end of 2000 should be 2,000, and the input and output sample can not be seen in the end It is counted from the beginning or the end of the count, cause I could not have been very hard to accept AC).
If the title is not like I misread it, this question should be two ways, one is digital processing, combine to take more than rounding operator slowly output, with the first time I was done this way. The second string is treated with a string sprintf digital conversion, and then inserted from the end of every three a ',', similar to the insertion sequence table, to be positive or negative case, because the first symbol is the negative -.

Specific code

#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 分)

Title Contents

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

word

polynomial

English /, pɒlɪ'nəʊmɪəl / US /, pɑlɪ'nomɪəl /

. n polynomial; multi-word Latin name; represents any number and the number of
adj multiple, multi-word; of polynomials

exponent

English / ɪk'spəʊnənt; ek- / US / ɪk'sponənt /

n [number] index; Typically; stated, was described

n. advocate, advocates, representatives, advocates
adj. illustration

coefficient

英 / kəʊɪfɪʃ (a) nt / 美 / koʊəfɪʃənt /

. n-[c] coefficient; rate; cofactors
adj cooperation;. interacting

decimal

英 / 'desɪm (g) l / 美 /' dɛsɪml /

. n-decimal
adj decimal;. Decimal

Topic analysis

Polynomial addition, since the small index range, an array can be constructed, with the subscripts represent index, coefficient corresponding element, so the code can be simplified very much.
At first I used a slightly stupid way to do a three array of structures, and its index is based on the default input mode decreasing, so there have been two test points had not, and now also the error code together posted.

Specific code

correct

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

int main(void)
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int e;
        double c;
        scanf("%d %lf", &e, &c);
        m[e] += c;
    }
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int e;
        double c;
        scanf("%d %lf", &e, &c);
        m[e] += c;
    }
    for (int i = 0; i < MAXSIZE; i++)
        if (m[i] != 0)
          k++;
    printf("%d", k);
    for (int i = MAXSIZE; i >= 0; i--)
    {
        if (m[i] != 0)
        {
            printf(" %d %.1f",i,m[i]);
        }
    }
}

  
  
 
 

error

#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 < M; i++)
   
        scanf("%d %f", &A[i].e, &A[i].c);
   
    scanf("%d", &N);
   
    for (int i = 0; i < N; i++)
   
        scanf("%d %f", &B[i].e, &B[i].c);
   
    int p = 0, q = 0;
   
    while (p != M && q != N)
   
    {
   
        if (A[p].e > B[q].e)
   
        {
   
            C[K].c = A[p].c;
   
            C [K] .and = A [t] .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 < B[q].e)
        {
            C[K].c = B[p].c;
            C[K].e = B[p].e;
            K++;
            q++;
        }
    }
    if(p==M)
        while (q != N)
        {
            C[K].c = B[p].c;
            C[K].e = B[p].e;
            K++;
            q++;
        }
    else if(q==N)
        while (p != M)
        {
            C[K].c = A[p].c;
            C[K].e = A[p].e;
            K++;
            p++;
        }
    printf("%d ", K);
    for (int i = 0; i < K; i++)
    {
        if (i == K - 1)
            printf("%d %.1f", C[i].e, C[i].c);
        else
            printf("%d %.1f ", C[i].e, C[i].c);
    }
}
  
  
 
 

Guess you like

Origin www.cnblogs.com/z-y-k/p/11517248.html