您的位置:首页 > 其它

hdu 2031 解题报告

2011-08-31 16:25 162 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2031

其实这个题可以直接用stl中的栈的 不过我试了一下用数组模拟栈 发现自己的问题

题目很简单 就是每次不停地求商 把余数放入栈中 最后一口气弹出就行了

※写模拟的时候出现问题 p=st;表示p的指针指向st[0] 而不是自己想的栈底 所以要先p++后进数 相当于st[0]不用※

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int num;
int st[100];
int *p=st;
void push(int n)
{
p++;
*p=n; //注意此处先改变指针  后进入数据

}
int pop()
{
int temp=*p;
p--;
return temp;
}
int main()
{
int n,m,i,j,k,r,count=0,temp=0;
bool is_under;
while(scanf("%d %d",&n,&m)!=EOF)
{
is_under=0;
if(n<0)
{
is_under=1;
n=-n;
}
count=0;
while(n!=0)
{
r=n%m;
n=n/m;
push(r);
count++;
}
if(is_under)
printf("-");
for(i=0;i<count;i++)
{
temp=pop();
printf("%c",a[temp]);
}
p=st;
printf("\n");
}
//system("pasue");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: