PAT甲级刷题记录——1058 A+B in Hogwarts (20分)

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(Galleonis an integer in [0,10​7​​ ], Sickleis an integer in [0, 17), and Knutis 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

思路

这题和【1037 在霍格沃茨找零钱 (20分)】很像哈,只不过乙级的那道题实现的是减法,这题实现的是加法。

计算方式还是一样的,从低位开始看起,如果某一位相加之后,它们的和大于等于它的进制数,那就说明要进位(这里要注意的是,除了进到最高位可以直接在结果的最高位+=1,其余的进位一定要在A或B对应的数位上+=1。举个例子,假如以时分秒为例,原先是58分+1分,但是呢,两个秒相加又进了一位,因此,此时的分钟数相加应该是58分+1分+1分,也要进位,如果把这个1分钟算在结果里的话,58分+1分依然不进位,最后结果累加起来就会出现60分的情况,这应该是测试点1所考察的东西)。

最后累加完了之后直接按题目要求输出就好啦。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int Galleon_A, Sickle_A, Knut_A, Galleon_B, Sickle_B, Knut_B;
    string A, B;
    cin>>A>>B;
    int pos1, pos2;
    pos1 = A.find(".");
    Galleon_A = stoi(A.substr(0, pos1));
    pos2 = A.find(".", pos1+1);
    Sickle_A = stoi(A.substr(pos1+1, pos2-pos1-1));
    Knut_A = stoi(A.substr(pos2+1));
    pos1 = B.find(".");
    Galleon_B = stoi(B.substr(0, pos1));
    pos2 = B.find(".", pos1+1);
    Sickle_B = stoi(B.substr(pos1+1, pos2-pos1-1));
    Knut_B = stoi(B.substr(pos2+1));
    int Galleon_R = 0, Sickle_R = 0, Knut_R = 0;
    if(Knut_A+Knut_B>=29){
        Knut_R += Knut_A+Knut_B-29;
        Sickle_A += 1;
        //这里不能写Sickle_R += 1,否则会出现Sickle加上Knut的进位之后需要再进一位但是没进位的情况,即:Sickle_R = 17
    }
    else Knut_R = Knut_A+Knut_B;
    if(Sickle_A+Sickle_B>=17){
        Sickle_R += Sickle_A+Sickle_B-17;
        Galleon_R += 1;
    }
    else Sickle_R += Sickle_A+Sickle_B;
    Galleon_R += Galleon_A+Galleon_B;
    cout<<Galleon_R<<"."<<Sickle_R<<"."<<Knut_R;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个测试场景我可以用 Python 的 selenium 库进行自动化测试。以下是大致的测试代码: ```python from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # 登录账户 driver = webdriver.Chrome() driver.get("https://thingsboard.hogwarts.ceshiren.com/") elem = driver.find_element_by_name("username") elem.send_keys("1945075385@qq.com") elem = driver.find_element_by_name("password") elem.send_keys("hogwarts") elem.send_keys(Keys.RETURN) time.sleep(5) # 新增设备 driver.get("https://thingsboard.hogwarts.ceshiren.com/dashboard/3f7d7b20-7b43-11eb-bd5a-3f8ae6b9bcbc") elem = driver.find_element_by_class_name("add-button") elem.click() time.sleep(1) elem = driver.find_element_by_class_name("tb-create-entity-type-select") elem.click() time.sleep(1) elem = driver.find_element_by_xpath("//div[@class='tb-create-entity-type-select']/div[2]/div[1]/div[1]") elem.click() time.sleep(1) elem = driver.find_element_by_class_name("tb-create-entity-form") elem = elem.find_element_by_xpath("//input[@placeholder='Device name']") elem.send_keys("Test Device") elem = driver.find_element_by_xpath("//input[@placeholder='Serial number']") elem.send_keys("123456") elem = driver.find_element_by_class_name("tb-create-entity-footer") elem = elem.find_element_by_xpath("//button[@type='submit']") elem.click() time.sleep(5) # 修改设备 elem = driver.find_element_by_xpath("//span[text()='Test Device']") elem.click() time.sleep(1) elem = driver.find_element_by_xpath("//input[@name='name']") elem.clear() elem.send_keys("New Test Device") elem = driver.find_element_by_xpath("//input[@name='type']") elem.clear() elem.send_keys("New Test Type") elem = driver.find_element_by_xpath("//input[@name='key']") elem.clear() elem.send_keys("New Test Key") elem = driver.find_element_by_xpath("//button[@type='submit']") elem.click() time.sleep(5) # 删除设备 elem = driver.find_element_by_xpath("//span[text()='New Test Device']") elem.click() time.sleep(1) elem = driver.find_element_by_class_name("tb-delete-entity-button") elem.click() time.sleep(1) elem = driver.find_element_by_xpath("//button[text()='Delete']") elem.click() time.sleep(5) # 关闭浏览器 driver.close() ``` 需要注意的是,我使用的是 Chrome 浏览器,所以需要下载对应版本的 chromedriver 并放在系统环境变量中。另外,测试过程中需要等待页面加载,所以我使用了 time.sleep() 函数来进行等待。如果网速较快,可以适当减少等待时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值