After a project has been running for a long time, I find that my code implementation capabilities are getting worse and worse! After looking for a job some time ago, You can obviously feel that your code is not mature enough! Therefore, more exercises are required in the future. If you want to be a good programmer, you have to think more about code.
★Conclusion
The stack concept is used to compress the remainder obtained by integer N and R, and then compress the remainder obtained by N/R to the stack until n/R is less than R. You can use arrays to simulate stack operations. You can store the remainder into an array and then output it in reverse order!
★Code Implementation
#include <stdio.h>int main(){ int a,b,c,n,r,i,flag,len; char s[100]; while(scanf("%d %d",&n,&r)!=EOF) { if(n >= 0) { b = n; i = 0; while(b > r) { a = b; s[i++] = a%r; b /= r; } s[i] = b; flag = i; } else { b = -n; i = 0; while(b > r) { a = b; s[i++] = a%r; b /= r; } s[i++] =b; s[i] = '-'; flag = i; } for(i = flag;i >=0;i --) { if(s[i]=='-') { printf("%c",s[i]); continue; } if(s[i]==10) printf("A"); else if(s[i]==11) printf("B"); else if(s[i]==12) printf("C"); else if(s[i]==13) printf("D"); else if(s[i]==14) printf("E"); else if(s[i]==15) printf("F"); else printf("%d",s[i]); } printf("\n"); } return 0; }
Hdoj2031 hexadecimal conversion