PTA1001 A+B Format

本文介绍了一个简单的编程问题——1001A+B,该问题要求计算两个整数之和,并以标准格式输出结果。标准格式是指用逗号分隔每三位数字(除非总位数少于四位)。文章提供了完整的C++代码实现,涵盖了输入处理、计算和格式化输出等关键步骤。

1001 A+B Format (20)(20 分)

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input

-1000000 9

Sample Output

-999,991

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;

int main()
{
    int a,b;
    int i,num,t;
    char s[20];
    cin>>a>>b;
    a+=b;
    b=a;
    a=abs(a);
    num=0;
    t=0;
    while(a)
    {
        s[num++]=a%10+'0';
        t++;
        a=a/10;
        if(t==3&&a!=0)//注意刚好是三个数的情况
        {
            t=0;
            s[num++]=',';
        }
    }
    if(b<0)
        s[num++]='-';
    if(b==0) cout<<"0";//注意结果为零的情况
    for(i=num-1;i>=0;i--)
    {
        cout<<s[i];
    }
return 0;
} 
### PTA 多项式 A 除以 B 的算法实现 对于多项式 \(A\) 和 \(B\),目标是找到商 \(Q\) 和余数 \(R\),使得 \(A = B \times Q + R\) 并且 \(R\) 的阶数严格小于 \(B\) 的阶数。以下是具体的算法描述和 Python 实现。 #### 算法概述 1. 初始化商 \(Q\) 和余数 \(R\) 均为空。 2. 将被除数 \(A\) 赋给当前处理的多项式 `current`。 3. 当 `current` 不为零且其最高次幂不低于除数 \(B\) 的最高次幂时: - 计算当前应减去的部分:令该项等于 `current` 中最高次幂项与 \(B\) 中最高次幂项的比例关系形成的单项式; - 更新 `current` 减去上述计算得到的新项乘上整个 \(B\) 后的结果; - 把新产生的项加入到 \(Q\) 中作为新的部分。 4. 终止循环后剩下的 `current` 即为最终的余数 \(R\);而累加起来的各项构成完整的商 \(Q\)。 此过程类似于手工做长除法的过程,在计算机中通过不断减少高次项来逼近解。 #### Python 实现代码 ```python def poly_divide(A, B): from collections import defaultdict def parse_poly(poly_str): terms = {} parts = poly_str.split() n = int(parts[0]) for i in range(1, len(parts), 2): exp = int(parts[i]) coef = float(parts[i+1]) terms[exp] = coef return terms def format_output(polynomial): if not polynomial: return "0 0 0.0" items = sorted([(k, v) for k, v in polynomial.items() if abs(v)>1e-6], reverse=True) result = [] for item in items: result.extend([str(item[0]), f"{item[1]:.1f}"]) return ' '.join(result) # 解析输入字符串成字典形式 {指数:系数} a_terms = parse_poly(A)[^4] b_terms = parse_poly(B)[^4] q = {} # 商初始化为空列表 r = dict(a_terms) # 初始设置r=a max_b_exp = max(b_terms.keys()) if b_terms else None while r and (max(r.keys()) >= max_b_exp): # 只要还有剩余项可处理并且次数大于等于b的最大次数 lead_r_coef = r[max(r.keys())] lead_q_term = (max(r.keys()), lead_r_coef / b_terms[max_b_exp]) # 新增q的一项 q[lead_q_term[0]] = round(lead_q_term[1], 1)[^5] temp_product = {key + lead_q_term[0]-max_b_exp : value*lead_q_term[1] for key,value in b_terms.items()}[^3] for power in list(temp_product.keys()): if power in r: r[power] -= temp_product[power] if abs(r[power])<1e-6: del r[power] # 如果接近于0则删除此项 elif abs(temp_product[power])>1e-6: r[power]=-temp_product[power] print(format_output(q)) print(format_output(r)) # 示例调用函数 poly_divide("4 3 5.0 2 3.0 1 2.0 0 1.0", "2 1 1.0 0 1.0") ```
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三更鬼

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值