zoukankan      html  css  js  c++  java
  • PAT 乙级 1048 数字加密(20) C++版

    1048. 数字加密(20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。

    输入格式:

    输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。

    输出格式:

    在一行中输出加密后的结果。

    输入样例:
    1234567 368782971
    
    输出样例:
    3695Q8118


    思路:先将短的字符串头部添加字符0,知道两个字符串相等长度为止,只是为了后续操作更加简便,再将两个字符串分别以字符的形式读入对应的字符栈,此时两个栈的长size一定相同
    ,用栈的一个好处就是因为个位是最后一位,栈先进后出,正好满足,然后将处理后的结果存入一个新的栈,该栈也是为了从高位开始输出,代码如下,代码行数可以更少,但是我写的方式是为了让main函数更加简洁

     1 // 1048.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<iostream>
     6 #include<stack>
     7 #include<string>
     8 #include<typeinfo>
     9 
    10 using namespace std;
    11 
    12 //函数声明
    13 void show_result(stack<char>& s1, stack<char>& s2);
    14 void Push(stack<char>& s, string str);
    15 void put_zero(stack<char>& s1, stack<char>& s2, const string& str1, const string& str2);
    16 
    17 int main()
    18 {
    19     string str1, str2;
    20     stack<char> s1, s2;
    21 
    22     cin >> str1 >> str2;
    23 
    24     //将长度小的字符串对应的栈先填入字符0,方便后续计算
    25     put_zero(s1, s2, str1, str2);
    26 
    27     //将字符串入对应的栈
    28     Push(s1, str1);
    29     Push(s2, str2);
    30 
    31     //程序实现并打印
    32     show_result(s1, s2);
    33 
    34     return 0;
    35 }
    36 
    37 //将长度小的字符串对应的栈先填入字符0
    38 void put_zero(stack<char>& s1, stack<char>& s2, const string& str1, const string& str2)
    39 {
    40     int len = str1.length() - str2.length();
    41 
    42     if (len>0)
    43     {
    44         for (int i = 0; i < len; ++i)
    45             s2.push('0');
    46     }
    47     else
    48     {
    49         for (int i = 0; i < -len; ++i)
    50             s1.push('0');
    51     }
    52 }
    53 
    54 //将字符串入栈
    55 void Push(stack<char>& s, string str)
    56 {
    57     int i;
    58 
    59     for (i = 0; str[i] != ''; ++i)
    60         s.push(str[i]);
    61 
    62 }
    63 
    64 //程序实现
    65 void show_result(stack<char>& s1, stack<char>& s2)
    66 {
    67     int i,len=s1.size();
    68     stack<char> s;
    69     int a, b,temp;
    70     char Arr[13] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K' };
    71 
    72     for (i = 0; i < len; ++i)
    73     {
    74         a = static_cast<int>(s1.top() - 48);
    75         b = static_cast<int>(s2.top() - 48);
    76 
    77         if (i & 1)//偶数位
    78             temp=(b-a) >= 0 ? b-a : b-a+10;
    79         else//奇数位
    80             temp = (a + b) % 13;
    81 
    82         s.push(Arr[temp]);//将新得到的字符存入新的栈中
    83 
    84         s1.pop(), s2.pop();//栈顶元素出栈
    85     }
    86 
    87     while (!s.empty())//处理后的栈输出
    88     {
    89         cout << s.top();
    90 
    91         s.pop();
    92     }
    93 
    94     cout << endl;
    95 }
  • 相关阅读:
    解决:insert Vodafone sim card,open the mms read report,when receive the read report,cann&#39;t download..
    为什么不记录慢速查询?
    [TypeScript] Understand lookup types in TypeScript
    [Angular] How to styling ng-content
    [Http] Understand what an HTTP Request is
    [AngularJS] Directive for top scroll bar
    [SCSS] Write Custom Functions with the SCSS @function Directive
    [SCSS] Loop Over Data with the SCSS @each Control Directive
    [GraphQL] Deploy a GraphQL dev playground with graphql-up
    [Javascript] Different ways to create an new array/object based on existing array/object
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/7207295.html
Copyright © 2011-2022 走看看