The base number problem, explored the use of while and for

Figure out "last item that meets the criteria" and "first item that does not meet the criteria"

#include<stdio.h>
void main(){
    
    
	int i,n,r,a[33];
	while(scanf("%d%d",&n,&r)!=EOF){
    
    
		i=0;
		if(n<0){
    
    
			printf("-");
			n=-n;
		}
		while(n){
    
    //=while(n!=0) 等于0时跳出,假设一共C个
			a[i]=n%r;//a[0],a[1],a[2],……,a[C-1]
			n/=r;
			i++;//最后i=C
		}
		while(i--){
    
    //a[C-1],a[C-2],……,a[3],a[2],a[1],a[0] while(i--)是先使用再自减,所以是判断的是C>0? C-1>0? 1>0?但是使用的是C-1,c-2  0
			if(a[i]<10)
				printf("%d",a[i]);
			else
				printf("%c",a[i]-10+'A');
		}
		printf("\n");
	}
}
#include<stdio.h>
void main(){
    
    
	int i,r,n,a[33];
	while(~scanf("%d%d",&n,&r)){
    
    
		if(n<0){
    
    
			printf("-");
			n=-n;
		}
		for(i=0;n;i++){
    
    
			a[i]=n%r;
			n/=r;
		}
		for(i-=1;i+1;i--){
    
    
			if(a[i]<10)
				printf("%d",a[i]);
			else
				printf("%c",a[i]-10+'A');
		}
		printf("\n");
	}
}

Summary
//The specific process of while(n–) is to judge whether it is zero from n to 1, and the value of n is used from n-1 to 0 below. A total of n cycles. It is equivalent to for(i=n-1;i+1;i--)
//while(n) is equivalent to for(;n;)
//while() for() In many cases, the difference lies in whether it is the current item or the previous item One item is still the next item
//i++ i–Increment and decrement operators are used first and then add, use first and then subtract

Guess you like

Origin blog.csdn.net/cwindyc/article/details/107009557