1 条题解

  • 0
    @ 2025-4-12 21:33:54

    C :

    #include<stdio.h>
    #define NO 0
    #define OK 1
    #define Stack_Size 50
    typedef int StackElementType;
    typedef struct 
    {
    StackElementType elem[Stack_Size];
    int top;
    }SeqStack;
    void InitStack(SeqStack *S)
    {
    S->top=-1;
    }
    int Push(SeqStack *S,StackElementType x)
    {
       if(S->top==Stack_Size)
           return(NO);
       else 
       {
    S->top++;
    S->elem[S->top]=x;
    return(OK);
       }
    }
    int Pop(SeqStack *S,StackElementType *x)
    {
       if(S->top==-1)
           return(NO);
       else
       {
    *x=S->elem[S->top];
    S->top--;
    return(OK);
       }
    }
    int GetPop(SeqStack *S,StackElementType *x)
    {
    if(S->top==-1)
    return(NO);
    else
    {
    *x=S->elem[S->top];
    return (OK);
    }
    }
    void Converson(int N,int n,SeqStack S,int x)
    {
    InitStack(&S);
    while(N>0)
    {
    x=N%n;
    Push(&S,x);
    N=N/n;
    }
    while(S.top>-1)
    {
    Pop(&S,&x);
    printf("%d",x);
    
    }
    
    } 
    void main()
    {
    SeqStack S;
    int N,n,x;
    scanf("%d%d",&N,&n);
    Converson(N,n,S,x);
    printf("\n");
    }
    
    
    
    
    
    
    
    
    
    
    

    C++ :

    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    #define TRUE 1
    #define FALSE 0
    #define Stack_Size 50
    #include <iostream>
    using namespace std;
    typedef int StackElementType; 
    typedef struct		//初始化顺序栈
    {
    	StackElementType elem[Stack_Size];
    	int top;
    }SeqStack;
    void InitStack(SeqStack *S)
    {
    	S->top=-1;
    }
    int Push(SeqStack *S,StackElementType x)	//顺序栈进栈
    {
    	if(S->top==Stack_Size-1)
    		return(FALSE);
    	S->top++;
    	S->elem[S->top]=x;
    	return(TRUE);
    }
    int Pop(SeqStack *S,StackElementType *x)//出栈
    {
    	if(S->top==-1)
    		return(FALSE);
    	else
    	{
    		*x=S->elem[S->top];
    	S->top--;
    		return(TRUE);
    	}
    }
    int GetTop(SeqStack *S,StackElementType *x)	//顺序栈读栈顶元素
    {
    	if(S->top==-1)
    		return(FALSE);
    	else
    	{
    		*x=S->elem[S->top];
    		return(TRUE);
    	}
    }
    void StackOutput(SeqStack *S/*,StackElementType *x */)	//输出
    {
    	int i;
        for(i=0;i<S->top;i++)
    	cout<<S->elem[i]<<endl;    
    }
    
    void Converson(int N,int n)		//进制转换
    {	
    	SeqStack S;
    	InitStack(&S);
    	int x;
    	while(N>0)
    	{
    		x=N%n;
    		Push(&S,x);
    		N=N/n;
    	}
    while(S.top>-1)
    	{
    		Pop(&S,&x);
    		cout<<x;
    	}
    }
    int main()
    {
    	int N,n,x;
    	cin>>N>>n;
    	x=0;
    	Converson(N,n);
      return 0;
    }
    

    Pascal :

    var x,n,i,j:longint;a:array[1..100] of longint;
    begin
    readln(x,n);i:=0;
    while x>0 do
    begin
    inc(i);
    a[i]:=x mod n; x:=x div n;
    end;
    for j:=i downto 1 do
    if a[j]>=10 then write(chr(a[j]+55))
    else   write(a[j]);
    writeln;
    end.
    
    
    • 1

    信息

    ID
    698
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者