例25    确定进制

问题描述

6*9 = 42 对于十进制来说是错误的,但是对于13进制来说是正确的。即

6(13)* 9(13)= 42(13),因为,在十三进制中,42 = 4 * 13 + 2 = 54(10)。

编写一个程序,输入三个整数p、q和r,然后确定一个进制B(2<=B<=16),使得在该进制下 p * q = r。如果 B有很多选择,输出最小的一个。例如,p = 11,q = 11,r = 121。则有 11(3) * 11(3)= 121(3),还有 11(10)* 11(10)= 121(10)。这种情况下,输出3。如果没有合适的进制,则输出0。

输入格式

三个整数p、q和r。

输出格式

所确定的进制B。如果没有合适的进制,则输出0。

输入样例

6 9 42

输出样例

13

(1)编程思路。

选择一个进制B,按照该进制将被乘数p、乘数q、乘积r分别转换成十进制数pb、qb和rb。然后判断等式pb*qb==rb是否成立。使得等式成立的最小B就是所求的结果。

设n位B进制数num=(an-1an-2……a1a0),将其按权值展开后求和就可得到对应的十进制数ret。

由上式可以看出,B进制数num转换为十进制数ret可以写成一个循环。方法是:另ret初始值为0,从高位到低位循环分离出num的各位数字digit,执行        ret=ret*b+digit,循环结束就可得B进制数num对应的十进制数ret。

编写函数int b2ten(int num,int b)完成b进制数num转换为十进制数。

由于转换时需要从高位向低位分离数字,而用循环

while (num!=0)

{

digit = num%10;

num = num/10;

}

能方便地完成从低位向高位分离出num的各位数字。因此,可采用一个数组digit[]来保存从低位向高位分离出的各位数字,同时num中数字的位数保存到变量cnt中。

(2)源程序。

#include <stdio.h>

int b2ten(int num,int b);

int main()

{

int b,p,r,q;

int pb,qb,rb;    // 用来存储转换为十进制后的结果

scanf("%d%d%d",&p,&q,&r);

for(b=2;b<=16;b++)

{

pb=b2ten(p,b);

qb=b2ten(q,b);

rb=b2ten(r,b);

if(pb==-1 || qb==-1 || rb==-1) continue;

if (pb*qb==rb)

{

printf("%d\n",b);

break;

}

}

if(b==17)

printf("0\n");

return 0;

}

int b2ten(int num,int b)

{

int ret=0,digit[10];

int cnt=0;

while (num!=0)

{

digit[cnt++]=num%10;

num=num/10;

}

cnt--;

while (cnt>=0)

{

if (digit[cnt]>=b) return -1;  // 数字超过B进制的数码范围

ret=ret*b+digit[cnt];

cnt--;

}

return ret;

}

习题25

25-1  Faulty Odometer

