pat甲级 1001 A+B Format

要写甲级题,首要任务是解决英文这个大难题。

困难词汇(我不认识的):calculate计算  standard format 标准格式  digits数字  separated 分离  commas逗号

这道题的大致意思是,给出两个数a和b,并且a和b都大于等于-10的6次方小于等于10的6次方,求出a和b的和,将这个和,每三个数字用逗号分隔一下。看懂了题意以后这题就简单了

ac代码如下:

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    int c=a+b;
    string e=to_string(c);//e是a和b的和
    string d;
    for(int i=e.length()-1,j=0;i>0;j++,i--){//将e的数给d,每三个加上一个,号
        d+=e[i];
        if((j+1)%3==0&&e[i-1]!=‘-‘){//如果e[0]是-的时候是不用给逗号的
            d+=‘,‘;
        }
    }
    d+=e[0];
    for(int i=d.length()-1;i>=0;i--){
        cout<<d[i];
    }
}

原文地址:https://www.cnblogs.com/fromzore/p/9949037.html

时间: 2024-11-08 06:26:52

pat甲级 1001 A+B Format的相关文章

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 digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case

PAT甲级 1001. 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 Each input file contains one test case. Each case contains a pair of i

PAT 甲级 1001 A+B Format

https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 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). Inpu

PAT甲级——1001 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) Sample Input: -1000000 9 Sample Output: -999,991 第一种方法,注意题目说明的数字范围,及时处理越界即可. 为啥捏,因为 in

【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 must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case cont

PAT甲级1001

[题目] 1001 A+B Format (20 point(s)) 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

PAT甲级 1001

1001 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.

PAT:1001. A+B Format (20)(数组存储,逢三加“,”) AC

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } int arr[30]; int i=0; do { arr[i]=sum%10; sum/=10; ++i; }while(sum!=0); for(int j=i-1 ; j>=0 ; --j) { pr

PAT:1001. A+B Format (20)(取整取余法) 部分正确

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum>1000000) //[思维],两个数字在:-1000000 <= a, b <= 1000000,之和不会超过1000000再加三个0的数量级 printf("%d,%d