A+B Problem && Financial Management

本文解析了ACM编程挑战赛中的两个经典题目:一个是处理大整数加减法的问题,通过自定义类实现字符串形式的大整数运算;另一个是计算银行账户十二个月平均余额的题目,涉及浮点数的特殊处理。

A+B Problem
Time Limit:1000MS  Memory Limit:10000K
Total Submit:33936 Accepted:17764

Description
Calculate a+b

Input
Two integer a,b (0<=a,b<=10)

Output
Output a+b

Sample Input

 

Sample Output

 

Hint
这题当然没有字面的那么简单,必须要求考虑到长整数的存储.最后完成代码如下,说真的代码写得太臃肿了:(

#include<iostream>
#include<stack>
#include<string>

using namespace std;

class ADD
{
public:
 string A;
 int flag;
 ADD(){flag=0;}
 bool operator>(ADD B)
 {
  if((A.length()-flag)>(B.A.length()-B.flag))return 1;
  if((A.length()-flag)<(B.A.length()-B.flag))return 0;
  string::iterator iter_A=A.begin();
  string::iterator iter_B=B.A.begin();
  if(flag)iter_A++;
  if(B.flag)iter_B++;
  while((iter_A<=A.end())&&(iter_B<=B.A.end()))
  {
    if((*iter_A)>*(iter_B))
    return 1;
    else if((*iter_A)<*(iter_B))
     return 0;
    else{
     iter_A++;
     iter_B++;
    }
  }
 
 }
 friend istream& operator>>(istream& is,ADD &A)
 {
  cin>>A.A;
  if(A.A[0]=='-')A.flag=1;
  return is;
 }
 void Add(ADD B)
 {
  string::iterator iter_A=A.end();
  string::iterator iter_B=B.A.end();
  iter_A--;
  iter_B--;
  int carry=0;
  int sum;
  stack<int> C;
  while(iter_A>=(A.begin()+flag)&&iter_B>=(B.A.begin()+B.flag))
  {
   sum=(*iter_A)-48+(*iter_B)-48+carry;
   carry=sum/10;
   sum=sum%10;
   C.push(sum);
   iter_A--;
   iter_B--;
  }
  while(iter_A>=(A.begin()+flag))
  {
   sum=(*iter_A)-48+carry;
   carry=sum/10;
   sum=sum%10;
   C.push(sum);
   iter_A--;
  }
  while(iter_B>=(B.A.begin()+B.flag))
  {
   sum=(*iter_B)-48+carry;
   carry=sum/10;
   sum=sum%10;
   C.push(sum);
   iter_B--;
  }
  if(carry!=0)C.push(carry);
  if(flag==1)cout<<'-';
  while(!C.empty())
  {
   int temp=C.top();
   C.pop();
   cout<<temp;
  }
  cout<<endl;
 }
 void SUB(ADD B)
 {
  string::iterator iter_A=A.end();
  string::iterator iter_B=B.A.end();
  iter_A--;
  iter_B--;
  int sum;
  stack<int> C;
  while(iter_A>=(A.begin()+flag)&&iter_B>=(B.A.begin()+B.flag))
  {
   sum=(*iter_A)-(*iter_B);
   if(sum<0)
   {
    sum=10+sum;
    (*(iter_A-1))=(*(iter_A-1))-1;
   }
   C.push(sum);
   iter_A--;
   iter_B--;
  }
  while(iter_A>=(A.begin()+flag))
  {
   sum=(*iter_A)-48;
   if(sum!=0)
   {
    if(sum>0)C.push(sum);
    else{
     C.push(10+sum);
     (*(iter_A-1))=(*(iter_A-1))-1;
    }
   }
   iter_A--;
  }
  
  while(!C.empty()&&(C.top()==0))C.pop();
  if(C.empty())
  {
   cout<<0;
   cout<<endl;
   return;
  }
  if(flag==1)cout<<'-';
  while(!C.empty())
  {
   
   
   int temp=C.top();
   C.pop();
   cout<<temp;
  }
  cout<<endl;
 }
};
int main()
{
 ADD A;
 ADD B;
 while(cin>>A>>B){
 if((A.flag+B.flag)!=1)A.Add(B);
 else
 {
  if(A>B)
  A.SUB(B);
  else
   B.SUB(A);
 }

另外一题是相近的,不过只是要处理特殊浮点数及其除法,问题如下

Financial Management
Time Limit:1000MS  Memory Limit:10000K
Total Submit:15307 Accepted:6728

Description
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.

Output
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.

Sample Input

 

Sample Output

 

Source
Mid-Atlantic 2001


 }
 return 0;
}

完成代码如下:

#include <iostream>
#include<string.h>
#include<fstream>
using namespace std;

ifstream outfile("data.txt");


class Double
{
public:
 char Integer[1000]; //整数部分
 char Decimal[3]; //小数部分
 void ADD(Double B);
 void DIVIE();/*除以12 个月份*/
};

void Double::ADD(Double B)//假设B的整数位小
{
 int temp=atoi(Decimal)+atoi(B.Decimal);
 char D_result[3];
 char carry='0';
 _itoa(temp,D_result,10);
 if(temp<9)/*小于两位数要补上一个0*/
 {
  Decimal[strlen(Decimal)-1] = D_result[0];
 }
 else if (temp>99)
 {
  strncpy(Decimal,D_result+1,2);/*只CPOY低两位,其余高位用于进位*/
  carry=D_result[0];
 }
 else/*否则不改变*/
 {
  strcpy(Decimal,D_result);
 }
 int i = strlen(Integer)-1;
 int j = strlen(B.Integer)-1;
 int k=0;
 char I_reslut[1000];
 while(i>=0&&j>=0)/*从低位开始运算*/
 {
  temp =Integer[i]-48+B.Integer[j]-48+carry-48;/*就像一个一位全加器运算一样*/
  if(temp<9)
  {
   carry='0';
   I_reslut[k] = temp + 48;
  }
  else
  {
   carry = temp/10+48;
   I_reslut[k] = temp%10+48;
  }
  i--;
  j--;
  k++;
 }
 while(i>=0)/*当操作数A有多余时*/
 {
  temp = Integer[i] - 48 + carry -48;
  if(temp<9)
  {
   carry='0';
   I_reslut[k] = temp + 48;
  }
  else
  {
   carry = temp/10+48;
   I_reslut[k] = temp%10+48;
  }
  i--;
  k++;
 }
 while(j>=0)/*当操作数B有多余时*/
 {
  temp = B.Integer[j] - 48 + carry -48;
  if(temp<9)
  {
   carry='0';
   I_reslut[k] = temp + 48;
  }
  else
  {
   carry = temp/10+48;
   I_reslut[k] = temp%10+48;
  }
  j--;
  k++;
 }
 while(carry!='0')/*当两数相同长度且有进位时*/
 {
  I_reslut[k++] =carry;
 }
 strncpy(Integer,I_reslut,k);
 _strrev(Integer);
}

void Double::DIVIE()/*由于求十二个月的平均值,所以就可以针对特殊问题简化*/
{
 int len = strlen(Integer);
 int temp = Integer[0]-48;
 char D_result[1000];
 char I_result[1000];
 int i,k;
 for ( i=1,k=0;i<=len-1;i++)
 {
  temp = temp*10+Integer[i]-48;/* 如秦九韶算法由高位开始计算商,因为12相除最多不超过109*/
  while(temp<12)temp=temp*10+(Integer[++i]-48);
  I_result[k]=temp/12+48;/*所以更多的情况是除于两位*/
  temp=temp%12;
  k++;
 }
 strncpy(Integer,I_result,k);
 Integer[k]='/0';/*这里如果忘记了加结束符就会出错,对字符串操作要特别小心*/
 len = strlen (Decimal);
 for ( i=0,k=0;i<=len-1;i++)/*对小数位求商*/
 {
  temp = temp*10+Decimal[i]-48;
  while(temp<12)temp=temp*10+(Decimal[++i]-48);
  D_result[k]=temp/12+48;
  temp=temp%12;
  k++;
 }
 strncpy(Decimal,D_result,k);
 Decimal[k]='/0';
}

Double sum,num;
int main()
{
 strcpy(sum.Decimal,"00");
 
 for(int i=0;i<12;i++)
 {
  cin.getline(num.Integer ,1000,'.');/*把一个浮点数拆分成整数位和小数位*/
  cin.getline(num.Decimal ,1000,'/n');
  sum.ADD(num);/*把十二个月的值累加*/
 }
 sum.DIVIE();/*除于12求出平均值*/
 cout<<"$"<<sum.Integer <<"."<<sum.Decimal<<endl ;/* 输出结果 */
 return 0;
}

还是那句代码太臃肿了~还望高手指点,一个ACM入门者:)终于可睡觉了~今天好累O~

