pat甲级题目1001 A+B Format详解
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详解的更多相关文章
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- 【转】Java魔法堂:String.format详解
Java魔法堂:String.format详解 目录 一.前言 二.重载方法 三.占位符 四.对字符.字符串进行格式化 五.对整数进行格式化 六. ...
- 【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解
原文网址:http://www.cnblogs.com/622698abc/p/3348692.html declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs. ...
- PAT甲级题目1-10(C++)
1001 A+B Format(20分) Calculate a+b and output the sum in standard format -- that is, the digits must ...
- PAT甲级练习题1001、1002
1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...
- 自定义控件的自定义的属性attrs.xml下的declare-styleable中format详解
最近在摸索自定义控件,查找到一些自定义属性的一些资料,解决转载记载下来:看了此详解才方便理解! 我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中 ...
- VC++ CTime Format 详解
参考链接: VC++中CTime类Format参数详解 CTime/COleDateTime::Format方法的使用 http://stat.ethz.ch/R-manual/R-devel/lib ...
- 二分算法题目训练(一)——Shell Pyramid详解
HDU2446——Shell Pyramid 详解 Shell Pyramid 题目描述(Google 翻译的) 在17世纪,由于雷鸣般的喧嚣,浓烟和炽热的火焰,海上的战斗与现代战争一样.但那时,大炮 ...
- 二分算法题目训练(四)——Robin Hood详解
codeforces672D——Robin Hood详解 Robin Hood 问题描述(google翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...
随机推荐
- Asp.Net Core 第02局:Program
总目录 前言 本文介绍Program,它包含程序的入口Main方法.从这里开始... 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:Program ...
- 3.tensorflow——NN
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data numClasses=10 inp ...
- VMware Workstation Pro 无法在Windows 上运行 检查可在Windows上运行的此应用的更新版
1.问题描述 2.排查原因 国庆节后微软推送了一个新的更新补丁,更新之后发现VMware无法打开(未更新前正常). 3.解决方案 3.1卸载微软更新补丁 Windows 10的安全更新难以被彻底规避, ...
- Flask-Scrip
介绍及安装 Flask-Script是一个让你的命令行支持自定义命令的工具,它为Flask程序添加一个命令行解释器.可以让我们的程序从命令行直接执行相应的程序. 安装 pip install Flas ...
- sciencedirect 网站抓取过程
开发环境 C#+SQLite 软件使用教程: 设置页面 1. 首先录入需要查询的关键词,如果需要根据年去查询,可以勾选对应的年,支持多个年份查询.点击[设置关键字]按钮,把待查询关键 ...
- Nginx 实现 Rewrite 跳转
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 上一篇文章对Nginx的Location配置进行了讲解,本篇主要对于Nginx中的Rewrite跳转进行讲解. ...
- java当中的Timer定时器的4种使用方式
import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask ...
- CSS中表示颜色的4种方法
#1:直接用颜色名称 #2:十六进制数 #3:RGB整数设置颜色 0-255 #4:RGB百分数设置颜色0%-100%
- DNS安装配置主从
准备环境 关闭防火墙 挂载一下 更改配置文件 安装dns服务 更改配置文件 先复制保存一份
- Harbor私有镜像仓库(下)
Harbor私有镜像仓库(下) 链接:https://pan.baidu.com/s/1MAb0dllUwmoOk7TeVCZOVQ 提取码:ldt5 复制这段内容后打开百度网盘手机App,操作更方便 ...