Fork me on GitHub

PAT 1016

思路:用字符串类型string可以很方便的获取题目要求的数字,获得DA||DB的个数,然后通过循环转换成整型,相加后的到结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
int main()
{
    string  A =  "0" , B = "0";
    int DA = 0, DB = 0;
    cin >> A >> DA >> B >> DB;
    int count_A = 0, count_B = 0;
    int  side = 0;
    side = 11;
    if (A.length() >=side|| B.length() >=side)
    {
        return 0;
    }
    for (int i = 0; i < A.length(); i++)
    {
        //获取DA的个数
        if (A[i] == (DA+'0'))
        {
            count_A++;
        }
    }
    for (int i = 0; i < B.length(); i++)
    {
        //获取DB的个数
        if (B[i] == (DB + '0'))
        {
            count_B++;
        }
    }
    int sum_A = 0, sum_B = 0, sum = 0;
    while (count_A--)
    {    //转换成整数
        sum_A *= 10;
        sum_A += DA;
    }
    while (count_B--)
    {
        sum_B *= 10;
        sum_B += DB;
    }
    cout << sum_A + sum_B << endl;
    return 0;
}

 

posted @   GOGP_nikto  阅读(104)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示