PAT甲级1058,1059

49 篇文章 0 订阅
47 篇文章 0 订阅

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<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
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<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值