pat1001 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

题目分析:题目要求计算a+b的和并从右往左每三位加一个","正序输出,大致思路为将计算的数转换为字符后,然后一位一位输出,同时判断何时该加“,”。
难点在于这个地方,存在以下两种情况:1、若整个a+b和的位数是3的倍数,则重点考虑如何判断最后一位之后不加“,”如“324,113”不能为“324,113,”。
2、若整个a+b和的位数不是3的倍数,则重点考虑如何实现从右往左每三位加一个“,”。
关键代码:
 (i+)%==strlen(s)%&&i!=strlen(s)-  //s为数字转换后的字符数组
完整代码:
 #include<stdio.h>
#include<string.h>
int main()
{
int a,b,i;
char s[];
scanf("%d %d",&a,&b);
sprintf(s, "%d", a+b); //将数字转换为字符数组
for(i=;i<strlen(s);i++){
printf("%c",s[i]);
if(s[i]=='-') continue; //如果为"-"号继续循环不执行以下部分
if((i+)%==strlen(s)%&&i!=strlen(s)-) //要保证上述所说的情况都要满足
printf(",");
}
}

 

pat甲级题目1001 A+B Format详解的更多相关文章

  1. 【PAT甲级】1001 A+B Format (20 分)

    题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6​​≤a,b≤1e6​​) AAAAAccepted code: #include<bits/stdc++.h> us ...

  2. 【转】Java魔法堂:String.format详解

    Java魔法堂:String.format详解     目录     一.前言    二.重载方法     三.占位符     四.对字符.字符串进行格式化     五.对整数进行格式化     六. ...

  3. 【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解

    原文网址:http://www.cnblogs.com/622698abc/p/3348692.html declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs. ...

  4. PAT甲级题目1-10(C++)

    1001 A+B Format(20分) Calculate a+b and output the sum in standard format -- that is, the digits must ...

  5. PAT甲级练习题1001、1002

    1001 A+B Format (20 分)   Calculate a+b and output the sum in standard format -- that is, the digits ...

  6. 自定义控件的自定义的属性attrs.xml下的declare-styleable中format详解

    最近在摸索自定义控件,查找到一些自定义属性的一些资料,解决转载记载下来:看了此详解才方便理解! 我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中 ...

  7. VC++ CTime Format 详解

    参考链接: VC++中CTime类Format参数详解 CTime/COleDateTime::Format方法的使用 http://stat.ethz.ch/R-manual/R-devel/lib ...

  8. 二分算法题目训练(一)——Shell Pyramid详解

    HDU2446——Shell Pyramid 详解 Shell Pyramid 题目描述(Google 翻译的) 在17世纪,由于雷鸣般的喧嚣,浓烟和炽热的火焰,海上的战斗与现代战争一样.但那时,大炮 ...

  9. 二分算法题目训练(四)——Robin Hood详解

    codeforces672D——Robin Hood详解 Robin Hood 问题描述(google翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...

随机推荐

  1. Yahoo34条军规——雅虎WEB前端网站优化

    雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...

  2. 斯坦福【概率与统计】课程笔记(六):EDA | 标准差和方差

    这一篇比较简单,就不展开记录了,方差和标准差的计算方法记住了就可以. 计算mean 计算每个样本与mean的差值的平方,将其累加后除以(样本数-1)[注:这里的除数可以是n-1也可以是n],即得到方差 ...

  3. BootStrap自定义轮播图播放速度

    $('.carousel').carousel({      interval: 3000 });

  4. PAT甲级——A1155 HeapPaths【30】

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  5. .eslintrc.js相关配置

    module.exports = { root: true, //此项是用来指定javaScript语言类型和风格,sourceType用来指定js导入的方式,默认是script,此处设置为modul ...

  6. mysql 查看数据库最大连接数

    show variables like '%max_connections%'; navicat 切换到命令行: navicat查看建表语句: 选中表,右键,对象信息,选择DDL

  7. vue动态组件 互相之间传输数据 和指令的定义

    地址:https://blog.csdn.net/zhanghuanhuan1/article/details/77882595 地址:https://www.cnblogs.com/xiaohuoc ...

  8. C++中继承的protected访问级别

    1,子类是否可以直接访问父类的私有成员? 2,根据面向对象理论: 根据 C++ 语法: 3,继承中的访问级别编程实验: #include <iostream> #include <s ...

  9. Centos7 安装vscode

    1.官网下载vscode https://vscode.cdn.azure.cn/stable/0f3794b38477eea13fb47fbe15a42798e6129338/code-1.36.0 ...

  10. [Java 教程 00] 计算机基础

    前言 我想,来到这的朋友肯定是想学习JAVA或者想要进入IT这个行业的.考虑到大家的基础可能不一样,有些人可能还是用着新买的电脑,为了让大家在后续的学习中更加顺畅.在学习一门全新的计算机语言之前,我需 ...