藍橋練習總結(C語言)——基礎篇(二)

進制轉換

總體思路(只討論常用的二、八、十、十六):

  • 十轉N
    除以N取餘,餘數逆序排列
  • N轉十
    按權展開相加
  • 八或十六 與二的轉換
    三位二進制對應一位八進制
    四位二進制對應一位十六進制
  • 八與十六間的轉換
    找中間進制:二或十
    先把要轉的轉換成二進制或十進制,再把二進制或十進制轉換爲目標進制

藍橋例題:

以下只是本人想出來的代碼,不代表最簡單最高效的
  • 十轉十六:

    #include <stdio.h>
    #include <stdlib.h>
    
    int a[100000000];	//這一行如果寫在main函數中會報錯,溢出緩存區,涉及到局部變量與全局變量的堆棧儲存問題
    int main(int argc, char *argv[]) {
    	int n,i=0,j;
    	scanf("%d",&n);
    	if(n==0)
    		printf("0");
    	else
    	{
    		while(n)
    		{
    			a[i]=n%16;
    			n/=16;
    			i++;
    		}
    		for(j=i-1;j>=0;j--)
    		{
    			switch(a[j])
    			{
    				case 10:printf("A");break;
    				case 11:printf("B");break;
    				case 12:printf("C");break;
    				case 13:printf("D");break;
    				case 14:printf("E");break;
    				case 15:printf("F");break;
    				default:printf("%d",a[j]);
    			}
    		}
    	}
    	return 0;
    }
    
  • 十六轉十

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[]) {
    	char a[9];
    	gets(a);
    	int n,i,j;
    	long long num = 0;
    	n=strlen(a);
    	int b[n];
    	for(i=0,j=n-1;i<n;i++,j--)
    	{
    		if(a[i]=='0')
    			b[j]=0;
    		else if(a[i]=='1')
    			b[j]=1;
    		else if(a[i]=='2')
    			b[j]=2;
    		else if(a[i]=='3')
    			b[j]=3;
    		else if(a[i]=='4')
    			b[j]=4;
    		else if(a[i]=='5')
    			b[j]=5;
    		else if(a[i]=='6')
    			b[j]=6;
    		else if(a[i]=='7')
    			b[j]=7;
    		else if(a[i]=='8')
    			b[j]=8;
    		else if(a[i]=='9')
    			b[j]=9;
    		else if(a[i]=='A')
    			b[j]=10;
    		else if(a[i]=='B')
    			b[j]=11;
    		else if(a[i]=='C')
    			b[j]=12;
    		else if(a[i]=='D')
    			b[j]=13;
    		else if(a[i]=='E')
    			b[j]=14;
    		else if(a[i]=='F')
    			b[j]=15;
    		else
    			b[j]=a[i];
    	}
    	for(i=0;i<n;i++)
    	{
    		num+=b[i] * pow(16,(double)i);
    	}
    	printf("%lld",num);
    	return 0;
    }
    
  • 十六轉八(來源於博客https://blog.csdn.net/qq_39405494/article/details/87987926)

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_SIZE 100000
    int main()
    {
        int n;
        int i,j,k;
        int result;
        int str_len;
        scanf("%d",&n);
        char str[n][MAX_SIZE];
        char demo[16][4] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
        memset(str,0,n*MAX_SIZE);
        for(i=0;i<n;i++){
            scanf("%s",str[i]);
            k = 0;
            str_len = strlen(str[i]);
            int ss = 4*str_len%3; 
            char temp[4*str_len+3-ss];
            memset(temp,0,4*str_len+3-ss);
            if( ss == 1){
                strcat(temp,"00");
                k += 2;
            }
            if( ss == 2){
                strcat(temp,"0");
                k += 1;
            }
            for(j=0;j<str_len;j++)
            {
                if(str[i][j]<65){
                    temp[k++] = demo[str[i][j]-48][0];
                    temp[k++] = demo[str[i][j]-48][1];
                    temp[k++] = demo[str[i][j]-48][2];
                    temp[k++] = demo[str[i][j]-48][3];
                }
                else if(str[i][j]>=65){
                    temp[k++] = demo[str[i][j]-55][0];
                    temp[k++] = demo[str[i][j]-55][1];
                    temp[k++] = demo[str[i][j]-55][2];
                    temp[k++] = demo[str[i][j]-55][3];
                }
            }
            temp[k] = 0;
            for(j=0;j<4*str_len;j+=3)
            {
                if(j==0){
                    result=(4*(temp[j]-48) + 2*(temp[j+1]-48) + (temp[j+2]-48));
                    if( result != 0)
                    printf("%d",result);
                }
                else{
                result=(4*(temp[j]-48) + 2*(temp[j+1]-48) + (temp[j+2]-48));
                printf("%d",result);
                }
            }
            printf("\n");
        }
        return 0;
    }
    //此代碼來源於博客https://blog.csdn.net/qq_39405494/article/details/87987926,該作者寫的比較詳細,可以去看一下
    //侵刪
    
  • 特別說明一點:
    在devcpp中,用變量來定義數組不會報錯,也會正常運行,提交到藍橋系統中也判定正確,說明在比賽的時候也是可以用的。

發佈了11 篇原創文章 · 獲贊 11 · 訪問量 925
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章