PTA200题总结

PTA200题总结

1. A+B Format

题目描述

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

结题思路

思路1:字符串处理题

  1. 输入两个数。
  2. 计算两数之和。
  3. 用to_string()将两数之和转换成string类型。
  4. 判断string[0]是否为’-’,若为’-’,则输出它,并将遍历变量i=1。
  5. 从i遍历此字符串。每3个一循环,在第len%3处时输出’,’,且不能输出最后一个逗号。

源代码

#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
int main()
{
    
    
	int a, b, sum;
	cin >> a >> b;
	sum = a + b;
	string str = to_string(sum);
	int i  = 0;
	if(str[i] == '-')
	{
    
    
		cout << '-';
		i = 1;	
	}
	int len = str.length();
	for(; i < len; i++)
	{
    
    
		cout << str[i];
		if((i+1) % 3 == len % 3 && i!= len-1)
		{
    
    
			cout << ',';
		}
	}
	return 0;
} 

此类题型分析

字符串处理题,要注意

  1. 正负号的判别。
  2. 遍历条件的处理,如输出都要需要每3个一循环,在第len%3处时输出’,’,并且不能输出最后一个逗号。

我的观点

  1. 常规题,以后多做。
    ####获取字符数组,字符串的长度的函数总结:
  2. sizeof()一般用来计算变量占据存储空间大小。用来算字符数组长度时,包括计算最后的’\0’。
  3. strlen()计算到’\0’的长度,但不包括’\0’。
  4. length()计算字符串s的长度,不包括’\0’。

2.A+B for Polynomials

题目描述

传送门:
https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000

结题思路

使用索引法,将值同时存储在下标和value中。

第一个数的存储:
幂:{0, 1, 0, …, }
系数:{2.4, 3.2, 0, …}
第二个数的存储:
幂:{0, 1, 2, 0, …}
系数:{0, 0.5, 1.5, 0, …}
思路1:

  1. 使用索引法,将值同时存储在下标和value中。
  2. 从0-1001依次遍历num1,num2的幂和系数的数组,将相同幂的值相关起来。
  3. 从后到前输出sum数组的值,并用setprecition设置精度。

源代码

#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;
int num1mi[1001] = {
    
    0};
double num1xishu[1001] = {
    
    0};
int num2mi[1001] = {
    
    0};
double num2xishu[1001] = {
    
    0};
int main()
{
    
    
	int n1, n2;
	int sum_k = 0;
	int sum_mi[1001] = {
    
    0};
	double sum_xishu[1001] = {
    
    0};
	cin >> n1;
	for(int i = 0; i < n1; i++)
	{
    
    
		int index;
		cin >> index ;
		num1mi[index] = index;
		cin >> num1xishu[index];
	}
	cin >> n2;
	for(int i = 0; i < n2; i++)
	{
    
    
		int index;
		cin >> index ;
		num2mi[index] = index;
		cin >> num2xishu[index];
	}
	for(int i = 0; i < 1001; i++)
	{
    
    
		sum_xishu[i] = num1xishu[i] + num2xishu[i]; // computer sum
		if(sum_xishu[i])
		{
    
    
			sum_k++;
		}
	}
	cout << sum_k << " ";
	for(int i = 1001; i >= 1; i--)
	{
    
    
		if(abs(sum_xishu[i]) > 0.000000001)
		{
    
    
			cout << i << " " << setiosflags(ios::fixed) << setprecision(1) << sum_xishu[i] << " ";
		}
	}
	if(sum_xishu[0])
	{
    
    
		cout << 0 << " " << setiosflags(ios::fixed) << setprecision(1) << sum_xishu[0];
	}
	return 0;
} 

题型分析

  1. 数组题,记得利用[]画图分析的方法解决。

下次再此类题中要注意的地方

  1. 数组类型题,注意数据类型、输入输出的精度。
  2. <iomanip> 和setprecition()设置c++输出的精度。
  3. c++保留精度问题
    1)保留5位有效数字
    cout << “2)” << setprecision(5) << x << " " << y << " " << endl;
    2)保留小数点后面5位
    cout << “3)” << fixed << setprecision(5) << x << " " << y << endl;

此类题模板代码

  1. 将值同时存在索引和value中。
for(int i = 0; i < n1; i++)
	{
    
    
		int index;
		cin >> index ;
		num1mi[index] = index;
		cin >> num1xishu[index];
	}

3.

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方

此类题模板代码

猜你喜欢

转载自blog.csdn.net/qq_40092110/article/details/105218776