PAT Grade A-String Processing Type-1001 A+B Format Problem Solving Ideas

1001 A+B Format (20 points)

Insert picture description here

Ideas

When processing, you need to prepare the character array in advance, and then output one by one
. Be careful when using ++ in the subscript of the array. The symbol can be replaced with a number in advance, and the output will be updated later.

The second idea is to store the number of each digit when counting the length of the number. When outputting, when the remainder is 0 after dividing by 3, a comma is output. This idea is more concise.

We have to find a way to complete the following key operations during input or processing, so that the code is more concise.

Code

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int m,n;
    scanf("%d%d",&m,&n);
    int ans;
    ans = m+ n;

    if(ans<0)
        printf("%c",'-');

    int tmp =1,i;

    ans = abs(ans);
    for (i =0;i<8;i++)
    {
    
    
        tmp*=10;
        if(tmp > ans)
            break;
    }
    
    int len = i+1,num =0;
    int abc[12] = {
    
    0};
    
    
    for(int i =0 ;i<len;i++)
    {
    
    
        if (num%4==0)
        {
    
       
            abc[num++] = 888;
            i--;
            continue;
        }
        abc[num++] = ans%10;
        ans /=10;  
    }

    for(int i = num-1;i>0;i--)
    {
    
    
        if (abc[i]==888)
            cout<<",";
        else cout<<abc[i];
    }    
}

Guess you like

Origin blog.csdn.net/weixin_43999137/article/details/114087857
Recommended