Sometimes you just need a straight SWITCH

Posted lym11248

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sometimes you just need a straight SWITCH相关的知识,希望对你有一定的参考价值。

I used to sovle questions by spilt it into very basic parts.

But sometimes it is hard to rebuild the "wheel" or just not convenient to do so.

Not only do I have this experience in coding,but also in sovling math problems.

I have thought a lot about it.

Let‘s face the fact,it do cultivate the value of deep analyze,which makes you see the essence of things.

But it‘s not,especially in examing and contest.

A better way is given below:

1.find a possible solution and think a little bit deeper,in the realization process especially.

2.ask the question:

  is there anything I can use that can lead to different solutions?

  is there anything I can use that can easy the realization process?

  what more assosiation can I make by reviewing the questions?

In a word,think more,ask more and observe more before you take actions.

 

Let‘s get down to business.

Q:Make the transformation from hexadecimal to octonary number system.

ps.the hexadecimal number can be very strange.

for example:

B441E2411DC709E111C7E1E7ACB6F8CAC0BB2FC4C8BC2.............AE3BAAAB9165CC458E199CB89F51B135F7091A5ABB0874DF3E8CB45

The first way come to my mind is to use the header file <iomanip>

If we can change the input base and out base,it will be a very simple question.

Here it is:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    long long n;
    cin>>hex>>n;
    cout<<oct<<n<<endl;
    return 0;
}

And indeed it is a very nice solution if the input isn‘t "strange".

So,we have to find another way to solve it.

We all know that hexadecimal number can be transformed to octonary number through binary number.

P:every 4 bits represents 1 hexadecimal number and every 3 bits represents 1 octonary number.

So,maybe you will think about this solution:convert the input to binary number and separate them in every 3 bits.

Here comes the point:instead of calculate the 4/3 bits number  by pow or shift operator,we can just give the corresponding number and use switch function.

Such as:"A" represents "1010" and separate it into "001","010",they correspond "1" and "2".

The complete code is given below.

#include <iostream>
#include <string>
using namespace std;
 
int main(){
    int n;
    cin>>n;
    for(int k=1;k<=n;k++){
        string s1,s2;
        cin>>s1;
        s2="";
        for(int i=0;i<s1.length();i++){
            switch(s1[i]){
                case 0:s2+="0000";break;
                case 1:s2+="0001";break;
                case 2:s2+="0010";break;
                case 3:s2+="0011";break;
                case 4:s2+="0100";break;
                case 5:s2+="0101";break;
                case 6:s2+="0110";break;
                case 7:s2+="0111";break;
                case 8:s2+="1000";break;
                case 9:s2+="1001";break;
                case A:s2+="1010";break;
                case B:s2+="1011";break;
                case C:s2+="1100";break;
                case D:s2+="1101";break;
                case E:s2+="1110";break;
                case F:s2+="1111";break;
                default:break;
            }
        }
        int len=s2.length();
        if(len%3==1)
            s2="00"+s2;
        else if(len%3==2)
            s2="0"+s2;
        int flag=0;
        for(int i=0;i<=s2.length()-3;i+=3){
            int num=4*(s2[i]-0)+2*(s2[i+1]-0)+(s2[i+2]-0);
            if(num)
                flag=1;
            if(flag)
                cout<<num;
        }
        cout<<endl;
    }
    return 0;
}

It tells that if the details are hard to realize and you know the corresponding answer for each case,you can just skip the details.

STRAIGHT SWICTH.

Simple questions isn‘t it?

 

Input:

B441E2411DC709E111C7E1E7ACB6F8CAC0BB2FC4C8BC2AE3BAAAB9165CC458E199CB89F51B135F7091A5ABB0874DF3E8CB45

 

Output:

13210170440435616047410434374171726266761453005662770462136052707352525621313461054341463456117521542327670221513256604164676372145505

 

Absolutely strange!

 

以上是关于Sometimes you just need a straight SWITCH的主要内容,如果未能解决你的问题,请参考以下文章

Rust sometimes needs manual type annotation

Rust sometimes needs manual type annotation

When you hit a wall, just kick it in.

The React Router Cheatsheet – Everything You Need to Know

Life is short, You need Python

Life is short,you need python!|