2.19 A+B

本文介绍了一种将带有逗号和负号的字符串转换为长整型数字的方法,并通过实例演示了如何进行数学运算。重点讲解了字符串遍历、字符转数字技巧以及使用C++实现的代码示例。

题目:

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。 现在请计算A+B的结果,并以正常形式输出。

输入描述:

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出描述:

请计算A+B的结果,并以正常形式输出,每组数据占一行。

示例1

输入

-234,567,890 123,456,789
1,234 2,345,678

输出

-111111101
2346912

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

long long change(string s){

    long long num=0;;
    for(int i=0;i<s.length();i++){
        if(s[i]!=','&&s[i]!='-'){
            num=num*10+s[i]-'0';
        }
    }

    if(s[0]=='-') return -num;
    else return num;
}
int main(){

    string s1;
    string s2;

    cin>>s1;
    cin>>s2;

    long long a=change(s1);
    long long b=change(s2);
    cout<<a+b;

}

这题我能学习的地方(即我所需要帮助的地方):
1.对于一个输入有数字有字符,最好选用字符串,则需要头文件#include<string.h>

一些可能用到的函数

strcmp

int strcmp(const char *str1, const char *str2);
按字典顺序比较两个字符串,返回整数值的意义如下:
小于0,str1小于str2;
等于0,str1等于str2;
大于0,str1大于str2;

strlen

size_t strlen(const char *str);
返回字符串str的长度,’\0’不算在内。
或者用str.length()

2.对于一串字符串有字符和数字转化为数字,一个个的遍历字符,用c-‘0’转化
3.对于数字的加减乘除,最后转化为数字是最好的
"C:\Program Files\Python312\python.exe" C:\Users\Administrator\py2025\MCP01A.py ===== 雷达系统优化程序 ===== 目标: 最大化雷达检测距离 === 求解器状态检测 === IPOPT (非线性求解器): 可用 CBC (整数规划求解器): 不可用 GLPK (线性规划求解器): 不可用 警告: 关键求解器缺失,使用备用方案... [策略1] 使用IPOPT求解... Ipopt 3.14.19: ****************************************************************************** This program contains Ipopt, a library for large-scale nonlinear optimization. Ipopt is released as open source code under the Eclipse Public License (EPL). For more information visit https://github.com/coin-or/Ipopt ****************************************************************************** This is Ipopt version 3.14.19, running with linear solver MUMPS 5.8.0. Number of nonzeros in equality constraint Jacobian...: 0 Number of nonzeros in inequality constraint Jacobian.: 3 Number of nonzeros in Lagrangian Hessian.............: 5 Total number of variables............................: 3 variables with only lower bounds: 0 variables with lower and upper bounds: 3 variables with only upper bounds: 0 Total number of equality constraints.................: 0 Total number of inequality constraints...............: 1 inequality constraints with only lower bounds: 1 inequality constraints with lower and upper bounds: 0 inequality constraints with only upper bounds: 0 iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls 0 -1.0100000e+02 0.00e+00 1.00e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0 1 -1.0208899e+02 0.00e+00 4.95e+00 -1.0 1.10e+00 - 4.95e-01 9.90e-01f 1 2 -3.1859175e+02 0.00e+00 9.80e+02 -1.0 2.19e+02 - 9.81e-03 9.90e-01f 1 3 -9.9003186e+04 0.00e+00 5.43e+04 -1.0 1.83e+05 - 1.19e-03 5.40e-01f 1 4 -9.9990032e+04 0.00e+00 7.89e+04 -1.0 1.99e+03 - 9.90e-01 4.96e-01f 1 5 -9.9999900e+04 0.00e+00 2.32e+03 -1.0 2.00e+01 - 1.00e+00 9.95e-01f 1 6 -9.9999900e+04 0.00e+00 9.98e-07 -1.0 4.79e+02 - 1.00e+00 1.00e+00f 1 7 -1.0000000e+05 0.00e+00 8.88e+00 -8.6 1.00e-01 - 1.00e+00 1.00e+00f 1 8 -1.0000000e+05 0.00e+00 2.44e-14 -8.6 1.00e-07 - 1.00e+00 1.00e+00f 1 Number of Iterations....: 8 (scaled) (unscaled) Objective...............: -1.0000000009999750e+05 -1.0000000009999750e+05 Dual infeasibility......: 2.4424906541753444e-14 2.4424906541753444e-14 Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00 Variable bound violation: 9.9997501820325851e-05 9.9997501820325851e-05 Complementarity.........: 2.5059035596830285e-09 2.5059035596830285e-09 Overall NLP error.......: 2.5059035596830285e-09 2.5059035596830285e-09 Number of objective function evaluations = 9 Number of objective gradient evaluations = 9 Number of equality constraint evaluations = 0 Number of inequality constraint evaluations = 9 Number of equality constraint Jacobian evaluations = 0 Number of inequality constraint Jacobian evaluations = 9 Number of Lagrangian Hessian evaluations = 8 Total seconds in IPOPT = 0.003 EXIT: Optimal Solution Found. ===== 优化结果 ===== ✅ 优化成功! 求解器: <undefined> 最大检测距离 R = 100,000.00 米 发射功率 P_t = 500.50 W 天线增益 G = 6 🔥 程序异常: unsupported format string passed to NPV_DivisionExpression.__format__ Traceback (most recent call last): File "C:\Users\Administrator\py2025\MCP01A.py", line 181, in main print(f"实际接收功率 P_r = {P_r:.3e} W (要求: > {model.P_min.value:.1e} W)") ^^^^^^^^^ TypeError: unsupported format string passed to NPV_DivisionExpression.__format__ 进程已结束,退出代为 0
最新发布
10-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值