PAT B 1048 Digital Encryption (c + + edition)

Source: Internet
Author: User

1048. Digital encryption (20) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

A digital encryption method is required for this subject. First fixed an encryption with a positive integer A, any positive integer b, each of its 1 digits and a corresponding position on the number of the following operation: to the odd digits, the corresponding bit of the number added to 13 to take the remainder--here with J for 10, Q for 11, K for 12, and dual digits minus a number of a number, If the result is negative, then add 10. This makes the digit 1th.

Input format:

The input is given a and b in a row, each of which is a positive integer of no more than 100 bits, separated by a space.

Output format:

Outputs the encrypted result in a row.

Input Sample:
1234567 368782971
Sample output:
3695q8118


Idea: This idea is clearer, first add a short string to the head of the character 0, know two strings equal length, just for the next operation more convenient, and then two strings in the form of a character read into the corresponding word Fu Yi, at this time two stacks of long size must be the same
, a benefit of the stack is because the bit is the last, stack FIFO, just meet, and then the result of processing into a new stack, the stack is to start from the high output, the code as follows, the number of lines of code can be less, but I wrote the way to make the main function more concise

1 //1048.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <stack>7#include <string>8#include <typeinfo>9 Ten using namespacestd; One  A //function Declaration - voidShow_result (stack<Char>& S1, stack<Char>&S2); - voidPush (stack<Char>& S,stringstr); the voidPut_zero (stack<Char>& S1, stack<Char>& S2,Const string& Str1,Const string&str2); -  - intMain () - { +     stringstr1, str2; -stack<Char>s1, S2; +  ACIN >> STR1 >>str2; at  -     //The string corresponding to the small length of the stack is filled in the character 0, convenient for subsequent calculations - Put_zero (S1, S2, str1, str2); -  -     //put the string into the corresponding stack - Push (S1, str1); in Push (S2, str2); -  to     //program implementation and printing + Show_result (S1, S2); -  the     return 0; * } $ Panax Notoginseng //The stack that corresponds to a small length string is filled with the character 0 first - voidPut_zero (stack<Char>& S1, stack<Char>& S2,Const string& Str1,Const string&str2) the { +     intLen = Str1.length ()-str2.length (); A  the     if(len>0) +     { -          for(inti =0; i < Len; ++i) $S2.push ('0'); $     } -     Else -     { the          for(inti =0; i <-len; ++i) -S1.push ('0');Wuyi     } the } -  Wu //to put a string into a stack - voidPush (stack<Char>& S,stringstr) About { $     inti; -  -      for(i =0; Str[i]! =' /'; ++i) - S.push (Str[i]); A  + } the  - //Program Implementation $ voidShow_result (stack<Char>& S1, stack<Char>&S2) the { the     inti,len=s1.size (); thestack<Char>s; the     intA, b,temp; -     Chararr[ -] = {'0','1','2','3','4','5','6','7','8','9','J','Q','K' }; in  the      for(i =0; i < Len; ++i) the     { AboutA = static_cast<int> (S1.top ()- -); theb = static_cast<int> (S2.top ()- -); the  the         if(I &1)//even digits +temp= (b-a) >=0? B-a: b-a+Ten; -         Else//Odd digits theTemp = (A + b)% -;Bayi  theS.push (Arr[temp]);//put the new characters into the new stack the  -S1.pop (), S2.pop ();//stack top element out stack -     } the  the      while(!s.empty ())//post-processing stack output the     { thecout <<s.top (); -  the S.pop (); the     } the 94cout <<Endl; the}

PAT B 1048 Digital Encryption (c + + edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.