zoukankan      html  css  js  c++  java
  • PAT Basic 1016

    1016 部分A+B (15 分)

    正整数 A 的“DA​​(为 1 位整数)部分”定义为由 A 中所有 DA​​ 组成的新整数 PA​​。例如:给定 A=3862767,DA​​=6,则 A 的“6 部分”PA​​ 是 66,因为 A 中有 2 个 6。

    现给定 A、DA​​、B、DB​​,请编写程序计算 PA​​+PB​​。

    输入格式:

    输入在一行中依次给出 A、DA​​、B、DB​​,中间以空格分隔,其中 0<A,B<1010​​。

    输出格式:

    在一行中输出 PA​​+PB​​ 的值。

    输入样例 1:

    3862767 6 13530293 3
    

    输出样例 1:

    399
    

    输入样例 2:

    3862767 1 13530293 8
    

    输出样例 2:

    0

    注意头文件,memset的头文件
    别忘了注意进位,还有最后进位之后的i值
    在c1 c2的长度条件上,加个等号即可
    或者算完i++,反正最后需要去0,从后往前去0
    另外,以为自己没考虑0的情况,在前面多加了个c1=c2=0的特殊判断(在注释里)
    加也就加了,竟然忘了加个return,这个错找了好久
    最后优化一下,这第一版本有点复杂

    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include <vector>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    #define max 1000
    #define debug 0
    using namespace std;

    int main() {
    #if debug
        freopen("in.txt", "r", stdin);
    #endif

        int temp = 0;
        long long A, B;
        int DA, DB, c1 = 0, c2 = 0;
        int SA[max], SB[max];
        memset(SA, 0, sizeof(int) * max);
        memset(SB, 0, sizeof(int) * max);
        cin >> A >> DA >> B >> DB;
        while (A > 0)
        {
            temp = A % 10;
            A /= 10;
            if (temp == DA)
                SA[c1++] = DA;
        }
        while (B > 0)
        {
            temp = B % 10;
            B /= 10;
            if (temp == DB)
                SB[c2++] = DB;
        }
            /*if (c1 == c2&&c1 == 0)
            {
                  cout << 0;
                  return 0;
            }*/
        int i=0;
        for (; i <= c1||i <=c2; i++)
        {
            SB[i] += SA[i];
            if (SB[i] >= 10)
            {
                SB[i] %= 10;
                SB[i + 1] += 1;
            }
        }
        while (SB[i] == 0&&i>0)
            i--;
        while (i + 1)
        {
            cout << SB[i--];
        }
    #if debug
        freopen("CON", "r", stdin);
    #endif
        return 0;
    }

    优化后
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include <vector>
    #include<algorithm>
    #include<string>
    #include<math.h>
    #define max 1000
    #define debug 0
    using namespace std;

    int main() {
    #if debug
        freopen("in.txt", "r", stdin);
    #endif

        int temp = 0;
        long long A, B;
        int DA, DB, c1 = 0, c2 = 0;
        int SA=0, SB=0;
        cin >> A >> DA >> B >> DB;
        while (A > 0)
        {
            temp = A % 10;
            A /= 10;
            if (temp == DA)
                SA=SA*10+DA;
        }
        while (B > 0)
        {
            temp = B % 10;
            B /= 10;
            if (temp == DB)
                SB = SB * 10 + DB;
        }
        cout << SA + SB;
    #if debug
        freopen("CON", "r", stdin);
    #endif
        return 0;
    }
  • 相关阅读:
    7.ASP.NET Core InProcess Hosting
    highcharts.Js
    Csharp: Linq Query
    HtmlAgility 抓取网页上的数据
    更改 Solution (.Sln) file
    Sql: Oracle paging
    css: hide or dispaly div
    Csharp:HttpWebRequest , HttpClient and RestSharp
    Csharp:jquery.ajax-combobox
    Csharp: TreeList Drag and Drop
  • 原文地址:https://www.cnblogs.com/lxzbky/p/10500440.html
Copyright © 2011-2022 走看看