为什么WA??!!

P1303 A*B Problem

1234ZHW @ 2017-05-23 17:13:47

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;
int main(){  
string sa,sb; 
short a[10001]={0},b[10001]={0},c[21001]={0};
getline(cin,sa);
getline(cin,sb);
int x,y,z,i,j,k;
x=sa.length();y=sb.length();
for(i=x-1;i>=0;i-- ){a[x-i]=int(sa[i])-48;}
for(i=y-1;i>=0;i--){b[y-i]=int(sb[i])-48;}  
for(i=1;i<=x;i++)
for(j=1;j<=y;j++) 
{
c[i+j-1]+=a[i]*b[j];
c[i+j]+=c[i+j-1]/10;
c[i+j-1]%=10;
}
for(i=i+j+1;i>=0;i--){
if(c[i]>0){k=i;break;}
}
for(i=k;i>=1;i--)cout<<c[i];
return 0;
}

by hehehemc @ 2017-05-23 17:19:21

嘿!ZHW·1234的:

var
  a,b,c:array[0..100000] of longint;
  x,y:ansistring;
  i,j,k,l:longint;
begin
  readln(x);
  readln(y);
  if (x='0')or(y='0') then 
    begin
      writeln(0);
      exit;
    end;
  l:=length(x);
  k:=length(y);
  for i:=1 to l do
    val(x[l-i+1],a[i]);
  for i:=1 to k do
    val(y[k-i+1],b[i]);
  for i:=1 to l do
    for j:=1 to k do
      begin
        c[i+j-1]:=c[i+j-1]+a[i]*b[j];
        if c[i+j-1]>=10 then
          begin
            c[i+j]:=c[i+j]+c[i+j-1] div 10;
            c[i+j-1]:=c[i+j-1] mod 10;
          end;
      end;
  l:=l+k;
  while c[l]=0 do dec(l);
  for i:=l downto 1 do
    write(c[i]);
end.

by 硫代硫酸钠 @ 2017-06-03 12:49:13

你好,试试scanf,别使用getline。我用了gets就错了


|