C语言程序设计100例之(25):确定进制
例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):确定进制的更多相关文章
- 黑马程序员——经典C语言程序设计100例
1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...
- C语言程序设计100例之(21):折半查找
例21 折半查找 问题描述 顺序查找是一种最简单和最基本的检索方法.其基本思想是:从检索表的一端(如表中第一个记录或最后一个记录)开始,逐个进行记录的关键字和给定值的比较.若某个记录的关键字和给定值 ...
- C语言程序设计100例之(24):数制转换
例24 数制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入格式 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用 ...
- C语言程序设计100例之(22):插入排序
例22 插入排序 问题描述 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素或记录的任意序列,重新排列成一个以关键字递增(或递减)排列的有序序列. 排序的方法有很多,简单插入排序就是一 ...
- C语言程序设计100例之(4):水仙花数
例4 水仙花数 题目描述 一个三位整数(100-999),若各位数的立方和等于该数自身,则称其为“水仙花数”(如:153=13+53+33),找出所有的这种数. 输入格式 没有输入 输出格式 若 ...
- C语言程序设计100例之(14):丑数
例14 丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...
- C语言程序设计100例之(15):除法算式
例15 除法算式 问题描述 输入正整数n(2≤n≤68),按从小到大输出所有形如abcde/fghi=n的表达式.其中a~i为1~9的一个排列. 输入格式 每行为一个正整数n (n <= 1 ...
- C语言程序设计100例之(12):Eratosthenes筛法求质数
例12 Eratosthenes筛法求质数 问题描述 Eratosthenes筛法的基本思想是:把某范围内的自然数从小到大依次排列好.宣布1不是质数,把它去掉:然后从余下的数中取出最小的数,宣布它 ...
- C语言程序设计100例之(9):生理周期
例9 生理周期 问题描述 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为 23 天.28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如 ...
随机推荐
- synchronized被这么问,谁能受得了
synchronized是面试中经常会被问到的知识点,相关的问题点也很多,问题答案涉及的知识点也很多,有经验的面试官就会顺着你的答案不断追问一下,下面的对话场景就是相关面试题的连环炮. 面试官:说一下 ...
- VLAN实验3(Hybrid接口的应用)
本实验基于<HCNA网络技术实验指南> 本实验使用eNSP软件 原理概述: Hybrid接口既可以连接普通终端的接入链路又可以连接交换机间的干道链路,它允 许多个VLAN的帧通过,并可以在 ...
- Solr搜索引擎【索引管理】
一.索引存储 当文档提交到索引之后,directory目录组件会将它们写入到持久化存储器.Solr的目录组件具有以下重要特点: 1.隐藏持久存储的读写细节,例如,将文档写入到磁盘或通过JDBC在数据库 ...
- 链式栈-C语言实现
相对于顺序栈的空间有限,链式栈的操作则更加灵活 #include<stdio.h> #include<malloc.h> typedef int SElemType; //元素 ...
- 学习WebFlux时常见的问题
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾一下上篇我对WebFlux的入门,如果没读过 ...
- 【SpringSecurityOAuth2】源码分析@EnableOAuth2Sso在Spring Security OAuth2 SSO单点登录场景下的作用
目录 一.从Spring Security OAuth2官方文档了解@EnableOAuth2Sso作用 二.源码分析@EnableOAuth2Sso作用 @EnableOAuth2Client OA ...
- Vue-Router中History模式【华为云分享】
[摘要] vue-router的history模式的服务端支持 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端> ...
- 洛谷 3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题解
本蒟蒻又来发题解了, 一道较水的模拟题. 题意不过多解释, 思路如下: 在最开始的时候求出每头牛在t秒的位置(最终位置 然后,如果后一头牛追上了前一头牛,那就无视它, 把它们看成一个整体. else ...
- luogu P3152 正整数序列
题目描述 kkk制造了一个序列,这个序列里的数全是由正整数构成的.你别认为她的数列很神奇--其实就是1, 2, -, n而已.当然,n是给定的.kkk的同学lzn认为0是一个好数字(看上去很饱满有木有 ...
- 详解OS X和iOS图像处理框架Core Image
转自:http://www.csdn.net/article/2015-02-13/2823961-core-image 摘要:本 文结合实例详解了OS X和iOS图像处理框架Core Image的使 ...