c++的起始之路
c++是真的好用,记住了头文件函数库就可以省下很大的空间,没有c那么繁琐,很多东西并不需要花时间去写,今晚刚接触栈和队列的第一道题,以下是题目以及c和c++的代码。
数据结构实验之栈与队列一:进制转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。
Input
第一行输入需要转换的十进制非负整数;
第二行输入 R。
Output
输出转换所得的 R 进制数。
Sample Input
1279
8
Sample Output
2377
#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
int main()
{
int N, R;
stack<int> s;
queue<int> t;
cin>>N>>R;
if(N==0)
{
cout<<0<<endl;
}
else
{
while(N)
{
s.push(N%R);
t.push(N%R);
N/=R;
}
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
while(!t.empty())
{
cout<<t.front();
t.pop();
}
cout<<endl;
}
return 0;
}
这个是使用c++写的,包括了栈和队列的输入输出(并不是全部是这道题的答案)还有队列的部分;
#include <stdio.h>
#include <stdlib.h>
struct stacks
{
int *base;
int *top;
};
int main()
{
int N, R;
struct stacks s;
scanf("%d %d", &N, &R);
if(N==0)
{
printf("0\n");
}
else
{
s.base=(int *)malloc(sizeof(int[100]));
s.top=s.base;
while(N)
{
s.top++;
*s.top=N%R;
N/=R;
}
while(s.top!=s.base)
{
int t=*s.top;
printf("%d", t);
s.top--;
}
printf("\n");
}
return 0;
}
这个是用c写的AC代码,只含栈的部分,第一个代码38行,第二个36行,但第一个包含栈和队列的基本输入输出,优胜立下。
最后我想说一句,我要转c++了(^ v ^)。
最后说点什么好呢?提前祝大家新年快乐吧!