您的位置:首页 > 其它

各进制转换

2016-07-23 17:03 471 查看
通过数据库转换:

#include <iostream>  

using namespace std;  

  

int main()  

{  

    int test=64;  

    cout<<"DEC:"<<test<<endl;  

    cout<<"OCT:"<<oct<<test<<endl;//八进制  

    cout<<"HEX:"<<hex<<test<<endl;//十六进制  

  

    return 0;  

}
运用递归运算:
#include <iostream>

using namespace std;

void foo(int n, int base)
{
if (n == 0) { cout << endl; return; }
foo(n / base, base);
cout << n % base;
}

int main()
{
int i = 12345;
//cin >> i;
foo(i, 8);
}
辗转相除法:
#include <math.h>
int L[100];
using namespace std;
int main()
{
int a,A;
_int64 hk,ls;

while(scanf("%d",&a),a)
{
scanf("%I64d%I64d",&hk,&ls);
A=hk+ls;
int i=0;
if(A==0) {
printf("0\n");
continue;
}
while(A)
{
L[i++]=A%a;
A=A/a;
}
for(int j=i-1;j>=0;j--)
{
printf("%d",L[j]);
}
cout<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: