ZOJ Problem Set - 1292

本文介绍了一种使用字符串实现高精度加法的方法,解决了大数相加的问题,并提供了完整的C++代码示例。
Integer Inquiry

Time Limit: 2 Seconds      Memory Limit: 65536 KB

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.

``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)


Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.


Output

Your program should output the sum of the VeryLongIntegers given in the input.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0


Sample Output

370370367037037036703703703670


解题报告:

题意就是几个很大的数相加,这时想到用string来做,但是想了半天也不知道怎么写,我还是太水了大哭

看了下大佬的解题过程,有些地方没看懂,智商捉急啊!!!


#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
//高精度相加模板 
string add(string str1,string str2)//高精度加法,两个正数相加 
{
    string str;

    int len1=str1.length();
    int len2=str2.length();
    if(len1<len2)                 //前面补0,弄成两个数长度相同
    {
        for(int i=1;i<=len2-len1;i++)
           str1="0"+str1;      
    }
    else
    {
        for(int i=1;i<=len1-len2;i++)
           str2="0"+str2;
    }
    len1=str1.length();
    int cf=0;
    int temp;
    for(int i=len1-1;i>=0;i--)     //从第一位开始相加 
    {
        temp=str1[i]-'0'+str2[i]-'0'+cf;
        cf=temp/10;             //低位相加,大于10进位 
        temp%=10;              //低位相加,大于10进位,个位数留下来 
        str=char(temp+'0')+str;     //char(temp+'0')不懂 
    }
    if(cf!=0)  str=char(cf+'0')+str;   //不为0最高位进1 
    return str;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string sum="0";
        string str1;
        while(cin>>str1)
        {
            if(str1=="0")break;
            sum=add(sum,str1);
        }
        cout<<sum<<endl;
        if(T>0)cout<<endl;
    }
    return 0;

}


内容概要:本文提出了一种针对大规模电动汽车接入电网的双层优化调度策略,并基于IEEE33节点系统进行了建模与仿真分析,配套提供了完整的Matlab代码实现。该策略构建了上层电网运行优化与下层电动汽车充电调度的双层协同模型,综合考虑电网负荷削峰填谷、电压稳定性维持以及电动汽车用户充电需求满足等多重目标,采用先进的优化算法实现对电动汽车集群的智能有序调度。研究详细阐述了双层模型的构建逻辑、目标函数设计、约束条件设定及迭代求解流程,有效降低了电网峰谷差,提升了配电系统对可再生能源的消纳能力,兼具扎实的理论深度与明确的工程应用前景。; 适合人群:电气工程、电力系统及其自动化、能源系统优化等相关专业的研究生、科研人员以及从事智能电网、电动汽车调度、分布式能源管理等领域工作的工程师和技术人员。; 使用场景及目标:①深入研究高比例电动汽车接入对配电网运行特性的影响机制;②掌握电力系统双层优化建模方法及其在实际系统中的求解技巧;③实现电动汽车集群的协同调度与车网互动(V2G)优化控制;④作为撰写学术论文、开展课题研究或复现高水平期刊成果的技术参考与代码基础。; 阅读建议:建议读者结合所提供的Matlab代码逐行理解双层优化模型的数学表达与程序实现细节,重点剖析上下层模型之间的信息交互机制与收敛判据,可通过调整电动汽车渗透率、充电行为参数或引入分布式电源等场景进行拓展性仿真,以深化对智能调度策略适应性的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值