从今天起每天刷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&ltstdio.h&gt
#include&ltstdlib.h&gt
#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&ltstdio.h&gt
#include&ltstdlib.h&gt
#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&ltstdio.h&gt
#include&ltstdlib.h&gt
#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

参考博客

【PAT甲级】1002 A+B for Polynomials (25 分)

PTA A1001&A1002的更多相关文章

  1. Python数据分析--Pandas知识点(一)

    本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘 1. 重复值的处理 利用drop_duplicates()函数删除数据表中重复多余的记录, 比如删除重复多余的ID. im ...

  2. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  3. 浙大PTA - - File Transfer

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21732 #include "iostream" #includ ...

  4. ERROR<53761> - 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-Direc ...

  5. PTA中提交Java程序的一些套路

    201708新版改版说明 PTA与2017年8月已升级成新版,域名改为https://pintia.cn/,官方建议使用Firefox与Chrome浏览器. 旧版 PTA 用户首次在新版系统登录时,请 ...

  6. PTA分享码-Java

    主要用于Java语法练习,非竞赛类题目.   1. Java入门          959dbf0b7729daa61d379ec95fb8ddb0   2. Java基本语法   23bd8870e ...

  7. C语言第一次实验报告————PTA实验1.2.3内容

    一.PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度100°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...

  8. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  9. 第十四,十五周PTA作业

    1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...

随机推荐

  1. 如何在onCreate中获取View的高度和宽度

    如何在onCreate中获取View的高度和宽度 原文链接:http://mp.weixin.qq.com/s?__biz=MzAwODE1NTI2MQ==&mid=2247483676&am ...

  2. 【雕爷学编程】Arduino动手做(16)---数字触摸传感器

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  3. jar在linux上运行

    打jar后一直在linux远程的运行: nohup java -jar xxx.jar & CRT(打开时运行):(另外上传文件可使用“rz”命令,上传jar包) java -jar xxx. ...

  4. vue-小爱ADMIN系列文章(二):微信微博等分享,国际化,前端性能优化,nginx服务器部署

    最近在做我的小爱ADMIN后台管理系统,结合当前市场后台管理系统对相关功能的需求,我又开始新增了一些新的功能和组件,如分享功能组件,项目国际化功能:项目完成后,部署在nginx服务器,发现首次访问的速 ...

  5. Mac os 下 python爬虫相关的库和软件的安装

      由于最近正在放暑假,所以就自己开始学习python中有关爬虫的技术,因为发现其中需要安装许多库与软件所以就在这里记录一下以避免大家在安装时遇到一些不必要的坑. 一. 相关软件的安装:   1. h ...

  6. C#开发BIMFACE系列7 服务端API之获取文件信息列表

    系列目录     [已更新最新开发文章,点击查看详细] 本文详细介绍如何获取BIMFACE平台中所有上传过的文件信息列表. 请求地址:GET https://file.bimface.com/file ...

  7. Java多线程的中断原理和 interrupt() 几个方法简介

    上节讲到,线程被 wait() 通知后进入等待池,可以由本线程的 interrupt() 方法解救,使本线程可以去重新竞争锁等等.是如何实现的呢? 实际上,中断仅仅是在线程对象做一个标记而已,称为中断 ...

  8. k近邻法的实现

    k近邻法 模型 使用的模型实际上对应于特征空间的划分.模型的三个基本要素:1.距离度量 2. k值的选择 3. 分类决策规则决定. k值的选择:k值的选择,k如果选择的过小会导致过拟合,模型会变得复杂 ...

  9. JavaScript img标签自带的onload和onerror函数

    onload 加载完成时调用触发 原生: <img src="" alt="Park" onload=“handleImageLoaded()”/> ...

  10. git@github.com: Permission denied (publickey)

    1.  检查SSH key是否已经存在,如存在走第3步 :      ls ~/.ssh/ 2.  如果第1步中的SSH key不存在,生成一个新的SSH key:      ssh-keygen - ...