码迷,mamicode.com
首页 > 其他好文 > 详细

Overflow UVA 465 方法二求纠错

时间:2014-07-30 20:52:04      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:算法   acm   c   源代码   uva   

注明:

本题使用了两种解法,第一种参考了网上一种非常普遍的解法,即使用atof函数将两个数字字符串转化为两个浮点数,然后直接和int的最大值比较即可。这种方法较简单,不过也是在数据较小的情况下行得通。而第二种是我自己写的一种更较为普遍的解法,其实也就是直接根据字符串进行高精度的运算而已。自己用了很多数据进行测试都没有错,可是就是AC不了,不知道为什么。希望大神指教!!!bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

题目:

Overflow

Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal‘‘ signed integer (type integer if you are working Pascal, type int if you are working in C).

Input

An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.

Output

For each line of input, print the input followed by 0-3 lines containing as many of these three messages as are appropriate: ``first number too big‘‘, ``second number too big‘‘, ``result too big‘‘.

Sample Input

300 + 3
9999999999999999999999 + 11

Sample Output

300 + 3
9999999999999999999999 + 11
first number too big
result too big




源代码:(解法一)

#include <stdio.h>
#include <stdlib.h>
#define INT 2147483647

int main(){
 double a,b;
 char x[500],y[500];
 char operator;
 while(scanf("%s %c %s",x,operator,y)==3){
  a=atof(x);
  b=atof(y);
  if(a>INT) printf("first number too big\n");
  if(b>INT) printf("second number too big\n");
  if(operator=='+'&&a+b>INT||operator=='*'&&a*b>INT)
  printf("result too big\n");
 }

 return 0;
}


(解法二)

#include <stdio.h>
#include <string.h>
#define MAXN 1000+5

char x[MAXN],y[MAXN];
char operator;
char ans[MAXN];//存储答案的字符串,未必从开头开始
char Int[]="2147483647";
char *p;//指向ans中,答案字符串开始的位置

int int_cmp(char *);//比较输入字符串和Int的大小,大于返回1,否则返回0
void add();
void multip();

int main(){
 //freopen("data","r",stdin);
 while(scanf("%s %c %s",x,&operator,y)==3){
  printf("%s %c %s\n",x,operator,y);
  if(int_cmp(x))  printf("first number too big\n");

  if(int_cmp(y))  printf("second number too big\n");

  if(operator=='+')  add();
  else if(operator=='*')  multip();
  
  if(int_cmp(p)) printf("result too big\n");

  }

  return 0;
}

int int_cmp(char *s){
 int len1=strlen(Int);
 int len2=strlen(s);
 int i;

 if(len2>len1)
 return 1;
 else if(len1>len2)
 return 0;
 else {
  for(i=0;i<len1;i++)
   if(Int[i]<s[i]) return 1;
   else if(Int[i]>s[i]) return 0;

   return 0;
 }
}

void add(){//两个字符串的加法
 int j=MAXN-1;
 int carry,sum;
 int len1=strlen(x),len2=strlen(y);
 int i,max;
 max=len1>len2?len1:len2;
 
 ans[j--]='\0';
 carry=0;
 for(i=0;i<max;i++){//从两串的末尾开始相加,结果从ans的末尾开始放入
  sum=carry;
  if(len1-i>0) sum+=x[len1-i-1]-'0';
  if(len2-i>0) sum+=y[len2-i-1]-'0';
  ans[j--]=sum%10+'0';
  carry=sum/10;
  }

 while(carry){//将剩余的进位依次放好
  ans[j--]=carry%10+'0';
  carry/=10;
 }
 p=ans+j+1;
 
 while(*p=='0')
 p++;
 if(*p=='\0')
 p--;

 return ;
}

void multip(){
 int i,j,left,k,pos,product,carry;
 int x_len,y_len;

 ans[MAXN-1]='\0';
 k=left=MAXN-1;
 x_len=strlen(x);
 y_len=strlen(y);
 
 for(i=0;i<MAXN-1;i++)
 ans[i]='0';

 for(i=y_len-1;i>=0;i--){
 k--;//标记每个乘数最右的位置
 pos=k;
 carry=0;
 for(j=x_len-1;j>=0;j--){
  product=(y[i]-'0')*(x[j]-'0')+ans[pos]-'0'+carry;
  ans[pos]=product%10+'0';
  pos--;
  carry=product/10;
  }

  while(carry){
  carry+=ans[pos]-'0';
  ans[pos]=carry%10+'0';
  pos--;
  carry/=10;
  }
  
  left=left<=pos+1?left:pos+1;
  }

  p=ans+left;
  while(*p=='0')//若答案串的前面有0,则去掉
  p++;
  if(*p=='\0')
  p--;
  return;
}


Overflow UVA 465 方法二求纠错,布布扣,bubuko.com

Overflow UVA 465 方法二求纠错

标签:算法   acm   c   源代码   uva   

原文地址:http://blog.csdn.net/u011915301/article/details/38304743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!