• 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位精度的小数位数

    好~言归正传

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

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

    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
    1. #include
    2. #include
    3. using namespace std;
    4. string English[10]={"zero","one","two","three","four",
    5.                       "five","six","seven","eight","nine"};
    6. int main() {
    7.     string str;
    8.     cin>>str;
    9.     int sum=0;
    10.     for(int i=0;ilength();i++){
    11.         sum+=str[i]-'0';
    12.     }
    13.     str=to_string(sum);
    14.     int len =str.length();
    15.     for(int i=0;i
    16.         if(i-1) cout << English[str[i]-'0']<<" ";
    17.         else cout<'0']<
    18.     }
    19.     return 0;
    20. }

    类型: 字符串处理

    知识点:

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

     
    

  • 相关阅读:
    基于深度学习的车道检测(一)
    java 企业工程管理系统软件源码 自主研发 工程行业适用
    JavaScript中的Generator函数及其使用方式
    Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
    30天Python入门(第十天:深入了解Python中的循环)
    随机产生两个数在屏幕上打印,例如6*7=? 让学生输入答案,若正确打印答对了,否则提示学生重做,直到答对为止(小游戏)
    我梦想中的学习组织-勤学会
    unity_Vector3.up 和 transform.up 的区别
    【计算机毕业设计】70.毕业设计管理系统源码
    ExtJS-内置字体图标(Font)
  • 原文地址:https://blog.csdn.net/lijunyan5/article/details/127683096