It is mentioned in the title balloon that we can achieve the goal of 16 ext. 8 through the 2-step medium.
After learning to count the electrical logic, as we all know, 1-bit 16 binary can represent 4-bit 2-binary, 1-bit 8-binary can represent 3-bit binary, and for this reason my code big problem idea is out, string storage, 3-bit 16-sum into the stack output represents 4-bit 8-binary, and then out of the stack output, so I have the first code
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int
STACK[40000];
void transform (string str, int length) {int top =-1;
for (int i = length-1; I >= 0; I-= 3) {int sum = 0; for (int j = 0; J < 3 && i-j >= 0; j + +) {int temp = str[i-j] >= ' 0 ' && St R[I-J] <= ' 9 '?
STR[I-J]-' 0 ': str[i-j]-' A ' + 10; sum + = (temp << (4 * j));
Because each hexadecimal binary is accounted for in the binary 4 bits, so according to the position of the number is different, left 0 bits, 4 bits and 8 bits have reached his original decimal appearance} Stack[++top] = sum;
} while (stack[top] = = 0) {top--;
} for (int i = top; I >= 0; i--) {printf ("%o", Stack[i]);
} cout<<endl;
} int Main () {string *str;
int n;
cin>>n;
str = new String[n];
for (int i = 0; i < n; i++) {cin>>str[i];
} for (int i = 0; i < n; i++) {transform (Str[i], str[i].size ());
} return 0; }
But unfortunately, in the evaluation platform is 0 points, I have tried some data with others, but the test is basically not a string of f is a string of other, but it is to help people find out a mistake, but their mistakes still did not find, once thought is my stack open is not big enough, so the last data overflow, cannot be expressed accurately. Later consulted the C Language bar goddess of the smokers flame I found my Code error place. The wrong place lies in the lazy printf ("%o", Stack[i]); Because I am lazy sums, for this lost many of the leading 0.
The correct result after 1001 conversion should be 10001.
In order to correct this error, I finally converted to a string to see if the current number is enough to 4 bits, for this there is the following code.
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int stack[40000];
void transform (string str, int length)
{
char buff[4];
int top =-1;
for (int i = length-1; I >= 0; I-= 3)
{
int sum = 0;
for (int j = 0; J < 3 && i-j >= 0; j + +)
{
int temp = str[i-j] >= ' 0 ' && Str[i-j] & lt;= ' 9 '? STR[I-J]-' 0 ': str[i-j]-' A ' + ten;
sum + = (temp << (4 * j));
}
Stack[++top] = sum;
}
while (stack[top] = = 0)
{
top--;
}
for (int i = top; I >= 0; i--)
{
printf ("%04o", Stack[i]);
}
cout<<endl;
}
int main ()
{
string *str;
int n;
cin>>n;
str = new String[n];
for (int i = 0; i < n; i++)
{
cin>>str[i];
}
for (int i = 0; i < n; i++)
{
transform (str[i], str[i].size ());
}
return 0;
}
This will allow the answer to be output correctly.