首页 > ACM题库 > HDU-杭电 > hdu 2051 Bitset-栈-[解题报告]C++
2013
12-26

hdu 2051 Bitset-栈-[解题报告]C++

Bitset

问题描述 :

Give you a number on base ten,you should output it on base two.(0 < n < 1000)

输入:

For each case there is a postive number n on base ten, end of file.

输出:

For each case there is a postive number n on base ten, end of file.

样例输入:

1
2
3

样例输出:

1
10
11

数据结构,栈:十进制转换二进制

 

 

 

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 10005
typedef int Elemtype;
typedef struct Stack
{
    Elemtype data[M];
    int top;
}Stack;
struct Stack *Init()
{
    struct Stack *s;
    s= (struct Stack *)malloc(sizeof(struct Stack ));
    if(s!= NULL)
    {
        s->top = -1;
        return s;
    }
    else exit(0);
}

int main()
{
    struct Stack *s;
    int n,temp;
    s = Init();
    while(scanf("%d",&n)!= EOF)
    {
        while(n)
        {
            s->top++;
            s->data[s->top] = n%2;
            n = n/2;
        }
        while(s->top != -1)
        {
            temp = s->data[s->top];
            s->top--;
            printf("%d",temp);
        }
        printf("\n");
    }
    return 0;
}

 

解题转自:http://blog.csdn.net/chuck_0430/article/details/8690654


,
  1. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测