目录
1002 A+B for Polynomials (25分)
备用知识点梳理:
具体题目(1001~1115)如下:
1001 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
题意:
对两个整数a、b求和,结果按照一定规范输出:三个数一组,用逗号隔开
题解:
先求出a、b的和为sum,特判sum为0时的输出,然后判断正负、对sum的绝对值求其位数以及把每一位都存到数组c中,然后按要求在合适的位置输出逗号。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,b;
scanf("%d%d",&a,&b);
int sum=a+b;
if(sum==0) printf("0");
int num=0;
int c[10];
int temp=abs(sum);
while(temp){
c[num]=temp%10;
temp/=10;
num++;
}
if(sum<0){
printf("-");
}
for(int i=num-1;i>=0;i--){
printf("%d",c[i]);
if(i%3==0&&i!=0) printf(",");
}
printf("\n");
return 0;
}
1002 A+B for Polynomials (25分)
题目链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each 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 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and 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
题意:
给出用多项式表示的两个数,每个数各占一行:k代表项数,然后后面会跟k对数,每一对(Ni代表指数,aNi代表系数);
计算a、b的和,按要求输出。
题解:
只需要将a、b对应系数不为零的项进行运算,然后按要求输出即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
double a[1005];
double b[1005];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int ka,kb;
scanf("%d",&ka);
int n;
double kn;
for(int i=0;i<ka;i++){
scanf("%d%lf",&n,&kn);
a[n]=kn;
}
scanf("%d",&kb);
for(int i=0;i<kb;i++){
scanf("%d%lf",&n,&kn);
b[n]=kn;
}
int num=0;
int cnt=0;
for(int i=0;i<=1000;i++){
a[i]+=b[i];
if(a[i]!=0){
num++;
cnt=i;
}
}
printf("%d",num);
for(int i=1000;i>=0;i--){
if(a[i]!=0){
printf(" %d %.1f",i,a[i]);
}
}
printf("\n");
return 0;
}