算法笔记 上机训练实战指南 第3章 入门篇(1) --入门模拟 3.5进制转换 学习笔记
PAT B1037 在霍格沃茨找零钱 (20分)
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。
输入格式:
输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut
,其间用 1 个空格分隔。这里 Galleon
是 [0, 1] 区间内的整数,Sickle
是 [0, 17) 区间内的整数,Knut
是 [0, 29) 区间内的整数。
输出格式:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
输入样例 1:
10.16.27 14.1.28
输出样例 1:
3.2.1
输入样例 2:
14.1.28 10.16.27
输出样例 2:
-3.2.1
#include<cstdio> const int Galleon = 17*29; const int Sickle = 29; int main(){ int p1,p2,p3; int a1,a2,a3; scanf("%d.%d.%d %d.%d.%d",&p1,&p2,&p3,&a1,&a2,&a3); int realp = p1 * Galleon + p2 * Sickle + p3; int reala = a1 * Galleon + a2 * Sickle + a3; int ans = reala - realp; if(ans<0){ printf("-"); ans = -ans; } printf("%d.%d.%d",ans/Galleon,ans%Galleon/Sickle,ans%Sickle); return 0; }
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits ai as (. Here, as usual, 0 for all i and ak is non-zero. Then N is palindromic if and only if ai=ak−i for all i. Zero is written 0 in any base and is also palindromic by definition.
Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and b, where 0 is the decimal number and 2 is the base. The numbers are separated by a space.
Output Specification:
For each test case, first print in one line Yes
if N is a palindromic number in base b, or No
if not. Then in the next line, print N as the number in base b in the form "ak ak−1 ... a0". Notice that there must be no extra space at the end of output.
Sample Input 1:
27 2
Sample Output 1:
Yes
1 1 0 1 1
Sample Input 2:
121 5
Sample Output 2:
No
4 4 1
#include<cstdio> bool Judge(int z[],int num){ for(int i=0;i<=num/2;i++){ if(z[i]!=z[num-i-1]){ return false; } } return true; } int main(){ int n,b; scanf("%d %d",&n,&b); int z[40],num=0; while(n!=0){ z[num++] = n%b; n = n/b; } if(Judge(z,num)==true) printf("Yes\n"); else printf("No\n"); for(int i=num-1;i>=0;i--){ printf("%d",z[i]); if(i != 0) printf(" "); } return 0; }
If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of Galleon.Sickle.Knut
(Galleon
is an integer in [0], Sickle
is an integer in [0, 17), and Knut
is an integer in [0, 29)).
Input Specification:
Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.
Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input.
Sample Input:
3.2.1 10.16.27
Sample Output:
14.1.28
#include<cstdio> const int Galleon = 17*29; const int Sickle = 29; int main(){ long long a1,a2,a3; long long b1,b2,b3; scanf("%lld.%lld.%lld %lld.%lld.%lld",&a1,&a2,&a3,&b1,&b2,&b3); long long reala = a1 * Galleon + a2 * Sickle + a3; long long realb = b1 * Galleon + b2 * Sickle + b3; long long sum = reala + realb; printf("%lld.%lld.%lld",sum/Galleon,sum%Galleon/Sickle,sum%Sickle); return 0; }