PAT甲级练习题1001、1002
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 −. 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
解题时注意取余时i的位置
满分代码:
import java.util.Scanner; public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
int res = a+b;
String A = String.valueOf(res);
if(res<) {
int len = A.length();
int c = (len-)%;
System.out.print('-');
for(int i=;i<=c;i++) {
System.out.print(A.charAt(i));
}
if(c!=&&len>) System.out.print(','); for(int i=c+;i<A.length();i++) {
if((i-c-)!=&&(i-c-)%==&&i!=A.length()-) System.out.print(',');
System.out.print(A.charAt(i));
}
System.out.println();
}else {
int len = A.length();
int c = len%;
for(int i=;i<c;i++) {
System.out.print(A.charAt(i));
}
if(c!=&&len>) System.out.print(','); for(int i=c;i<A.length();i++) {
if((i-c)!=&&(i-c)%==&&i!=A.length()-) System.out.print(',');
System.out.print(A.charAt(i));
}
System.out.println();
}
}
in.close();
}
}
1002 A+B for Polynomials (25 分)
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 (,) are the exponents and coefficients, respectively. It is given that 1,0.
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
注意为0项需要溢出map表
满分代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int keyMax = ;
int up = in.nextInt();
Map<Integer, Double> hm = new HashMap<>();
for (int i = ; i < up; i++) {
int x = in.nextInt();
if (x > keyMax)
keyMax = x;
hm.put(x, in.nextDouble());
}
int down = in.nextInt();
for (int i = ; i < down; i++) {
int x = in.nextInt();
if (x > keyMax)
keyMax = x; if (!hm.containsKey(x)) {
hm.put(x, in.nextDouble());
} else {
double y = hm.get(x);
y += in.nextDouble();
if(y!=)
hm.put(x, y);
else hm.remove(x);
}
}
if(hm.size()==) System.out.print("");
else
System.out.print(hm.size() + " ");
int sign = ;
for (int i = keyMax; i >= ; i--) {
if (hm.containsKey(i)) {
sign++; System.out.print(i + " ");
if (sign != hm.size())
System.out.print(String.format("%.1f", hm.get(i)) + " ");
else
System.out.print(String.format("%.1f", hm.get(i)));
}
}
}
}
}
PAT甲级练习题1001、1002的更多相关文章
- PAT 乙级练习题1001 害死人不偿命的(3n+1)猜想 (15)
1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...
- pat甲级题目1001 A+B Format详解
pat1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级代码仓库
大道至简,知易行难.希望能够坚持刷题. PAT甲级真题题库,附上我的代码. Label Title Score Code Level 1001 A+B Format 20 1001 * 1002 A+ ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
随机推荐
- Python模块(三)(正则,re,模块与包)
1. 正则表达式 匹配字符串 元字符 . 除了换行 \w 数字, 字母, 下划线 \d 数字 \s 空白符 \n 换行符 \t 制表符 \b 单词的边界 \W \D \S 非xxx [ ...
- (转)iOS获取设备型号
//获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...
- 奇数结点升序偶数结点降序的单链表排序(Python实现)
题目 一个链表,奇数结点升序,偶数结点降序,要求变成一个全升序的链表. 例如:1->8->2->7->3->6->4->5,变为1->2->3-& ...
- Post页面爬取失败__编码问题
python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...
- Selenium2启动浏览器且加载插件
一.SELENIUM2启动浏览器 注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理 ...
- c++ dll 创建
建立一个C++的Win32DLL,这里要注意选择"Export symbols"导出符号.点击完成. 如下图所示: 由于项目的名称是"TestCPPDLL" ...
- vi 编辑器命令
插入命令 a append after the cursor A append after the current line i insert before the cursor I insert b ...
- mantisbt邮件配置
PHP.INI里面 [mail function]; For Win32 only.#SMTP = 192.168.0.249SMTP = smtp.163.comsmtp_port = 25 ; F ...
- API经济时代的思考(转载目的:为之后写API-first模式的生命周期治理做准备)
原文地址:API经济时代的思考 感觉这篇博客还不错,个人赞同其大部分的内容,借鉴参考一下,懒得自己写了(关键是不一定能轻松写得更好,嘿嘿,偷懒啦) 接下来会写关于API经济的概念下,如何进行AP ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...