题目:

我一开始的思路是:

  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. 远程连接mysql出现1130的错误

    数据库权限不足 连接数据以后执行以下命令 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '您的数据库密码' WITH GRANT OP ...

  2. Spring基础04——ApplicationContext

    1.ApplicationContext简述 ApplicationContext代表IOC容器,在SpringIOC容器中读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化后 ...

  3. 神经风格转换 (Neural-Style-Transfer-Papers)

    原文:https://github.com/ycjing/Neural-Style-Transfer-Papers Neural-Style-Transfer-Papers Selected pape ...

  4. 计蒜客 蓝桥模拟 I. 天上的星星

    计算二维前缀和,节省时间.容斥定理. 代码: #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  5. c语言之ascii字符

    int main(){ char buf[20] = {104,101,108,108,111,0}; printf("buf:%s\n",buf); return 0;} 打印结 ...

  6. SpringBoot2 Filter执行两次问题解决

    原因:在请求指定url之外还请求了/favicon.ico 地址 过滤掉问题就解决了 @Overridepublic void doFilter(ServletRequest request, Ser ...

  7. [ByteCTF 2019]EZCMS

    题目复现链接:https://buuoj.cn/challenges 参考链接:ByteCTF_2019&XNUCA_2019部分web题复现 一.知识点 1.源码泄露 访问www.zip获取 ...

  8. linux下实现web数据同步的四种方式(性能比较)

    实现web数据同步的四种方式 ======================================= 1.nfs实现web数据共享2.rsync +inotify实现web数据同步3.rsyn ...

  9. 【leetcode】1154. Day of the Year

    题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...

  10. ckeditor直接粘贴图片实现

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...