$1581.42
100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75
3
1 2
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合数据驱动方法与Koopman算子理论的递归神经网络(RNN)模型线性化方法,旨在提升纳米定位系统的预测控制精度与动态响应能力。研究通过构建数据驱动的线性化模型,克服了传统非线性系统建模复杂、计算开销大的问题,并在Matlab平台上实现了完整的算法仿真与验证,展示了该方法在高精度定位控制中的有效性与实用性。; 适合人群:具备一定自动化、控制理论或机器学习背景的科研人员与工程技术人员,尤其是从事精密定位、智能控制、非线性系统建模与预测控制相关领域的研究生与研究人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能预测控制;②为复杂非线性系统的数据驱动建模与线性化提供新思路;③结合深度学习与经典控制理论,推动智能控制算法的实际落地。; 阅读建议:建议读者结合Matlab代码实现部分,深入理解Koopman算子与RNN结合的建模范式,重点关注数据预处理、模型训练与控制系统集成等关键环节,并可通过替换实际系统数据进行迁移验证,以掌握该方法的核心思想与工程应用技巧。
解决 A+B Problem 是编程入门中最基础的问题之一,通常要求从标准输入中读取两个整数 A 和 B,并输出它们的和。以下是几种常见编程语言的实现方法,每种方法都展示了如何正确读取输入、计算和输出结果。 ### C++ 实现 在 C++ 中,可以使用 `cin` 读取输入,使用 `cout` 输出结果。以下是一个简洁的实现: ```cpp #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b; return 0; } ``` 如果需要处理多组输入,可以使用循环结构来持续读取输入并输出结果: ```cpp #include <iostream> using namespace std; int main() { int a, b; while (cin >> a >> b) { cout << a + b << endl; } return 0; } ``` ### C 语言实现 在 C 语言中,可以使用 `scanf` 读取输入,使用 `printf` 输出结果。以下是一个简单的实现: ```c #include <stdio.h> int main() { int a, b; scanf("%d %d", &a, &b); printf("%d\n", a + b); return 0; } ``` 如果需要处理多组输入,可以使用循环结构来持续读取输入并输出结果: ```c #include <stdio.h> int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { printf("%d\n", a + b); } return 0; } ``` ### Python 实现 在 Python 中,可以使用 `input()` 函数读取输入,使用 `print()` 函数输出结果。以下是一个简单的实现: ```python a, b = map(int, input().split()) print(a + b) ``` 如果需要处理多组输入,可以使用循环结构来持续读取输入并输出结果: ```python import sys for line in sys.stdin: a, b = map(int, line.strip().split()) print(a + b) ``` ### Java 实现 在 Java 中,可以使用 `Scanner` 类读取输入,使用 `System.out.println()` 函数输出结果。以下是一个简单的实现: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + b); } } ``` 如果需要处理多组输入,可以使用循环结构来持续读取输入并输出结果: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + b); } } } ``` ### JavaScript 实现 在 JavaScript 中,可以使用 `readline` 模块读取输入,使用 `console.log()` 函数输出结果。以下是一个简单的实现: ```javascript const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', (line) => { const [a, b] = line.split(' ').map(Number); console.log(a + b); }); ``` ### 总结 不同编程语言解决 A+B Problem 的方法各有不同,但核心逻辑是相同的:读取输入、计算和输出结果。选择合适的语言和方法,可以根据具体需求和环境进行调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值