十进制转化为非十进制C++代码
还是先为大家介绍一下原理吧。
假设余数为 r ,十进制数为 n :(拆分为整数 zs ,余数 ys)
对 zs:需要将 zs 除 r 取余数,直到商为 0 停止,将余数倒序排列即可。
对 ys:需要将 ys乘 r 取整数部分,直到小数部分为 0 停止,将所得整数正序排列。
C++代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <memory>
#include <malloc.h>
#include <fstream>
#include <algorithm>
using namespace std;
//10->非10
int main(){
double n,xs;int jz,zs;//xs小数,zs整数
string s="";
cout<<"输入一个十进制数:";
cin>>n;
cout<<"输入你要转化的进制:";
cin>>jz;
//整数部分zs
zs=(int)n;
xs=n-zs;
while(zs!=){
s+=zs%jz>=?(zs%jz-)+'A':(zs%jz)+'';
zs/=jz;
}
reverse(s.begin(),s.end());//将串s反序 if(xs!=){
if((int)n==)
s=s+"0.";
else
s=s+'.';
//小数部分,取8位数即可
for(int i=;i<;i++){
xs*=jz;
s+=int(xs)>=?(int(xs)-)+'A':int(xs)+'';
xs=xs-int(xs);
}
}
cout<<s<<endl;
system("pause");
return ;
}
运行结果如下:

此文章为原创,转载需说明出处。
十进制转化为非十进制C++代码的更多相关文章
- Java查表法实现十进制转化成其它进制
首先了解十进制转化成二级制的原理 156的二进制为: 156 % 2 = 78 …… 0 83 % 2 = 39 …… 0 39 % 2 = 19 …… 1 19 % 2 = 9 …… 1 9 % 2 ...
- 关于java中为什么尽量把受检异常转化为非受检异常
首先理解一下受检异常与非受检异常: 异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机操作中可能遇到的异常,是一种常见的运行错误,只要程序设计的没有问题通常就不会发生.受检异常与程序的上 ...
- S - Making the Grade POJ - 3666 结论 将严格递减转化成非严格的
S - Making the Grade POJ - 3666 这个题目要求把一个给定的序列变成递增或者递减序列的最小代价. 这个是一个dp,对于这个dp的定义我觉得不是很好想,如果第一次碰到的话. ...
- 十进制转化为二进制Java实现
提取2的幂 这个方法用代码实现貌似有点麻烦,需要探测大小,我只实现了整数十进制到二进制的转化 /* * 提取2的幂 */ public static String TenToBin1(int ten) ...
- 十进制转换为K进制 Java 代码
最近在读<计算机科学导论--跨学科方法>(机械工业出版社),习题索引:1.3.21: 编写一个新程序Kary,输入两个命令行参数i和k,并将i转换为基数k的数值表示.假设i是java中的l ...
- bignumber.js是一款用于任意精度十进制和非十进制算术的JavaScript库
安装 npm install bignumber.js 引入 import { BigNumber } from 'bignumber.js' 实例方法 加法:plus 0.1 + 0.2 // 0. ...
- 用JS实现将十进制转化为二进制
- hdu5432Rikka with Array (数位dp+十进制转化为二进制)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- struts2:字段校验和非字段校验代码示例
一.为什么要使用struts2的validate验证框架 :使用struts2的验证框架,能够提高客户端提交的数据的安全性.通过验证,确保保存进数据库的信息是正确的 二.使用struts2的valid ...
随机推荐
- shell编程中星号(asterisk "*")的坑
今天分享一个有关shell编程中由通配符引起的问题. 1. 问题代码 cat test.logs 4567890 * ##*************************************## ...
- 基于springcloud搭建项目-Hystrix篇(五)
1.概述 (1).首先要知道分布式系统面临的问题复杂分布式体系结构中应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败 (2).服务雪崩 多个服务之间相互调用的时候,假设微服务A调用微服 ...
- MySQL/InnoDB中的事务隔离级别
SQL标准中的事务四种隔离级别 隔离级别 脏读(Dirty Read) 不可重复读(NonRepeatable Read) 幻读(Phantom Read) 未提交读(Read uncommitted ...
- win10安装docker 和 splash
参考链接1:https://www.cnblogs.com/321lxl/p/9536616.html 参考链接2:https://blog.csdn.net/qq_18831501/article/ ...
- selenium 爬boss
# 有问题 from selenium import webdriver import time from lxml import etree class LagouSpider(object): d ...
- niginx:duplicate MIME type "text/html" in nginx.conf 错误(转载)
把nginx升级到最新以后,发现用原来的配置启动的时候会提示: duplicate MIME type "text/html" in /usr/local/nginx/conf/n ...
- Python基础篇_实例练习(二)
问题1:假设有同学A,A每周在工作日进步,周末退步,问一年(365天)后A同学是一年前的几倍? 工作日进步由用户输入,周末下降0.01即1% deyup = eval(input()) deyfact ...
- mysql8 修改root密码
Navicat工具里选中mysql数据库 执行: ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密 ...
- Lua Table pairs输出顺序问题 (版本差异 解决数字索引间断并兼容字符串索引)
问题标签: Lua Table 迭代器;Lua Table 输出顺序; Lua Table 顺序输出;Lua Table 数字索引 字符串索引;Lua Table pairs; 问题背景: 使用pai ...
- Mol Cell Proteomics. | Integration and analysis of CPTAC proteomics data in the context of cancer genomics in the cBioPortal (解读人:徐洪凯)
文献名:Integration and analysis of CPTAC proteomics data in the context of cancer genomics in the cBioP ...