本题选自北大POJ题库(http://poj.org/problem?id=2719)。

Description

You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from the digit 3 to the digit 5, always skipping over the digit 4. This defect shows up in all positions (the one's, the ten's, the hundred's, etc.). For example, if the odometer displays 15339 and the car travels one mile, odometer reading changes to 15350 (instead of 15340).

Input

Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 4.

Output

Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.

Sample Input

13

15

2003

2005

239

250

1399

1500

999999

0

Sample Output

13: 12

15: 13

2003: 1461

2005: 1462

239: 197

250: 198

1399: 1052

1500: 1053

999999: 531440

(1)编程思路。

本题的题意是:有一个里程表,表盘上的数字4坏了,因此所有的数字4无法显示,3之后显示5,39之后显示50,…。先给出里程表上显示的数字,求实际的里程应为多少?

由于里程表上无数字4,因此可以将里程表上的数看成是一个9进制数,有0,1,2,3,5,6,7,8,9共9个数码,规则逢九进一。因此本题实质是将一个9进制数转换为一个十进制数。

(2)源程序。

#include <stdio.h>

int main()

{

char odometer[10];

int actual,i,d;

while(scanf("%s",odometer) && odometer[0]!='0')

{

actual=0;

for (i=0;odometer[i]!='\0';i++)

{

d=odometer[i]-'0';

if (d>3) d--;

actual=actual*9+d;

}

printf("%s: %d\n",odometer,actual);

}

return 0;

}

25-2  Skew Binary

本题选自北大POJ题库(http://poj.org/problem?id=1565)。

Description

When a number is expressed in decimal, the kth digit represents a multiple of 10k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example,

81307(10) = 8 * 10^4 + 1 * 10 ^3 + 3 * 10^2 + 0 * 10^1 + 7 * 10^0

= 80000 + 1000 + 300 + 0 + 7

= 81307.

When a number is expressed in binary, the kth digit represents a multiple of 2^k . For example,

10011(2) = 1 * 2^4 + 0 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0

= 16 + 0 + 0 + 2 + 1

= 19.

In skew binary, the kth digit represents a multiple of 2^(k+1)-1. The only possible digits are 0 and 1, except that the least-significant nonzero digit can be a 2. For example,

10120(skew) = 1 * (2^5-1) + 0 * (2^4-1) + 1 * (2^3-1) + 2 * (2^2-1) + 0 * (2^1-1)

= 31 + 0 + 7 + 6 + 0

= 44.

The first 10 numbers in skew binary are 0, 1, 2, 10, 11, 12, 20, 100, 101, and 102. (Skew binary is useful in some applications because it is possible to add 1 with at most one carry. However, this has nothing to do with the current problem.)

Input

The input contains one or more lines, each of which contains an integer n. If n = 0 it signals the end of the input, and otherwise n is a nonnegative integer in skew binary.

Output

For each number, output the decimal equivalent. The decimal value of n will be at most 2^31-1 = 2147483647.

Sample Input

10120

200000000000000000000000000000

10

1000000000000000000000000000000

11

100

11111000001110000101101102000

0

Sample Output

44

2147483646

3

2147483647

4

7

1041110737

(1)编程思路。

按题目描述中给出的展开式进行展开计算即可。

(2)源程序。

#include <stdio.h>

#include <string.h>

int main()

{

char bin[33];

int i,value,p;

while (scanf("%s",bin) && strcmp(bin,"0")!=0)

{

p=1; value=0;

for (i=strlen(bin)-1;i>=0;i--)

{

p=p*2;

value+=(bin[i]-'0')*(p-1);

}

printf("%d\n",value);

}

return 0;

}

25-3  数列

本题选自洛谷题库 (https://www.luogu.org/problem/P1062)。

题目描述

给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3,时,这个序列是:1,3,4,9,10,12,13,…

(该序列实际上就是:3^0,3^1,3^0+3^1,3^2,3^0+3^2,3^1+3^2,3^0+3^1+3^2,…)

请你求出这个序列的第N项的值(用10进制数表示)。

例如,对于k=3,N=100,正确答案应该是981。

输入格式

2个正整数,用一个空格隔开:

k N(k、N的含义与上述的问题描述一致,且3≤k≤15,10≤N≤1000)。

输出格式

1个正整数。

输入样例

3 100

输出样例

981

(1)编程思路。

先分析样例

k=3时,数列为:1,3,4,9,10,12,13,…

转换成三进制就是:1,10,11,100,101,110,111,…

看起来像是二进制,转化成十进制就是:1,2,3,4,5,6,7,…

显然,第n项就是n。

编写一个程序,把上面的过程逆回去,即先把n转换成二进制,再把它当成K进制,重新转换为十进制,就可以得到结果。

(2)源程序1。

#include <stdio.h>

int main()

{

int a[11]={0},k,n,cnt,i;

scanf("%d%d",&k,&n);

cnt=0;

while (n!=0)

{

a[cnt++]=n%2;

n/=2;

}

long long s=0;

for (i=cnt-1;i>=0;i--)

s=s*k+a[i];

printf("%lld\n",s);

return 0;

}

(3)源程序2。

#include <stdio.h>

int main()

{

int k,n;

scanf("%d%d",&k,&n);

long long s=0,p=1;

while (n!=0)

{

s=s+(n%2)*p;

p*=k;

n/=2;

}

printf("%lld\n",s);

return 0;

}

C语言程序设计100例之(25):确定进制的更多相关文章

  1. 黑马程序员——经典C语言程序设计100例

    1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...

  2. C语言程序设计100例之(21):折半查找

    例21  折半查找 问题描述 顺序查找是一种最简单和最基本的检索方法.其基本思想是:从检索表的一端(如表中第一个记录或最后一个记录)开始,逐个进行记录的关键字和给定值的比较.若某个记录的关键字和给定值 ...

  3. C语言程序设计100例之(24):数制转换

    例24   数制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入格式 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用 ...

  4. C语言程序设计100例之(22):插入排序

    例22  插入排序 问题描述 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素或记录的任意序列,重新排列成一个以关键字递增(或递减)排列的有序序列. 排序的方法有很多,简单插入排序就是一 ...

  5. C语言程序设计100例之(4):水仙花数

    例4    水仙花数 题目描述 一个三位整数(100-999),若各位数的立方和等于该数自身,则称其为“水仙花数”(如:153=13+53+33),找出所有的这种数. 输入格式 没有输入 输出格式 若 ...

  6. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  7. C语言程序设计100例之(15):除法算式

    例15   除法算式 问题描述 输入正整数n(2≤n≤68),按从小到大输出所有形如abcde/fghi=n的表达式.其中a~i为1~9的一个排列. 输入格式 每行为一个正整数n (n <= 1 ...

  8. C语言程序设计100例之(12):Eratosthenes筛法求质数

    例12   Eratosthenes筛法求质数 问题描述 Eratosthenes筛法的基本思想是:把某范围内的自然数从小到大依次排列好.宣布1不是质数,把它去掉:然后从余下的数中取出最小的数,宣布它 ...

  9. C语言程序设计100例之(9):生理周期

    例9    生理周期 问题描述 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为 23 天.28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如 ...

随机推荐

  1. python初识-环境搭建,变量,条件,循环语句

    1.python环境搭建: (1)安装Anaconda ,可选择非C盘安装: 注意:都勾选: (2)安装Pycharm 默认安装即可,安装过程同样都勾选: (3)破解Pycharm https://w ...

  2. apache虚拟主机配置的3种方式

    Apache的虚拟主机功能 (Virtual Host) 是可以让一台服务器基于IP.主机名或端口号实现提供多个网站服务的技术. 下面我介绍一下这3种方式具体的配置流程: 一.基于ip地址 首先我们需 ...

  3. oracle创建jobs定时任务报错:ora-01008:not all variables bound

    原脚本(直接从jobs拖出生成的DDL): begin  sys.dbms_job.submit(job => :job,                      what => 'xx ...

  4. MYSQL“错误代码#1045 Access denied for user 'root'@'********8' (using password:YES)”

    用IP远程连接数据库时报这个错误,我查看了下数据库是否开启了远程连接,已经开了,服务也启动着,网上的方法都是重置密码修改权限之类的,我发现都没用,我看了一下数据库所在的电脑,IP地址变了,然后真相了.

  5. 18.DjangoRestFramework学习一之restful规范、APIview、解析器组件、Postman等

    一 预备知识 预备知识:django的CBV和FBV CBV(class based view):多用,简单回顾一下 FBV(function based view): CBV模式的简单操作:来个登陆 ...

  6. 源码分析 RocketMQ DLedger(多副本) 之日志复制(传播)

    目录 1.DLedgerEntryPusher 1.1 核心类图 1.2 构造方法 1.3 startup 2.EntryDispatcher 详解 2.1 核心类图 2.2 Push 请求类型 2. ...

  7. Day 02 作业

    作业 一 什么是编程? 基于编程语言的语法格式将自己脑中里想让计算机做的事,写到文件中. 二 简述计算机五大组成 控制器,运算器,存储器,输入设备,输出设备 三 操作系统有什么用? 操作系统直接与硬件 ...

  8. rails 构建 API

    我是来鼓吹使用 Rails 写 API 的. 原文在此: https://labs.kollegorna.se/blog/2015/04/build-an-api-now/ 原文有一个很大的缺陷就是读 ...

  9. wxxcx_learn异常处理

    属于基础框架,分级别 捕获异常,处理异常(记录日志,修复异常,返回客户端错误),抛出异常 全局异常处理(AOP)对错误同一格式化 try{ $banner = BannerModel::getBann ...

  10. docker入门-镜像管理命令篇

    一.下载.上传镜像   1:下载安装centos镜像 语法:docker [参数][镜像名称] [root@host1 ~]# docker pull centos Using default tag ...