A+B Problem && Financial Management

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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值