您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之栈一:进制转换

2017-02-13 16:29 337 查看
think:

1 刚才补了一个栈的题目,AC之后查了查之前一开始时候写的代码,真的感觉当时自己不理解,硬生生的套上一些模板,就和自己现在学习树的学习方法是一样的现在回想一下其实但是并没有那么难,自己但是将一种解决问题的思想硬生生的套上模板,感觉有点傻,不过这样也很正常,自己就是这样傻傻的,哈哈, 回归原题,题目的知识点为栈和进制转换,进制转换的方法是必须知道的。下面是百度上的二进制与十进制转换的知识举例:

十进制转二进制:

用2辗转相除至结果为1

将余数和最后的1从下向上倒序写 就是结果

例如302

302/2 = 151 余0

151/2 = 75 余1

75/2 = 37 余1

37/2 = 18 余1

18/2 = 9 余0

9/2 = 4 余1

4/2 = 2 余0

2/2 = 1 余0

故二进制为100101110

二进制转十进制

从最后一位开始算,依次列为第0、1、2…位

第n位的数(0或1)乘以2的n次方

得到的结果相加就是答案

例如:01101011.转十进制:

第0位:1乘2的0次方=1

1乘2的1次方=2

0乘2的2次方=0

1乘2的3次方=8

0乘2的4次方=0

1乘2的5次方=32

1乘2的6次方=64

0乘2的7次方=0

然后:1+2+0

+8+0+32+64+0=107.

二进制01101011=十进制107

2错误反思:

【注意0的进制转换依然为0】

sdut原题链接

数据结构实验之栈一:进制转换

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。

Input

第一行输入需要转换的十进制非负整数;

第二行输入 R。

Output

输出转换所得的 R 进制数。

Example Input

1279

8

Example Output

2377

Hint

Author

以下为Accepted代码

#include <stdio.h>
int main()
{
int stacks[199859];
int top, x, R;
top = 0;
scanf("%d", &x);
scanf("%d", &R);
if(x == 0)
printf("0\n");
while(x)
{
stacks[top++] = x%R;
x /= R;
}
while(top)
{
top--;
printf("%d", stacks[top]);
if(top == 0)
printf("\n");
}
return 0;
}

/***************************************************
User name: jk160630
Result: Accepted
Take time: 0ms
Take Memory: 124KB
Submit time: 2017-02-13 16:07:44
****************************************************/


以下为Wrong Answer代码—0的进制转换

#include <stdio.h>
int main()
{
int stacks[199859];
int top, x, R;
top = 0;
scanf("%d", &x);
scanf("%d", &R);
while(x)
{
stacks[top++] = x%R;
x /= R;
}
while(top)
{
top--;
printf("%d", stacks[top]);
if(top == 0)
printf("\n");
}
return 0;
}

/***************************************************
User name: jk160630
Result: Wrong Answer
Take time: 0ms
Take Memory: 120KB
Submit time: 2017-02-13 16:02:08
****************************************************/


以下为Wrong Answer代码—0的进制转换

#include <stdio.h>
#include <stdlib.h>
#define maxsize 64

struct node
{
int date[maxsize];
int top;
}stack;
//stack->top = 0;

void push(int n, int m)
{
while(n != 0)
{
stack.date[++(stack.top)] = n%m;
n /= m;
}
}

void pop()
{
while(stack.top != 0)
{
printf("%d", stack.date[(stack.top)--]);
}
printf("\n");
}

int main()
{
int n, m;
scanf("%d %d", &n, &m);
push(n, m);
pop();
return 0;
}

/***************************************************
User name: jk160630
Result: Wrong Answer
Take time: 0ms
Take Memory: 112KB
Submit time: 2017-01-13 15:02:01
****************************************************/


以下为Wrong Answer代码——0的进制转换

#include <iostream>
#include <algorithm>

using namespace std;

const int max_size = 104;

class stack{
private:
int tp, num[max_size];
public:
stack(){tp = 0;}
void push(int x);
int top();
void pop();
bool empty();
};

void stack::push(int x){
num[tp++] = x;
}
int stack::top(){
return num[tp-1];
}
void stack::pop(){
tp--;
}
bool stack::empty(){
return tp == 0? true: false;
}

int main(){
int n, r;
while(cin >> n >> r){
stack test;
while(n){
test.push(n%r);
n /= r;
}
while(!test.empty()){
cout << test.top();
test.pop();
}
cout << endl;
}
return 0;
}

/***************************************************
User name:
Result: Wrong Answer
Take time: 0ms
Take Memory: 200KB
Submit time: 2017-10-03 15:39:22
****************************************************/


以下为Wrong Answer代码——0的进制转换

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int tp, link[104];

int main(){
int n, r;
while(~scanf("%d %d", &n, &r)){
tp = 0;
while(n){
link[tp++] = n%r;
n /= r;
}
while(tp){
printf("%d", link[tp-1]);
tp--;
}
printf("\n");
}
return 0;
}

/***************************************************
User name:
Result: Wrong Answer
Take time: 0ms
Take Memory: 152KB
Submit time: 2017-10-03 15:42:25
****************************************************/


以下为Accepted代码

#include <iostream>
#include <algorithm>

using namespace std;

const int max_size = 104;

class stack{
private:
int tp, num[max
123dd
_size];
public:
stack(){tp = 0;}
void push(int x);
int top();
void pop();
bool empty();
};

void stack::push(int x){
num[tp++] = x;
}
int stack::top(){
return num[tp-1];
}
void stack::pop(){
tp--;
}
bool stack::empty(){
return tp == 0? true: false;
}

int main(){
int n, r;
while(cin >> n >> r){
if(!n){
cout << 0 << endl;
continue;
}
stack test;
while(n){
test.push(n%r);
n /= r;
}
while(!test.empty()){
cout << test.top();
test.pop();
}
cout << endl;
}
return 0;
}

/***************************************************
User name:
Result: Accepted
Take time: 0ms
Take Memory: 256KB
Submit time: 2017-10-03 15:49:46
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  错误反思