【再思考】PATBasic——1037. 在霍格沃茨找零钱(20)

1037. 在霍格沃茨找零钱(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

输入格式:

输入在1行中分别给出P和A,格式为“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
一道很简单的题,被我搞了一个多小时,居然还只得了19分,忧伤啊,最近脑子晕成浆糊了
#include<vector>
#include <sstream>
#include<cmath>
#include<iomanip>
#include<iostream>
#include <ctype.h>
#include <stdlib.h>

using namespace std;


struct NumGSK
{
	int galln;
	int sickle;
	int knut;
};
NumGSK Minus(NumGSK numa, NumGSK numb);
int main()
{
	string a, b;
	cin >> a >> b;
	vector<int> asignls;
	vector<int> bsignls;

	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] == '.')
		{
			asignls.push_back(i);
		}
	}

	for (int i = 0; i < b.length(); i++)
	{
		if (b[i] == '.')
		{
			bsignls.push_back(i);
		}
	}
	NumGSK numa;
	NumGSK numb;
	
	numa.galln = atoi(a.substr(0, asignls[0]).c_str());
	numa.sickle = atoi(a.substr(asignls[0] + 1, asignls[1] - 1).c_str());
	numa.knut = atoi(a.substr(asignls[1] + 1,a.length()-1).c_str());		

	numb.galln = atoi(b.substr(0, bsignls[0]).c_str());
	numb.sickle = atoi(b.substr(bsignls[0] + 1, bsignls[1] - 1).c_str());
	numb.knut = atoi(b.substr(bsignls[1] + 1, b.length() - 1).c_str());

	NumGSK temp = Minus(numa,numb);
	NumGSK numr;
	if (numb.galln > numa.galln)//从最高位开始,判断数的大小,百位相同,则判断十位,十位相同, 判断个位,以此类推
	{
		numr = Minus(numa,numb);
		cout << numr.galln << "." << numr.sickle << "." << numr.knut;
	}
	else if (numb.galln<numa.galln)
	{
		numr = Minus(numb, numa);
		cout << "-" <<numr.galln << "." << numr.sickle << "." << numr.knut;
	}
	else
	{
		if (numb.sickle>numa.sickle)
		{
			numr = Minus(numa, numb);
			cout << numr.galln << "." << numr.sickle << "." << numr.knut;
		}
		else if(numb.sickle<numa.sickle)
		{
			numr = Minus(numb, numa);
			cout << "-" << numr.galln << "." << numr.sickle << "." << numr.knut;
		}
		else
		{
			if (numb.knut>numa.knut)
			{
				numr = Minus(numa, numb);
				cout << numr.galln << "." << numr.sickle << "." << numr.knut;
			}
			else if(numb.knut<numa.knut)
			{
				numr = Minus(numb, numa);
				cout << "-" << numr.galln << "." << numr.sickle << "." << numr.knut;
			}
			else
			{
				cout << 0;
			}
		}
	}	

	return 0;
}

NumGSK Minus(NumGSK numa, NumGSK numb)//numb>numa本函数只计算结果为正数
{
	NumGSK numr;
	if (numb.knut >= numa.knut)
	{
		numr.knut = numb.knut - numa.knut;
		if (numb.sickle >= numa.sickle)
		{
			numr.sickle = numb.sickle - numa.sickle;
			numr.galln = numb.galln - numa.galln;
		}
		else
		{
			numr.sickle = 17 + numb.sickle - numa.sickle;
			numr.galln = numb.galln - 1 - numa.galln;
		}
	}
	else//个位b小于个位a
	{
		numr.knut = numb.knut + 29 - numa.knut;
		if ((numb.sickle - 1) >= numa.sickle)
		{
			numr.sickle = numb.sickle - 1 - numa.sickle;
			numr.galln = numb.galln - numa.galln;
		}
		else//十位b小于十位a
		{
			numr.sickle = numb.sickle - 1 + 17 - numa.sickle;
			numr.galln = numb.galln - 1 - numa.galln;
		}
	}	
	return numr;
}
第二次写的代码竟然只有18分,我也是醉了
#include<vector>
#include <sstream>
#include<cmath>
#include<iomanip>
#include<iostream>
#include <ctype.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;
struct Num
{
	int gal;
	int sik;
	int knt;
};

int main()
{
	string a, b;
	cin >> a >> b;
	Num numa, numb;

	int ca = a.length();
	int cb = b.length();

	vector<int> as;
	vector<int> bs;
	
	for (int i = 0; i < ca; i++)
	{
		if (a[i] == '.')
			as.push_back(i);
	}

	for (int i = 0; i < cb; i++)
	{
		if (b[i] == '.')
			bs.push_back(i);
	}

	
	numa.gal = stoi(a.substr(0, as[0]).c_str());	
	numa.sik = stoi(a.substr(as[0]+1, as[1]- 1).c_str());
	numa.knt = stoi(a.substr(as[1]+1, ca - 1).c_str());
	

	numb.gal = stoi(b.substr(0, bs[0]).c_str());	
	numb.sik = stoi(b.substr(bs[0] + 1, bs[1] - 1).c_str());	
	numb.knt = stoi(b.substr(bs[1] + 1, cb - 1).c_str());


	int g = numb.gal - numa.gal;
	int s = numb.sik - numa.sik;
	int k = numb.knt - numa.knt;

	if (k < 0)
	{
		s--;
		k += 27;
	}
	if (s < 0)
	{
		--g;
		s += 17;
	}
	if (g < 0)
	{
		g = numa.gal - numb.gal;
		s = numa.sik - numb.sik;
		k = numa.knt - numb.knt;
		if (k < 0)
		{
			--s;
			k += 29;
		}
		if (s < 0)
		{
			--g;
			s += 17;
		}
		g = -g;		
	}
	cout << g << "." << s << "." << k;	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值