PAT(甲级)渡劫(十八)-A+B in Hogwarts(20)

本文提供了一段解决PAT甲级渡劫题目的代码实现,通过解析两个由点分隔的数字序列,并进行特定的数学运算,最终输出计算结果。代码使用C++编写,涉及字符串操作、数组处理及数学计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT(甲级)渡劫(十八)-A+B in Hogwarts(20)

代码如下:
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>

using namespace std;

int main(){
    //freopen("in.txt","r",stdin);
    char s1[30],s2[30];
    int a[5],b[5];
    scanf("%s%s",s1,s2);
    char *result = NULL;
    result = strtok(s1,".");
    int cnt1 = 0;
    while(result != NULL){
        a[cnt1++] = atoi(result);
        result = strtok(NULL,".");
    }

    result = NULL;
    result = strtok(s2,".");
    int cnt2 = 0;
    while(result != NULL){
        b[cnt2++] = atoi(result);
        result = strtok(NULL,".");
    }

    int ans,tmp;
    int c[5],cnt3 = 0;
    for(int i = 2 ; i >= 0 ; i--){
        if(i == 2){
            ans = (a[i] + b[i])%29;
            tmp = (a[i]+b[i])/29;
            c[cnt3++] = ans;
        }else if(i == 1){
            ans = (a[i] + b[i] + tmp)%17;
            tmp = (a[i] + b[i] + tmp)/17;
            c[cnt3++] = ans;
        }else{
            ans = (a[i] + b[i] + tmp);
            c[cnt3++] = ans;
        }
    }
    cout<<c[2]<<"."<<c[1]<<"."<<c[0]<<endl;
    return 0;
}
运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值