1 solutions

  • 0
    @ 2024-12-10 23:27:11

    C++ :

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int a[1000],b[1000],c[1000];
    string s1,s2;
    void init();
    void my_minus(int a[],int b[]);
    bool my_comp(string ,string);
    int main()
    {	
    	init();	
    	if(my_comp(s1,s2))	my_minus(a,b);
    	else my_minus(b,a);
    	return 0;	
    }
    void init()
    {	
        //freopen("test0.in","r",stdin);
    	//freopen("test0.out","w",stdout);
    	cin>>s1;
    	cin>>s2;
    	if(my_comp(s1,s2)==false)cout<<'-';
    	a[0]=s1.size();//把数的长度记录在下标为0的数组元素里 
    	b[0]=s2.size();
    	for(int i=1;i<=a[0];++i) a[i]=s1[a[0]-i]-48;
    	for(int i=1;i<=b[0];++i) b[i]=s2[b[0]-i]-48;
    	//上面的两个for循环作用是把字符串s1,s2转换成整数后倒着
    	//存储在整数数组a和b中,倒着存储是为了让数组从下标1到
    	//n依次存储数的从低位到高位	
    }
    bool my_comp(string s1,string s2)//判断s1,s2的大小 
    {
    	bool flag=true;
    	if(s1.size()<s2.size()) flag=false;
    	if((s1.size()==s2.size())&&s1<s2) flag=false;
    	return flag;
    }
    void my_minus(int a[],int b[])
    {
    	int i=1;//i表示现在处理数组的第i位	
    	while(i<=a[0]||i<=b[0])//当i比a[0],b[0]中较大的大就跳出 
    	{
    		if(a[i]<b[i])//判断当前位是否需要向高位借位 
    		{
    			a[i]+=10;
    			a[i+1]--;
    		}
    		c[i]=a[i]-b[i];//把第i位相减的结果存储在c[i]中 		 
    		i++;//计算下一位,注意跳出循环时,i比a[0],b[0]中交大1
    		     
    	}
    	while((c[i]==0)&&(i>1))i--;//相减后高位可能会产生0,此循
    //目的是找到第一非零的位置,但若果结果为零需要保留最后一个零 	
    	c[0]=i;	//把c的长度存在c[0]中 
    	for( int j=c[0];j>0;--j)cout<<c[j];//输出一定要倒着输出 
    }
    
    

    Pascal :

    const maxn=250;
    type arr=array[1..maxn] of integer;
    var a,b,c:arr;
        g,i,j,k,len1,len2,n:longint;
        st:string;
    function f:boolean;
    var j:longint;
    begin
      If len1>len2 then
       begin f:=true; exit; end
      Else
      If len1=len2 then
      Begin
       j:=len1;
       while (j>0) and (a[j]=b[j]) do dec(j);
       if j=0 then
         begin writeln('0'); halt; end
       else if (j>0) then
             if  (a[j]<b[j]) then exit(false) else exit(true);
       end
      else If len1<len2 then exit(false);
    end;
    procedure sub(var a,b:arr;n:longint);
    var i,j:longint;
    begin
      for i:=1 to n do
        begin
         if a[i]>=b[i] then c[i]:=a[i]-b[i]
         else
          begin
            c[i]:=10+a[i]-b[i];
            dec(a[i+1]);
          end;
        end;
    end;
    begin
       readln(st);
       len1:=length(st);
       for i:=1 to len1 do
        a[i]:=ord(st[len1+1-i])-48;
       readln(st);
       len2:=length(st);
       for i:=1 to len2 do
        b[i]:=ord(st[len2+1-i])-48;
       if len1>len2 then n:=len1 else n:=len2;
       if f then sub(a,b,n) else sub(b,a,n);
       if not f then write('-');
       while (n>0) and (c[n]=0) do dec(n);
       if n>0 then
       for i:=n downto 1 do
        write(c[i]);
       writeln;
    end.
    
    • 1

    Information

    ID
    603
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By