1 条题解

  • 0
    @ 2021-6-14 23:29:11

    C++ :

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int main() {
        
        int nums[2] = {0}, letters[2] = {0};
        char ch, le;
        int nownum = 0, negative = 1, lr = 0;
        
        while(cin >> ch) {
            if (ch == '=') {
                lr = 1;
                nums[0] += negative * nownum;
                nownum = 0; negative = 1;
            } else if (ch == '-') {
                nums[lr] += negative * nownum;
                nownum = 0; negative = -1;
            } else if (ch == '+') {
                nums[lr] += negative * nownum;
                nownum = 0; negative = 1;
            } else if (ch >= 'a' && ch <= 'z') {
                le = ch;
                letters[lr] += negative * nownum;
                nownum = 0; negative = 1;
            } else {
                nownum *= 10; nownum += ch - '0';
            }
        }
        
        nums[lr] += negative * nownum;
        
        int num = nums[1] - nums[0], letter = letters[0] - letters[1];
        
        printf("%c=%.3f\n", le, (num * 1.0 / (letter * 1.0)));
        
        return 0;
        
    }
    

    Pascal :

    var s,left,right,letter:ansistring;
        i,j,x,left_ans,right_ans,left_num,right_num:longint;
    begin
      readln(s);
      x:=pos('=',s);
      left:=copy(s,1,x-1);
      right:=copy(s,x+1,length(s)-x);
      x:=0;
      for i:=1 to length(left) do
        begin
          if (left[i]>='a')and(left[i]<='z')or(left[i]>='A')and(left[i]<='Z') then
            if (x=0) then
              begin
                if (i<>length(left))and((not((left[i+1]>='a')
                   and(left[i+1]<='z')or(left[i+1]>='A')
                 and(left[i+1]<='Z')))) then
                   begin
                     for j:=i-1 downto 2 do
                       if not((left[j-1]>='0')and(left[j-1]<='9')) then break;
                     if left[j]='-' then left_num:=left_num-1
                     else inc(left_num);
                     x:=0;
                     continue;
                   end;
              end
            else
              begin
                if (i<>length(left))and((not((left[i+1]>='a')and(left[i+1]<='z')
                   or(left[i+1]>='A')
                 and(left[i+1]<='Z')))) then
                   begin
                     for j:=i-1 downto 1 do
                       if (left[j]='-')or(left[j]='+') then break;
                     if left[j]='-' then left_num:=left_num-x
                     else left_num:=left_num+x;
                     x:=0;
                     continue;
                   end;
              end;
          if (left[i]>='0')and(left[i]<='9') then x:=x*10+ord(left[i])-48;
          if (left[i]='-')or(left[i]='+') then
            begin
              if (i<>1)and(not((left[i-1]>='a')and(left[i-1]<'z')or
                 (left[i-1]>='A')and(left[i-1]<='Z'))) then
                   begin
                     for j:=i-1 downto 1 do
                       if (left[j]='-')or(left[j]='+') then break;
                     if left[j]='-' then left_ans:=left_ans-x
                     else left_ans:=left_ans+x;
                     x:=0;
                   end;
            end;
        end;
      for i:=length(left) downto 1 do
        if (left[i]='-')or(left[i]='+') then break;
      if (left[i]='-') then
        begin
          if (left[length(left)]>='0')and(left[length(left)]<='9') then
             left_ans:=left_ans-x
          else
            begin
              if x<>0 then left_num:=left_num-x
              else left_num:=left_num+1;
            end;
        end
      else
          if (left[length(left)]>='0')and(left[length(left)]<='9') then
             left_ans:=left_ans+x
          else
            begin
              if x<>0 then left_num:=left_num+x
              else left_num:=left_num+1;
            end;
      x:=0;
      for i:=1 to length(right) do
        begin
          if (right[i]>='a')and(right[i]<='z')or(right[i]>='A')and(right[i]<='Z')
            then
            if (x=0) then
              begin
                if (i<>length(right))and((not((right[i+1]>='a')
                   and(right[i+1]<='z')or(right[i+1]>='A')
                 and(right[i+1]<='Z')))) then
                   begin
                     for j:=i-1 downto 2 do
                       if not((right[j-1]>='0')and(right[j-1]<='9')) then break;
                     if right[j]='-' then right_num:=right_num-1
                     else inc(right_num);
                     x:=0;
                     continue;
                   end;
              end
            else
              begin
                if (i<>length(right))and((not((right[i+1]>='a')
                   and(right[i+1]<='z')or(right[i+1]>='A')
                 and(right[i+1]<='Z')))) then
                   begin
                     for j:=i-1 downto 1 do
                       if (right[j]='-')or(right[j]='+') then break;
                     if right[j]='-' then right_num:=right_num-x
                     else right_num:=right_num+x;
                     x:=0;
                     continue;
                   end;
              end;
          if (right[i]>='0')and(right[i]<='9') then x:=x*10+ord(right[i])-48;
          if (right[i]='-')or(right[i]='+') then
            begin
              if (i<>1)and(not((right[i-1]>='a')and(right[i-1]<'z')or
                 (right[i-1]>='A')and(right[i-1]<='Z'))) then
                   begin
                     for j:=i-1 downto 1 do
                       if (right[j]='-')or(right[j]='+') then break;
                     if right[j]='-' then right_ans:=right_ans-x
                     else right_ans:=right_ans+x;
                     x:=0;
                   end;
            end;
        end;
      for i:=length(right) downto 1 do
        if (right[i]='-')or(right[i]='+') then break;
      if (right[i]='-') then
        begin
          if (right[length(right)]>='0')and(right[length(right)]<='9') then
            right_ans:=right_ans-x
          else
            begin
              if x<>0 then right_num:=right_num-x
              else right_num:=right_num+1;
            end;
        end
      else
          if (right[length(right)]>='0')and(right[length(right)]<='9') then
            right_ans:=right_ans+x
          else
            begin
              if x<>0 then right_num:=right_num+x
              else right_num:=right_num+1;
            end;
      for i:=1 to length(s) do
        if (s[i]>='A')and(s[i]<='Z')or(s[i]>='a')and(s[i]<='z') then
          begin
            j:=i;
            while (j<=length(s))and(s[j]>='A')and(s[j]<='Z')or(s[j]>='a')
                  and(s[j]<='z') do
              begin
                letter:=letter+s[j];inc(j);
              end;
            break;
          end;
      writeln(letter,'=',(left_ans+(-right_ans))/(right_num+(-left_num)):0:3);
    end.
    
    • 1

    信息

    ID
    199
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者