例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. Mysql多表关系

    mysql多表关系 多表关系是关系型数据库特有的 三种关系 一对一关系 一对多关系 多对多关系 总结 一对一 例子:用户和用户信息 外键设置在用户上,外键字段唯一非空 添加 无级联:先增加被关联表记录 ...

  2. 腾讯云docker加速

    腾讯云的docker加速: 路径及配置如下: root@VM---ubuntu:~# cat /etc/docker/daemon.json { "registry-mirrors" ...

  3. element table 先显示暂无数据 之后再加载数据 问题

    项目中的表格请求数据时,进去页面,先出现 ''暂无数据'' 字样闪现一下之后在进行加载数据,用户体验十分不好 解决办法: <template> <el-table :data=&qu ...

  4. 有趣的css3实战案例剖析—(背景动态渐变)

    对于css3的学习,更多的是在于对新特性和基础理论的熟悉,这篇文章通过一个案例带领大家了解css3里一些理论知识,也将一些技巧加以总结,从而提高大家的开发效率: 本次案例为(背景颜色渐变),运用css ...

  5. 【原创】003 | 搭上基于SpringBoot事务思想实战专车

    前言 如果这是你第二次看到师长,说明你在觊觎我的美色! 点赞+关注再看,养成习惯 没别的意思,就是需要你的窥屏^_^ 专车介绍 该趟专车是开往基于Spring Boot事务思想实战的专车,在上一篇 搭 ...

  6. Redis 中的数据库

    前面我们花了很多的时间介绍了 redis 中基本的数据结构,及其内部的实现情况,这些都是非常基础的东西,可能不经意间你就会用到他们,希望你花点时间了解一下. 接下来,我们将走近 redis 数据库,学 ...

  7. luogu P4677 山区建小学 |dp

    题目描述 政府在某山区修建了一条道路,恰好穿越总共nnn个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为did_idi​(为正整数),其中,0& ...

  8. 微信SDK接入报undefined symbol错误

    按照官方SDK接入 编译后报如下错误 是因为没有link libc++库导致的.

  9. CoderForces Round60-(1117A,1117B,1117C题解)

    A. Best Subsegment time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. 【Web技术】337- 秒懂 Web 缓存

    点击上方"前端自习课"关注,学习起来~ 最近把前端缓存重新整理了一下,从整体的层面上把前端所有能用的缓存方案梳理了一遍.同时,对于http缓存,使用了表格的方案,使得原先晦涩难记的 ...