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道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
随机推荐
- GoF23种设计模式之结构型模式之组合模式
一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...
- python中打印金字塔和九九乘法表的几种方法
# 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...
- F查询与Q查询
F查询 如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值. # 查询评论 ...
- Linux学习-磁盘配额 (Quota) 的应用与实作
什么是 Quota 在 Linux 系统中,由于是多人多任务的环境,所以会有多人共同使用一个硬盘空间的情况发生, 如 果其中有少数几个使用者大量的占掉了硬盘空间的话,那势必压缩其他使用者的使用权力! ...
- luogu2590 [ZJOI2008]树的统计
树剖裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, hea[30005 ...
- 轻量级的C++插件框架 - X3 C++ PluginFramework
X3 C++ PluginFramework 代号为X3的C++轻量级通用插件框架平台是一套通用的C++轻量级插件体系,没有使用MFC.ATL.COM.可在Windows和Linux下编译运行.应用程 ...
- [译]pandas中的iloc loc的区别?
loc 从特定的 gets rows (or columns) with particular labels from the index. iloc gets rows (or columns) a ...
- API生命周期第三阶段:API实施模式,以及结合swagger和项目现状的最佳模式
这篇博客,主要是宏观介绍一下开发模式,尤其是针对于目前公司前后分离的项目! 一.API实施模式概述 API实施模式,主要是三个,其中API-First又是作为一种指导思想的一种,所以,简单来说事实实施 ...
- 自定义AlertView(Swift)
MyAlertView.swift // Pop Up Styles enum MyAlertViewStyle: Int { case success case error case notice ...
- Spring Boot 必须先说说 Spring 框架!
现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下. Spring Boot ...