PAT甲级1058,1059

1058 A+B in Hogwarts (20 point(s))

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,10​7​​], Sickleis 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

题目大意:自定义一个结构的运算,实现这个运算。a.b.c这样一个结构,第一个数范围是0到10的七次方,b是17,c是29

进位搞一下。

解题思路:水是很水,但我还是WA了一发,不知道为什么,第一个数范围明明是有限制的,为什么不考虑溢出的,我现在也不是很懂为什么错。。反正不考虑溢出就对了。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
	int a, b, c, a1, b1, c1;
	scanf("%d.%d.%d %d.%d.%d", &a, &b, &c, &a1, &b1, &c1);
	int resa, resb, resc;
	resc = (c + c1) % 29;
	resb = (b + b1 + (c + c1) / 29) % 17;
	resa = (a + a1 + (b + b1 + (c + c1) / 29) / 17);
	printf("%d.%d.%d\n", resa, resb, resc);
	return 0;
}

1059 Prime Factors (25 point(s))

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

题目大意:实现一下质因数分解。

解题思路:其实一开始看到这题目脑子里出现的就是打表了。。毕竟运行时间是在你进行输入后开始计算的是吧。后来又想了一下。。long int 能分解的质数最大也就10位吧,直接一个个判断上去不就行了,还搞什么素数判定,直接从2开始分解,不可能存在整除不是质因子的结果呀。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
	long int n;
	scanf("%ld", &n);
	if (n == 1)printf("1=1\n");
	else {
		printf("%ld=", n);
		bool flag = true;
		long int curcount = 0;//次数
		long int curprime = 2;//素数
		while (n > 1) {
			while (n%curprime == 0) {
				curcount += 1;
				n /= curprime;
			}
			if (curcount >= 1) {
				if (flag) {
					flag = false;
				}
				else printf("*");
				printf("%ld", curprime);
				if (curcount >= 2)
					printf("^%ld", curcount);
			}
			curprime++;
			curcount = 0;
		}
		printf("\n");
	}
	
	return 0;
}

 

你可能感兴趣的:(C++,PAT,C)