上机笔记3.5

*3.5 PAT B1037

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:

输入在 1 行中分别给出 PA,格式为 Galleon.Sickle.Knut,其间用 1 个空格分隔。这里 Galleon 是 [0, 107] 区间内的整数,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
//1.化成统一最小单位
//2.用除法和取余来换算输出
#include<stdio.h>
const int galleon = 17 * 29;
const int sickle = 29;
int main() {
	int a1, b1, c1;
	int a2, b2, c2;
	scanf("%d.%d.%d %d.%d.%d", &a1, &b1, &c1, &a2, &b2, &c2);
	int price = a1 * galleon + b1 * sickle + c1;
	int money = a2 * galleon + b2 * sickle + c2;
	int change = money - price;
	if (change < 0) {
		printf("-");
		change = -change;
	}
	printf("%d.%d.%d", change / galleon, (change % galleon) / sickle, change % sickle);
	return 0;
}

*3.5 PAT A1019

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 a**i as ∑i=0k(aib**i). Here, as usual, 0≤a**i<b for all i and a**k is non-zero. Then N is palindromic if and only if a**i=a**ki 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<N≤109 is the decimal number and 2≤b≤109 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 “a**k a**k−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<stdio.h>
#include<stdbool.h>
bool isPalindromic(int arr[],int n) {
	for (int i = 0; i <= n / 2; i++) {
		if (arr[i] != arr[n - i - 1])
			return false;
	}
	return true;

}

int main() {
	int n, d, num = 0, z[40];
	scanf("%d%d", &n, &d);
	do {
		z[num++] = n % d;
		n /= d;
	} while (n != 0);
	bool flag = isPalindromic(z, num);
	if (flag) {
		printf("Yes\n");
	}
	else {
		printf("No\n");
	}
	for (int i = num-1; i >= 0; i--) {
		printf("%d", z[i]);
		if (i != 0)
			printf(" ");
	}
}

*3.5 PAT A1027

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.

Sample Input:

15 43 71

Sample Output:

#123456
#include<stdio.h>
int main() {
	char r13[13] = { '0','1','2','3','4','5','6','7','8','9','A','B','C' };
	int r, g, b;
	scanf("%d%d%d", &r, &g, &b);
	printf("#");
	printf("%c%c", r13[r / 13], r13[r % 13]);
	printf("%c%c", r13[g / 13], r13[g % 13]);
	printf("%c%c", r13[b / 13], r13[b % 13]);

}

*3.5 PAT A1058

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,107], 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<stdio.h>
int main() {
	int a_g, a_s, a_k;
	int b_g, b_s, b_k;
	int c_g, c_s, c_k;
	scanf("%d.%d.%d %d.%d.%d", &a_g, &a_s, &a_k, &b_g, &b_s, &b_k);
	if (a_k + b_k >= 29) {
		c_k = a_k + b_k - 29;
		a_s++;
	}
	else {
		c_k = a_k + b_k;
	}
	if (a_s + b_s >= 17) {
		c_s = a_s + b_s - 17;
		a_g++;
	}
	else {
		c_s = a_s + b_s;
	}
	c_g = a_g + b_g;
	printf("%d.%d.%d", c_g, c_s, c_k);

}
//算法笔记,用进位的方法
int main() {
	int a[3], b[3], c[3];
	scanf("%d.%d.%d %d.%d.%d", &a[0], &a[1], &a[2], &b[0], &b[1], &b[2]);
	int carry = 0;
	c[2] = (a[2] + b[2]) % 29;
	carry= (a[2] + b[2]) / 29;
	c[1] = (a[1] + b[1] + carry) % 17;
	carry= (a[1] + b[1] + carry) / 17;
	c[0] = a[0] + b[0] + carry;
	printf("%d.%d.%d", c[0], c[1], c[2]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值