题目:

我一开始的思路是:

  1. 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题);
  2. pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / pow(10, len - 3)来获得千位以前的数字),从左往右依次输出,同时在对应位置输出,

也就是说,我这里的思路是直接用数字来进行处理的,但是这样做其实非常低效而且很容易写错代码,因此我看了下柳婼的代码,换成了这个思路:先将计算结果转换为字符串后进行处理。这样就会简单许多:

#include <iostream>

int main(){
int a, b;
std::cin >> a >> b;
std::string stringNum = std::to_string(a + b);
int len = stringNum.length();
for(int i=0; i<len; i++){
std::cout << stringNum[i];
if(stringNum[i] == '-'){
continue;
};
if((i + 1)%3 == len%3 && i != len-1){
std::cout << ",";
}
}
return 0;
}

我一开始的时候搞不明白为什么判定条件有(i + 1)%3 == len%3,后来想了想,整理成了以下思路:

首先,我们假设计算结果为7位,那么可以知道,len = 7,且len%3结果为1。画成图:

因为我们的下标是从左往右进行计数的,而且从0开始计数,所以需要先通过i+1来变成和计算长度时一样的从1开始计数。

然后,我们先想想看应该怎么输出,如果是从右往左输出的话,那么我们先输出3位,然后输出,,然后再输出3位,再输出一次,,最后因为剩下的字符只有一个,所以输出了这个字符之后就不需要再输出逗号了。

但是计算机输出字符只能够从左往右输出,所以我们先计算出输出完所有的“逗号”之后,最后剩下的字符的数目,所以需要len%3。我们接下来要做的事情就是先输出这几个最后会剩下的字符,然后输出逗号,然后继续往后走3位,每次走完3位就输出一次逗号,直到最终遍历完整个字符串。

因此我们可以明白,其实设置(i + 1)%3 == len%3的目的就是:

  1. 让程序先输出必然会剩下的几个字符
  2. 然后每隔3位让程序输出一次逗号

所以,其实(i + 1)%3 == len%3就是相当于偏置项,让程序能够通过“偏置”来先输出最头几个肯定剩下来的字符。但是这样比较不容易理解,所以我们可以改写成这样:(i + 1)%3 - (len%3)== 0,因为(i+1) % 3必然取0, 1, 2这3个值中的一个,所以如果减去len%3,那么可以确保前len%3个字符输出期间不会输出逗号,且不会干扰到后面的计数。

为了方便理解,在修改代码之后可以变成:

#include <iostream>

int main(){
int a, b;
std::cin >> a >> b;
std::string stringNum = std::to_string(a + b);
int len = stringNum.length();
for(int i=0; i<len; i++){
std::cout << stringNum[i];
if(stringNum[i] == '-'){
continue;
};
if((i + 1)%3 - len%3 == 0 && i != len-1){
std::cout << ",";
}
}
return 0;
}

而且提交之后可以看到,是可以通过的:

PAT 1001 A+B Format (20 point(s))的更多相关文章

  1. PAT 1001 A+B Format (20分) to_string()

    题目 Calculate a+b and output the sum in standard format -- that is, the digits must be separated into ...

  2. PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

  3. PAT 甲级 1001 A+B Format (20)(20 分)

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

  4. PAT 甲级1001 A+B Format (20)(C++ -思路)

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

  5. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

  6. 【PAT】1001. A+B Format (20)

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

  7. 1001.A+B Format (20)代码自查(补足版)

    1001.A+B Format (20)代码自查(补足版) 谢谢畅畅酱的提醒,发现了代码中的不足,把变量名更改成更合理的名字,并且把注释也换成英文啦! 栋哥提供的代码自查的方式也帮助了我发现很多代码中 ...

  8. PAT 1001. A+B Format 解题

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

  9. 关于‘1001.A+B Format (20)’的解题报告

    1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...

随机推荐

  1. Solr7.2.1环境搭建和配置ik中文分词器

    solr7.2.1环境搭建和配置ik中文分词器 安装环境:Jdk 1.8. windows 10 安装包准备: solr 各种版本集合下载:http://archive.apache.org/dist ...

  2. 简单Spring Cloud 微服务框架搭建

    微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...

  3. 自定义str_repr_format

    class Foo: def __init__(self,name,age): self.name = name self.age = age def __str__(self): #自定义类的实例对 ...

  4. 个人作业 项目alpha版本测试

    课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求:https://edu.cnblogs.com/campus ...

  5. C#索引器3 重载

    7.索引器  重载 public class Demo { private Hashtable name = new Hashtable(); public string this[int index ...

  6. Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)

    A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...

  7. 用递归将嵌套的JSON对象遍历出来,转为二维数组 或一维数组

    var map = new Array();//二维数组 var map2 = new Array();//一维数组 for (var i = 0; i < e.Data.length; i++ ...

  8. ipsec概念理解

    互联网安全协议(英语:Internet Protocol Security,缩写:IPsec): 本质上一个协议包,透过对IP协议的分组进行加密和认证来保护IP协议的网络传输协议族(一些相互关联的协议 ...

  9. Arduino-原理图标识

    VCC    电源正极    VDD GNG    电源负极     VSS vin        表示输入电源 TXD RXD 是主板串口通信用的接口,TXD表示发送数据,RXD表示接收数据,还有简 ...

  10. 多个excel文件内容合并到一个excel文件的多个sheet的小程序

    # -*- coding:utf-8 -*- import xlrd, xlsxwriter # 待合并excelallxls = ["D:\\excelcs\\***.xlsx" ...