zoukankan      html  css  js  c++  java
  • hdu 2031 解题报告

    题目链接: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;
    }
  • 相关阅读:
    Django01
    WEB框架介绍
    前端插件介绍
    JQuery
    DOM
    js
    css
    HTML
    图片懒加载
    js中style,currentStyle和getComputedStyle的区别
  • 原文地址:https://www.cnblogs.com/yujiaao/p/2160885.html
Copyright © 2011-2022 走看看