PTA甲级之 字符串处理

题目集合:

题目详情 - 1001 A+B Format (pintia.cn)

--------------

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

代码长度限制   16 KB

时间限制   400 ms

内存限制    64 MB

类型:字符串的处理

知识点:

1.to_string()方法,可将数字转换成字符。      扩展:stoi(str)  可将字符转成数字

2.(太久没接触了,最初开始做题甚至忘了int的范围,开始竟然怀疑是否可以声明为int类型)

int类型的范围数值是-2^(32-1) ~ 2^(32-1) -1 即-2147483648 ~ 2147483647   也就是说在10的10次方范围加减都没问题,

扩展:

char类型的变量存储值从-128到127         unsigned char类型的变量存储值从0到255
short类型的变量存储值从-32768到32767       unsigned short类型的变量存储值从0到65535
int类型的变量存储值从-2147483648到2147483647     unsigned int类型的变量存储值从0到4294967295
long类型的变量存储值从-2147483648到2147483647     unsigned long类型的变量存储值从0到4294967295

long long类型的变量存储值从-9223372036854775808到9223372036854775807
unsigned long long类型的变量存储值从0到18446744073709551615
最小的非零float类型变量的值的是1.175e-038
最大的float类型变量的值的是3.403e+038
最小的非零double类型变量的值的是2.225e-308
最大的double类型变量的值的是1.798e+308

最小的非零long double类型变量的值的是-0.000e+000
最大的long double类型变量的值的是-1.#QOe+000
float类型的变量提供6位精度的小数位数
double类型的变量提供15位精度的小数位数

long double类型的变量提供18位精度的小数位数

好~言归正传

本题思路:

题目数值范围为10的6次方,则可直接加减。
需要对输出结果做个处理,也就是从后往前,每三位的地方加个逗号
由此最好使用字符串进行处理。需要把数值转换成字符串,从后往前进行遍历,在3的倍数的地方加逗号。
要注意的是,若首位即使是3的倍数也不可再加逗号,所以需要进行判断,不为首位的条件就是下标不为0或者首位字符不为‘-’号
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    string s=to_string(a+b);
    string res="";
    int j=0;
    for(int i=s.size()-1;i>=0;i--){
        j++;
        res=s[i]+res;
        if(j%3==0&&i&&s[i-1]!='-'){
            res=","+res;
        }
    }
    cout<<res<<endl;
}

------------------------------

1005 Spell It Right

分数 20

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five
#include <iostream>
#include <string>
using namespace std;
string English[10]={"zero","one","two","three","four",
                      "five","six","seven","eight","nine"};
int main() {
    string str;
    cin>>str;
    int sum=0;
    for(int i=0;i<str.length();i++){
        sum+=str[i]-'0';
    }
    str=to_string(sum);
    int len =str.length();
    for(int i=0;i<len;i++){
        if(i<len-1) cout << English[str[i]-'0']<<" ";
        else cout<<English[str[i]-'0']<<endl;
    }
    return 0;
}

类型: 字符串处理

知识点:

起初用到了很笨的方法switch来一一对应,发现一直是编译错误。后来查到switch不能用于string类型,只能直接应用于int,所以最好还是用map数组或者简单的数组存储对应值吧

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值