方法一 (硬算)
#include#include #include #include typedef struct StackNode{ int data; struct StackNode *next; }StackNode,*linkstack; void InitStack(linkstack *S){ *S=NULL; } void PushStack(linkstack *S,int x){ StackNode *p; p=NULL; p=(linkstack)malloc(sizeof(StackNode)); (*p).data=x; (*p).next=*S; *S=p; } void PopStack(linkstack *S,int *x){ StackNode *p; if(*S==NULL) printf("栈空"); *x=(*S)->data; p=*S; *S=(*S)->next; free(p); } bool EmptyStack(linkstack S){ if(S==NULL) return true; else return false; } int main() { StackNode *S; int n,m; int i,j,x; int sum,num; InitStack(&S); scanf("%d%d",&n,&m); i=0;sum=n;num=0; while(pow(m,i)<=n){ PushStack(&S,i); i++; } printf("%d(10)=",n); while(!EmptyStack(S)){ PopStack(&S,&x); num=pow(m,x); sum=sum-num; if(sum>=0){ j=1; sum=sum+num; while(sum-j*pow(m,x)>=0){ j++; } printf("%d",j-1); sum=sum-(j-1)*pow(m,x); } else{ sum=sum+num; printf("0"); } } printf("(%d)",m); return 0; }
方法二 :
#include#include #include #include typedef struct StackNode{ int data; struct StackNode *next; }StackNode,*linkstack; void InitStack(linkstack *S){ *S=NULL; } void PushStack(linkstack *S,int x){ StackNode *p; p=NULL; p=(linkstack)malloc(sizeof(StackNode)); (*p).data=x; (*p).next=*S; *S=p; } void PopStack(linkstack *S,int *x){ StackNode *p; if(*S==NULL) printf("栈空"); *x=(*S)->data; p=*S; *S=(*S)->next; free(p); } bool EmptyStack(linkstack S){ if(S==NULL) return true; else return false; } int main() { StackNode *S; int n,m; int i,j,x; InitStack(&S); scanf("%d%d",&n,&m); printf("%d(10)=",n); while(n){ PushStack(&S,n%m); n=n/m; } while(!EmptyStack(S)){ PopStack(&S,&x); printf("%d",x); } printf("(%d)",m); return 0; }
节省太多了,呜呜呜



