- 浏览: 36854 次
- 性别:
- 来自: 杭州
最新评论
在一行中输入整数 a 和 b,用空格隔开( -1000000 <= a, b <= 1000000 )
输出a+b的值,用千分位格式输出
Sample Input
-1000000 9
Sample Output
-999,991
python版本代码如下
import re import sys def fun(num): if num >= 1000: fun(num/1000) sys.stdout.write(",%03d" % (num%1000)) else: sys.stdout.write(num) def test(): str = raw_input() s = re.split(" ",str) sum = int(s[0]) + int(s[1]) if sum < 0: sys.stdout.write("-") sum = sum * (-1) fun(sum) if __name__ == '__main__': test()
import re import sys str = raw_input() s = re.split(" ",str) sum = int(s[0]) + int(s[1]) if sum < 0: sys.stdout.write("-") sum = sum * (-1) str = repr(sum) mod = len(str)%3 num = len(str)/3 if mod > 0: if num == 0: sys.stdout.write(str[0:mod]) sys.exit() else: sys.stdout.write(str[0:mod]) sys.stdout.write(",") for i in range(num -1): sys.stdout.write(str[mod + 3*i: mod + 3*i + 3]) sys.stdout.write(",") sys.stdout.write(str[mod + 3*(num-1): mod + 3*num])
C++版本代码如下
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int a, b; cin>>a>>b; int sum = a + b; if (sum < 0) { cout<<"-"; } sum = abs(sum); stringstream strStream; strStream<<sum; string s = strStream.str(); int mod = s.length()%3; int num = s.length()/3; if (mod > 0) { if (num == 0) { cout<<s.substr(0,mod); return 0; } else { cout<<s.substr(0,mod)<<","; } } for(int i = 0; i < num - 1; i++) { cout<<s.substr(mod + 3*i, 3)<<","; } cout<<s.substr(mod + 3*(num-1),3); return 0; }
发表评论
-
PAT1013 Battle Over Cities
2012-11-29 23:59 785Sample Input 3 2 3 1 2 1 3 ... -
PAT1003 Emergency
2012-11-29 23:46 669Sample Input 5 6 0 2 1 2 1 ... -
PAT1041 Be Unique
2012-11-23 23:43 769找出只出现过一次的数,用各种排序必然超时,需要用数组做hash ... -
PAT1042 Shuffling Machine
2012-11-23 23:42 738扑克洗牌 #include < ... -
PAT1040 Longest Symmetric String
2012-11-23 23:41 965求最长回文子串 #include < ... -
PAT1036 Boys vs Girls
2012-11-23 23:41 720Sample Input 1: 3 Joe M Mat ... -
PAT1035 Password
2012-11-23 23:40 622Sample Input 1: 3 Team0000 ... -
PAT1031 Hello World for U
2012-11-22 23:54 660Sample Input: helloworld! S ... -
PAT1029 Median
2012-11-22 23:54 660用标准库的排序全部超时,需要自己实现,另外还不能用cin co ... -
PAT1028 List Sorting
2012-11-22 23:53 815用vector最后一个用例超时了。。。 Sample ... -
PAT1027 Colors in Mars
2012-11-22 23:52 633Sample Input 15 43 71 Samp ... -
PAT1025 PAT Ranking
2012-11-22 23:51 776Sample Input: 2 5 123456789 ... -
PAT1023 Have Fun with Numbers
2012-11-21 23:55 692大数的相加 比较两个字符串中字符完全相同 Sa ... -
PAT1020 Tree Traversals
2012-11-21 23:54 660已知中序遍历 后序遍历,求层次遍历 Sample In ... -
PAT1019 General Palindromic Number
2012-11-21 23:53 541十进制转任意进制,并比较是否是回文数 Sample I ... -
PAT1037 Magic Coupon
2012-11-21 15:46 667Sample Input: 4 1 2 4 -1 ... -
PAT1038 Recover the Smallest Number
2012-11-20 23:52 1675由一道面试题改的 把数组排成最小的数 不同之处是这 ... -
PAT1024 Palindromic Number
2012-11-20 23:51 654Sample Input 1: 67 3 Sampl ... -
PAT1015 Reversible Primes
2012-11-19 23:51 776十进制转任意进制 假设十进制数为number,转 ... -
PAT1012 The Best Rank
2012-11-19 23:50 934四门功课,输出排名最高的是哪个 Sample Inpu ...
相关推荐
目录1001 A+B Format (20分)思路一:不按他们的位数分情况处理代码一:用可变数组ArrayList+字符串截取方法subString代码二:栈Stack+字符串截取方法subString思路二:因为a和b的范围较小,所以可以按他们的位数分...
题目 题目链接 思路 把数字转化为字符串,倒序输出 大神的都是数字直接转为字符串,学到了 代码 第一个测试点不过,有会的可以帮我... a = a + b; if(a == 0) cout << 0; if(a<0) { a = -a; cout < 0){
我的PAT乙级练习题1001代码记录,题目地址:https://www.patest.cn/contests/pat-b-practise/1001
PAT乙级1022源代码 codeblocks 输入两个非负10进制整数A和B(),输出A+B的D (1 )进制数。
PTA跳转:原题链接 这道题相对比较简单,题目大意是输入两行数据,每一行的第一个数据表示这个多项式有多少对数据(包括指数和系数),每行数据代表一个多项式(如”2 1 2.4 0 3.2″表示”2.4x+3.2“,“2 2 1.5 1 ...
题意概述给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集。数据规模A 和 B 均为长度不超过${10}^6$的、由可见 ASCII 字符和
黑群晖最经典的版本 DSM_DS918+_24922.pat
#include ... cin>>a>>b; int c; c = a + b; int temp = abs(c); std::string s; std::stringstream out; out ; s = out.str(); int l; l = s.size(); int m = 1; int k = l-1; int num[9];
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 ...
DSM_DS220+_42962.pat
DSM
NAS群辉安装-DS918+_引导文件.img-DS918+_23824.pat
Pat试题答案,题号从1001到1049,有需要的同学可以用来参考
PAT(编程能力测试)发展环境编辑器: 一键( F5 )编译并运行C / C ++,Python,Java键绑定: [ { " key " : " f5 " , " command " : " workbench.action.tasks.runTask " , " args " : " C/C++ " }, { " key " : ...
DSM_DS3622xs+_42951.pat
PAT PAT《C/C++/Java/Pascal 程序设计基础》习题集 ,Java Version。
vs2010写的代码 语言c++ 1001 害死人不偿命的(3n+1)猜想 使用if语句和while语句
DSM_DS918+_25426.pat,和synoboot.img是一个群晖的引导的镜像教程及来源https://post.smzdm.com/p/aqx07xmk/
现有解集:弦乐A1001 A + B格式A1002多项式的A + B:多项式生成A1005正确拼写:串联各位和A1009多项式的乘积:多重式乘积A1015可逆素数:判断逆置素数A1025 PAT排名:PAT排名A1050字符串减法:删除去串中给定字符...