首页 > 其他分享 >1001 A+B Format

1001 A+B Format

时间:2022-11-12 14:55:05浏览次数:37  
标签:string Format int sum include 1001 cout

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 −106≤a,b≤106. 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

 

思路:

当输出多行数值时,一定要注意是否有多余或缺少空行的情况

将 int 转换为 string 可以直接使用 to_string(x)

使用string 代替char数组的好处是 可以使用string自带的函数简化代码,提高效率。

 

代码:

 

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main(){
    int a,b,sum,t=0,l;
    char s[100];
    while(scanf("%d%d",&a,&b) != -1){
        sum = a + b;
        sprintf(s, "%d", sum);
        if(s[0] == '-'){
            if(strlen(s) < 5){
                cout<<s;
            }else{
                cout<<s[0];
                l = strlen(s) - 1;
                for(int i=1; i<=l % 3; i++){
                    cout<<s[i];
                }
                if(l % 3 != 0) cout<<",";
                t = 0;
                for(int i=l % 3 + 1; i < strlen(s); i++){
                    cout<<s[i];
                    t++;
                    if(t == 3 && i != strlen(s) - 1){
                        cout<<",";
                        t = 0;
                    }
                }
            }
        }
        else{
           if(strlen(s) < 4){
               cout<<s;
           }else{
                l = strlen(s);
                for(int i=0; i<l % 3; i++){
                    cout<<s[i];
                }
                if(l % 3 != 0) cout<<",";
                t = 0;
                for(int i=l % 3; i < strlen(s); i++){
                    cout<<s[i];
                    t++;
                    if(t == 3 && i != strlen(s) - 1){
                        cout<<",";
                        t = 0;
                    }
                }
           }
        }
        cout<<endl;
    }
    return 0;
}

 

 

简化代码:

#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b; //输入a和b
    string s = to_string(a + b); //将a+b的值转换为字符串
    if(s[0] == '-') { //处理符号
        cout << '-';
        s.erase(0, 1);
    }
    int count = 0; //用于记录当前位置
    for(int i = s.length() - 1; i >= 0; i--){ //添加逗号
        count++;
        if(count % 3 == 0 && i > 0){
            s.insert(i, ",");
        }
    }
    cout << s;
}

 

标签:string,Format,int,sum,include,1001,cout
From: https://www.cnblogs.com/yccy/p/16883770.html

相关文章

  • MapReduce实战之小文件处理案例(自定义InputFormat)
    小文件处理案例(自定义InputFormat)1)需求无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案。将多个小文件合并成一......
  • js-Date扩展format()函数--处理时间字符串格式
    js-Date扩展format()函数--处理时间字符串格式constformatNumber=n=>{n=n.toString()returnn[1]?n:`0${n}`}处理月份和天的日期字符串,就是个位数前......
  • 611001 CAD 初始重置设置
    本节课讲解1CAD初始重置设置。1.双击打开CAD,打开CAD会出现三个界面,左侧【打开】可以打开文件。2.【新建】是新建空白文档,【最近使用的项目】中会有最近使用的文件,以名......
  • 701001 TXT 22G101-1图集的简介
    22G101-1图集的全称:混凝土结构施工图平面整体表示方法制图规则和构造详图(现浇混凝土框架、剪力墙、梁、板)。本图集制图规则适用于基础顶面以上各种现浇混凝土结构的柱......
  • String.Format(string, double)
    String.Format(string,double)string.Format("{0:F2}",750.5);//750.50string.Format("{0:F2}",750.22);//750.22string.Format("{0:F1}",750.12);//750.1......
  • python 中 format函数
     001、格式化数值>>>"{:.2%}".format(0.123456789)'12.35%'>>>"{:.5%}".format(0.123456789)'12.34568%'>>>"{:.2}".format(0.123456789)'0.12'>>>"{:.5}&quo......
  • SimpleDateFormat线程安全问题排查
    一.问题现象运营部门反馈使用小程序配置的拉新现金红包活动二维码,在扫码后跳转至404页面。二.原因排查首先,检查扫码后的跳转链接地址不是对应二维码的实际URL,根据代......
  • 常用类.SimpleDateFormat
    package常用类.calendar;importjava.text.SimpleDateFormat;importjava.util.Date;publicclasssimpleDateFormat{publicstaticvoidmain(String[]args)throw......
  • 论文笔记 - PRISM: A Rich Class of Parameterized Submodular Information Measures
    Motivation与ActiveLearning类似,TargetLearning致力于挑选外卖更“感兴趣”的数据,即人为为更重要的数据添加bias。例如我们当前的任务目标是增强自动驾驶算法的夜......
  • 论文笔记 - SIMILAR: Submodular Information Measures Based Active Learning In Rea
    motivationActiveLearning存在的重要问题:现实数据极度不平衡,有许多类别很少见(rare),又有很多类别是冗余的(redundancy),又有些数据是OOD的(out-of-distribution)。1.不同的......