PAT (Advanced Level) Practice---甲级刷题---题解(1001~1115)

目录

备用知识点梳理:

具体题目(1001~1115)如下:

1001 A+B Format (20分)

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 −10​6​​≤a,b≤10​6​​. 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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤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;
}

 

 


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值