Do not understand binary conversion, I could not even stand right B are not chasing Fan

This article was a little party the title, emmm, is not important, this is a station of the hit sci-fi comedy brainless youth campus Fan "fairy king of everyday life", let me once again understand, do not learn from it, even after Fan should be left behind.

 

See the following classic scene (Guards man the Lord will return NULL overflow spiritual power of accomplishments with 175 points):

 

 


Astrotech yards , two appearances, it is the Taoist culture, yin and yang, male and female, black and white, heaven and earth, the parity ......

Liang Yi, Taoism culture term, middle finger in classical philosophy is "yin and yang", mainly black and white color, is this road trip. World beginning, everything is chaos, for Promise, Promise of Health Tai Chi two appearances, two appearances as yin and yang. "Book of Changes": "easy to Tai Chi, beginning two appearances, two appearances of Health four images, four images and raw gossip." Astrotech in the "Book of Changes" middle finger Yin (- -) Yang (-). About "astrotech," said the comprehensive theory of the ancient Yi Jing, the count of eight, said: One that is yin and yang, one that for the world, one that is parity, one that is rigid-flexible, one that is Xuan Huang, one that is heaven and earth , one that is Spring, and one that is constant change. But usually it refers to the yin and yang. All things, all things in the world, takes the ancients as: yin and yang. When yin and yang is the world points out, clear air up for the day. Aggregate downwardly ground. Days yang for yin. All things, all things in the world, takes the ancients as: yin and yang.

So two appearances code in the end this is what?

 

 

Universe dry Kun dry dry dry dry , heaven and earth, or Taoism, heaven and earth, sun and moon, yin and yang, hard and soft ......

Universe is a Chinese vocabulary, spelling is qián kūn. Taoist cultural terms, a means "easy" and Qian Kun. Two-finger world. Three fingers sun and the moon. Four fingers of yin and yang; hard and soft. Fingers countries; country; world. Six-fingered situation, the overall situation. Seven refers to the emperor, after. Eight-fingered mystery; tricks. In Tai Chi gossip heaven and earth to mainly dry triple, six off-kun, the two Gua all-inclusive, is the source of all things, usually on behalf of the world, north and south, yin and yang. Congenital gossip, heaven and earth are set north and south, Kanli given something that is far apart for the order, for the day on a dry, under ground Kun, left for the East is from the right to the West as Canton. Dry, Kun is in the two Gua congenital gossip, dry for days, Kun for, on behalf of heaven and earth heaven and earth.

那么乾坤到底该是个什么?

 

 

我去,***不就是二进制吗?(动脑子想想,这小编,怕不是道教文化毕业辅修计算机专业。两仪码,就是二进制码,乾是1坤是0,的确符合道教术语解释)。

对不起,我不配,是我拖后腿了......

 

 

进制转换,计算机最基础的知识点,早已经被各种各样的计算器和进制转换机封装,方便使用的同时,你是否真的理解并学会了进制转换?离开了计算器和进制转换机,你是否还具有看懂动漫的能力?

进入正题,常见的进制转换一般是在二进制、八进制、十进制、十六进制等的转换。求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。

 

以二进制和十进制的相互转换为例:

在二进制转换为十进制时,需要使用到二进制的每一个字符和二进制的长度,即二进制的每一位字符乘2的(i-1)次方的和为十进制
程序描述:

将a进制的n(n是字符数组)转换为10进制的数。

