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
下面这个没有测试通过,暂时没有看出是哪里出现了问题,之后再仔细检查吧
方案1:
/**
作者:一叶扁舟
时间:17:32 2017/6/25
思路:
*/
#include <stdlib.h>
#include <string.h>
#include<iostream>
using namespace std;
int main(){
int pG, pS, pK;//要付的钱
int aG, aS, aK;//手里有的钱
int rG, rS, rK;//还剩余多少钱或者还差多少钱
scanf("%d.%d.%d", &pG, &pS, &pK);
scanf("%d.%d.%d", &aG, &aS, &aK);
int flag = 1;//标识手里有钱够付
//将数字转成字符串
char strP[10];
char strA[10];
sprintf(strP, "%02d%02d%02d", pG, pS, pK );
sprintf(strA, "%02d%02d%02d", aG, aS, aK);
if (strcmp(strP, strA) > 0){//说明不够支付(手头的钱不够)
flag = 0;
//交换钱
swap(pG, aG);
swap(pS, aS);
swap(pK, aK);
}
if (pK >aK){
rK = aK + 29 - pK;
aS = aS - 1;
}
else{
rK = aK - pK;
}
if (pS > aS){
rS = aS + 17 - pS;
aG = aG - 1;
}
else{
rS = aS - pS;
}
rG = aG - pG;
if (flag == 0){
printf("-");
}
//输出结果
printf("%d.%d.%d", rG, rS, rK);
system("pause");
return 0;
}
测试结果:
有一项没有测试通过
方案2:(完全测试通过了)
/**
作者:一叶扁舟
时间:16:31 2017/6/26
思路:就是将所有单位的货币钱转换成一种货币单位最小的值
*/
#include <stdlib.h>
#include <string.h>
#include<iostream>
using namespace std;
int main(){
int pG, pS, pK;//要付的钱
int aG, aS, aK;//手里有的钱
int rG, rS, rK;//还剩余多少钱或者还差多少钱
int allPK;
int allAK;
int flag = 1;//标识手里有钱够付
int rest;//最后剩下多少钱或还差多少钱
scanf("%d.%d.%d", &pG, &pS, &pK);
scanf("%d.%d.%d", &aG, &aS, &aK);
//将钱都转成最小单位
allPK = pG * 17 * 29 + pS * 29 + pK;
allAK = aG * 17 * 29 + aS * 29 + aK;
if (allAK < allPK){ //不够付账
flag = 0;
rest = allPK - allAK;
}
else{
rest = allAK - allPK;
}
rG = rest / (17 * 29);
int temp = rest % (17 * 29);
rS = temp / 29;
rK = temp % 29;
if (flag == 0){
printf("-");
}
//输出结果
printf("%d.%d.%d", rG, rS, rK);
system("pause");
return 0;
}
)