`

1001. A+B Format (20)

PAT 
阅读更多
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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

//正负数情况看能否分类处理,想好算法后再处理写代码
#include <stdio.h>

int main()
{
	int a,b,sum;
	while(scanf("%d %d",&a,&b) != EOF){
		sum = a+b;
		if(sum < 0){
			sum = -sum;	
			printf("-");	//转化成正数处理 
		}
		if(sum >= 1000000){
			printf("%d,%03d,%03d",sum/1000000,(sum%1000000)/1000,sum%1000);
		}
		else if(sum >= 1000){
			printf("%d,%03d",sum/1000,sum%1000);
		}
		else{
			printf("%d",sum);
		}
	}
	return 0;
}


主要是在数据输出格式的处理~水过
0
0
分享到:
评论

相关推荐

    pta_1001 A+B Format (20 分).docx

    pta_1001 A+B Format (20 分).docx

    【JAVA】1001 A+B Format (20分) PAT甲级 PAT (Advanced Level) Practice

    目录1001 A+B Format (20分)思路一:不按他们的位数分情况处理代码一:用可变数组ArrayList+字符串截取方法subString代码二:栈Stack+字符串截取方法subString思路二:因为a和b的范围较小,所以可以按他们的位数分...

    计算机应用基础-作业题.doc

    A.COPY B.DIR C.FORMAT D.DELTREE 2. Windows XP中的软盘格式化操作可以通过使用【 】来实现。 A.【格式】菜单 B.软盘操作快捷菜单 C.【工具】菜单 D.【控制面板】 3.关于硬件驱动程序的说法正确的是【 ...

    PAT(甲级) A+B Format

    题目 题目链接 思路 把数字转化为字符串,倒序输出 大神的都是数字直接转为字符串,学到了 代码 第一个测试点不过,有会的可以帮我... a = a + b; if(a == 0) cout &lt;&lt; 0; if(a&lt;0) { a = -a; cout &lt; 0){

    基于c#CP3平面网严密平差数据处理

    //true表示A+B*S bool Ma = true;//true表示验后单位权中误差 double LVRatio = 2.0;//水平方向 和 天顶距 权的比值 string AdjMethord = "普通";//三维平差方法 const double Pe = 0.0174532925199433;//Math....

    B+树_b+tree_

    In this project you are supposed to implement a B+ tree of order 3 with the following ... After all the insertions print out the B+ tree in a top-down lever-order format as shown by the samples.

    V5Shop8.3官方商业源码带注释【非破解版】

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "V5Shop.Payment.ChinaBank", "V5Shop.Payment.ChinaBank\V5Shop.Payment.ChinaBank.csproj", "{9714BACE-1FFD-4A08-B342-C131CC5ACFF9}" EndProject Project...

    0018计算机应用基础复习资料.doc

    在下列的DOS命令中,属于内部命令的是( A) A.COPY B.FORMAT C.FDISK D.ATTRIB 18.在Windows中,要将当前窗口的内容放入剪贴板,应按(C ) A.PrintScreen(PrtScr)键 B.Ctrl+PrintScreen(PrtScr)键 C.Alt+PrintScreen...

    js的日历时间控件

    weekend:[0,6],timeFormat:"HH:mm"}},createLocale:function(a,b){return Object.extend(Object.clone(Control.DatePicker.i18n.baseLocales[a]),{language:b})}});Control.DatePicker.i18n.prototype={initialize:...

    根据一边动态变化画圆

    float twoAB = 2 * A_LENGTH * B_LENGTH ; float angle = std::acos(abc/twoAB); strText.Format(_T("当前角度为:%.3f度"),(angle/ 3.1415926 * 180.0f)); pAngleStatic-&gt;SetWindowText(strText); Gdiplus::...

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    WPTools.v6.29.1.Pro

    * stream RTFvariables were not loaded from WPT format. They are loaded now. 9.11.2012 - WPTools 6.28 - Update to RTF reader to load landscape flag for sections better - when page mirror was used, ...

    数值分析-曹德欣-matlab程序

    c=(a+b)/2; while c&gt;esp if fun(a)*fun(c) b=c; c=(a+b)/2; elseif fun(c)*fun(b) a=c; c=(a+b)/2; else y=c; esp=10000; end n=n+1; end y=c; elseif fun(a)==0 y=a; elseif fun(b)==0 y=b; else...

    Debugging with GDB --2007年

    2.1.3 Redirecting WDB input and output to a file . . . . . 2.2 Quitting GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Shell commands . . . . . . . . . . . ....

    C#简易计算器

    a = b = 0; flag = flag2 = 0; format = false; i = 1; } if (a != 0) textBox1.Text = ""; st = st + button1.Text; textBox1.Text = st; } private void button2_Click(object sender, EventArgs e)...

    scala-csv:用于 Scala 的 CSV ReaderWriter

    a,b,c d,e,f 您可以使用 CSVReader#open 创建 CSVReader 实例。 scala &gt; val reader = CSVReader .open( new File &#40; " sample.csv " &#41;) 读取所有行 scala &gt; val reader = CSVReader .open( new File &#40;...

    FDTD用户手册

    FDTD软件英文使用说明 Initial Properties dialog box ................................................................................... 33 Mask Export......................................................

    Python字符串的格式化

    print('a+b={}'.format(a+b)) 输出了什么,是不是a+b的结果。是不是很神奇。 现在我们来说说这三行代码。 首先a与b的赋值应该没有问题。输出的时候就有问题了。输出a+b={},这也没问题,如果只是这么写,输出的就是...

Global site tag (gtag.js) - Google Analytics