int func(int a,char n[]){  int len=strlen(n);  int i,ans=0;  for(i=0;i<len;i++){    //二进制到十进制之间使用的运算规则     if(n[i]>='0'&&n[i]<='9')      ans=ans+pow(a,len-1-i)*(n[i]-'0');    //十进制之后需要增加的运算规则     else if(n[i]>='a'&&n[i]<='z')      ans=ans+pow(a,len-1-i)*(n[i]-'a'+10);    else      ans=ans+pow(a,len-1-i)*(n[i]-'A'+10);  }  return ans;}

在十进制转换为二进制时,需要对所求的数进行循环取余和取商的操作,直到符合要求,需要注意两点是:
1.当所求值为0时,需要特判;
2.结果需要逆序输出,如上图。
程序描述:

将10进制的ans转换为b进制的数。

void fun(int ans,int b){  char arr[100];  int i,j=0,tmp;  while(ans!=0){    tmp=ans%b;//取出余数     if(tmp>=10)//大于等于十,需要使用字母       arr[j++]=tmp-10+'A';    else//小于十,使用数字       arr[j++]=tmp+'0';    ans=ans/b;   }   //当ans为0,也即j为0,需要特判;  if(j==0)    arr[j++]='0';  //逆序输出   for(i=j-1;i>=0;i--){    printf("%c",arr[i]);  }}

以上的两个函数结合起来,就可以在任意进制之间的转换。任何的进制都可以先转换到十进制,再由十进制转换为对应的进制(潜移默化的意识);

#include<cstdio> #include<cstring>#include<cmath>const int maxn = 10010;int func(int a,char n[]){  int len=strlen(n);  int i,ans=0;  for(i=0;i<len;i++){    //二进制到十进制之间使用的运算规则     if(n[i]>='0'&&n[i]<='9')      ans=ans+pow(a,len-1-i)*(n[i]-'0');    //十进制之后需要增加的运算规则     else if(n[i]>='a'&&n[i]<='z')      ans=ans+pow(a,len-1-i)*(n[i]-'a'+10);    else      ans=ans+pow(a,len-1-i)*(n[i]-'A'+10);  }  return ans;}void fun(int ans,int b){  char arr[100];  int i,j=0,tmp;  while(ans!=0){    tmp=ans%b;//取出余数     if(tmp>=10)//大于等于十,需要使用字母       arr[j++]=tmp-10+'A';    else//小于十,使用数字       arr[j++]=tmp+'0';    ans=ans/b;   }   //当ans为0,也即j为0,需要特判;  if(j==0)    arr[j++]='0';  //逆序输出   for(i=j-1;i>=0;i--){    printf("%c",arr[i]);  }}int main(){  int a,b,x=0;  char n[maxn];   scanf("%d%s%d",&a,n,&b);  x=func(a,n);  fun(x,b);  printf("\n");  return 0;}

不,还没完,在c++的函数库中,还有一种更加简单的函数,可以直接使用,原谅我把这最简单的不用思考就可以解题的方法留到文末

 

C++库函数(strtol()/itoa())进制转换

1.strtol()函数:

作用:将一个任意1-36进制数转化为10进制数,返回是long int型。

 long int strtol(const char *str, char **endptr, int base);

把参数 str 所指向的字符串根据给定的 base 转换为一个长整数(类型为 long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

函数描述:

long ret = strtol(n,&result,a);

n -- 要转换为长整数的字符串。

result -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。

a -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

 

实例:

输入进制大小和该进制下的数,输出转换成功后对应的十进制数:

#include<cstdio>#include<cstdlib>#include<cstring>const int maxn = 10010;int main(){  int a;  char n[maxn];  char *result;  scanf("%d%s",&a,n);  long ret = strtol(n,&result,a);  printf("%ld",ret);  return 0;}
 

640?wx_fmt=png

 

2.itoa()函数:

作用:将一个10进制的数转化为n进制的值、其返回值为char型。(和上面的strtol效果相反)

char* itoa(int val,char* dst,int radix = 10);//定义的时候默认指定10进制

函数描述:

itoa(a,result,b);

a为要转换的数,result用来存储转换结果,b为要转变的进制数。

 

实例:

输入一个十进制的数和将要转变的进制,输出转换成功后对应的进制数:

#include<cstdio>#include<cstdlib>#include<cstring>const int maxn = 10010;int main(){  int a,b;  char result[maxn];  scanf("%d%d",&a,&b);  itoa(a,result,b);  printf("%s",result);  return 0;}

 

640?wx_fmt=png

strtol()函数和itoa()函数也可以很好的解决任意进制之间的转换:

#include<cstdio>#include<cstdlib>#include<cstring>const int maxn = 10010; int main(){  int a,b;  char n[maxn];   char *result;  scanf("%d%s%d",&a,n,&b);  long ret = strtol(n,&result,a);  itoa(int(ret),n,b);  printf("%s",n);  return 0;}

 

来源于:微信公众号【李歘歘】

作者:李歘歘

扫码关注,领取众多粉丝福利,阅读更多原创文章,联系作者

 

发布了312 篇原创文章 · 获赞 570 · 访问量 41万+

Guess you like

Origin blog.csdn.net/qq_42410605/article/details/104190705