Editorial for Mass of Molecule


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const m:array[1..3] of integer=(1,12,16);

var a:string;
    d:array[1..100] of integer;
    dau:array[1..100] of byte;
    re:integer;

procedure rf;
begin
     read(a);
end;

function mass(c:char):integer;
begin
     case c of
          'H':mass:=1;
          'C':mass:=12;
          'O':mass:=16;
     end
end;

procedure pr;
var i,j,t,l,k:byte; code:integer;
begin
     fillchar(dau,sizeof(dau),0);
     l:=length(a);  re:=0;
     for i:=1 to l do d[i]:=1;
     for i:=1 to l do
         if a[i]=')' then
         begin
              for j:=i-2 downto 1 do
                  if (a[j]='(') and (dau[j]=0) then
                  begin
                       dau[j]:=1;
                       if (ord(a[i+1])>=50) and (ord(a[i+1])<=57) then
                       begin
                            val(a[i+1],t,code);
                            for k:=j+1 to i-1 do
                                d[k]:=d[k]*t;
                       end;
                       break;
                  end;
         end
         else
         begin
              if (ord(a[i])>=50) and (ord(a[i])<=57) then
              begin
                   val(a[i],t,code);
                   d[i-1]:=d[i-1]*t;
              end;
         end;
     for i:=1 to l do
         if (a[i]='C') or (a[i]='H') or (a[i]='O') then
             re:=re+mass(a[i])*d[i];
end;

procedure wf;
begin
     write(re);
end;

begin
     rf;
     pr;
     wf;
end.

Code mẫu của happyboy99x

#include<cstdio>
#include<cstring>

int mass(char c) { return c == 'C' ? 12 : c == 'H' ? 1 : 16; }

char s[200];

int calc(int lo, int hi) {
    int res = 0;
    for(int i = lo; i <= hi;) 
        if(s[i] == '(') {
            int cnt = 1, st = i+1;
            for(++i; cnt != 0; ++i) 
                if(s[i] == ')') --cnt;
                else if(s[i] == '(') ++cnt;
            if(s[i] >= 0x30 && s[i] <= 0x39) res += calc(st,i-2) * (s[i++] - 0x30);
            else res += calc(st,i-2);
        } else {
            if(s[i+1] >= 0x30 && s[i+1] <= 0x39) res += mass(s[i]) * (s[(++i)++] - 0x30);
            else res += mass(s[i++]);
        }
    return res;
}

int main() {
    scanf("%s", s);
    printf("%d\n", calc(0, strlen(s) - 1));
    return 0;
}

Code mẫu của ladpro98

program mmass; //VNOI
const   maxN = 10000;
        fi='';
        fo='';
type mystack = record
        items: array[0..maxN] of longint;
        top:longint;
        end;
var     stack:mystack;
        s:ansistring;
        ch,num:set of char;
        inp,oup:text;
        i,temp:longint;
function toVal(c:char):longint;
begin
        if c = 'C' then exit(12)
        else if c = 'O' then exit(16)
        else if c = 'H' then exit(1);
end;

function toNum(c:char):longint;
begin
        exit(ord(c)-48);
end;

function isEmpty:boolean;
begin
        exit(stack.top=0);
end;

function pop:longint;
begin
       dec(stack.top);
       exit(stack.items[stack.top+1]);
end;

function get:longint;
begin
        exit(stack.items[stack.top]);
end;

procedure push(val:longint);
begin
        inc(stack.top);
        stack.items[stack.top]:=val;
end;

procedure setTop(val:longint);
begin
        stack.items[stack.top]:=val;
end;

procedure input;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,s);
        close(inp);
end;

begin
        input;
        s:=s+'0';
        ch:=['C','H','O'];
        num:=['2','3','4','5','6','7','8','9'];
        stack.top:=0;
        i:=1;
        while i<length(s) do
        begin
                if (s[i] in ch) and (s[i+1] in num) then
                begin

                        setTop(get+toVal(s[i])*toNum(s[i+1]));
                        inc(i,2);
                end
                else if s[i] in ch then
                begin
                        setTop(get+toVal(s[i]));
                        inc(i);
                end
                else if s[i] = '(' then
                begin
                        push(0);
                        inc(i);
                end
                else if (s[i] = ')') and (s[i+1] in num) then
                begin
                        temp:=pop*toNum(s[i+1]);
                        setTop(get+temp);
                        inc(i,2);
                end
                else if (s[i] = ')') then
                begin
                        setTop(pop+get);
                        inc(i);
                end;

        end;
        assign(oup,fo);
        rewrite(oup);
        write(oup,get);
        close(oup);
end.

Code mẫu của RR

//Written by Nguyen Thanh Trung
{$R+,Q+}
{$Mode objFPC}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=111;
  so:array['0'..'9'] of longint=(0,1,2,3,4,5,6,7,8,9);
var
  f1,f2:text;
  n:longint;
  s:string;
  stack,left:array[1..MAXN] of longint;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1);
  close(f2);
end;
function cal(l,r:longint):longint;
var
  i,sum:longint;
begin
  sum:=0;
  i:=r+1;
  while i>l do
    begin
      i-=1;
      case s[i] of
        'C': sum+=12;
        'O': sum+=16;
        'H': sum+=1;
        '0'..'9': if s[i-1]=')' then
                    begin
                      sum+=cal(left[i-1]+1,i-2)*so[s[i]];
                      i:=left[i-1];
                    end
                  else
                    begin
                      sum+=cal(i-1,i-1)*so[s[i]];
                      i-=1;
                    end;
        ')': begin
               sum+=cal(left[i]+1,i-1);
               i:=left[i];
             end;
      end;
    end;
  exit(sum);
end;
procedure init;
var
  top,i:longint;
begin
  top:=0;
  for i:=1 to n do
    if s[i]='(' then
      begin
        inc(top);
        stack[top]:=i;
      end
    else if s[i]=')' then
      begin
        left[i]:=stack[top];
        dec(top);
      end;
end;
begin
  openF;
  readln(f1,s); n:=length(s);
  init;
  writeln(f2,cal(1,n));
  closeF;
end.

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>
#include <string.h>
int T(char a)
  {
  int t;         
  if(a=='C')
    t=12;
  else if(a=='H')
    t=1;
  else if(a=='O')
    t=16;
  return t;
  }
main()
  {
  char s[10001];
  int n,m,k,x,N=0,a[10001];
  scanf("%s",s);
  k=strlen(s);
  for(int i=0;i<k;i++)
    if(s[i]=='C'||s[i]=='H'||s[i]=='O')
      {
      int n=0;m=0,x=0;
      if(s[i+1]!='C'&&s[i+1]!='H'&&s[i+1]!='O'&&s[i+1]!='('&&s[i+1]!=')'&&(i+1)!=k)
        x=T(s[i])*(s[i+1]-48);
      else x=T(s[i]);
      for(int j=0;j<i;j++)
        {   
        if(s[j]=='(')
          n++;
        else if(s[j]==')')
          n--;
        }
      for(int j=i+1;j<k;j++)
        {
        if(n==0)
          break;      
        else if(s[j]=='(')
          {
          n++;
          m++;
          }
        else if(s[j]==')')
          {
          n--;
          if(m>0)                
             m--;
          else if(s[j+1]!='C'&&s[j+1]!='H'&&s[j+1]!='O'&&s[j+1]!='('&&s[j+1]!=')'&&(j+1)!=k)
             x*=(s[j+1]-48);
          }
        }                       
      N+=x;
      }
  printf("%d",N);                                                                
  //getch();
  }

Code mẫu của ll931110

Program MMASS;
        Const
                input  = '';
                output = '';
        Var
                  s: string;
                k,r: ansistring;
              stack: ansistring;
                  q: array[1..1000] of integer;
                top: integer;

Procedure convert1;
          Var
                f: text;
                i: integer;
          Begin
                Assign(f, input);
                        Reset(f);
                        Readln(f, s);
                Close(f);

                k:= s[1];
                For i:= 2 to length(s) do
                  If ('2' <= s[i]) and (s[i] <= '9') then
                    Begin
                        k:= k + ' * ';
                        k:= k + s[i];
                    End
                  else if (s[i] = 'C') or (s[i] = 'H') or (s[i] = 'O') or (s[i] = '(') then
                    Begin
                        If s[i - 1] <> '(' then k:= k + ' + ';
                        k:= k + s[i];
                    End
                  else k:= k + s[i];
          End;

Function priority(ch: char): integer;
         Begin
                Case ch of
                  '*': priority:= 2;
                  '+': priority:= 1;
                  '(': priority:= 0;
                End;
         End;

Function pop: char;
         Begin
                pop:= stack[length(stack)];
                Delete(stack, length(stack), 1);
         End;

Function get: char;
         Begin
                get:= stack[length(stack)];
         End;

Procedure convert2;
          Var
                i,top: integer;
                    x: char;
          Begin
                r:= '';
                stack:= '';

                For i:= 1 to length(k) do
                  Case k[i] of
                    '(': stack:= stack + k[i];
                    ')': Repeat
                             x:= pop;
                             If x <> '('then r:= r + x + ' ';
                         Until x = '(';

                '+','*': Begin
                             While (stack <> '')
                                and (priority(k[i]) <= priority(get)) do r:= r + pop + ' ';
                             stack:= stack + k[i];
                         End
                else
                        r:= r + k[i] + ' ';
                  End;

                While stack <> '' do r:= r + pop + ' ';
          End;

Procedure pushval(v: integer);
          Begin
                inc(top);
                q[top]:= v;
          End;

Function popval: integer;
         Begin
                popval:= q[top];
                dec(top);
         End;

Procedure convert3;
          Var
                      f: text;
                i,m1,m2: integer;
          Begin
                top:= 0;
                For i:= 1 to length(r) do
                       If r[i] = 'C' then pushval(12)
                  else if r[i] = 'H' then pushval(1)
                  else if r[i] = 'O' then pushval(16)
                  else if ('2' <= r[i]) and (r[i] <= '9') then pushval(ord(r[i]) - 48)
                  else if (r[i] = '+') or (r[i] = '*') then
                    Begin
                        m2:= popval;
                        m1:= popval;

                        If r[i] = '+' then m1:= m1 + m2;
                        If r[i] = '*' then m1:= m1 * m2;
                        pushval(m1);
                    End;

                Assign(f, output);
                        Rewrite(f);
                        Writeln(f,popval);
                Close(f);
          End;

Begin
        convert1;
        convert2;
        convert3;
End.

Code mẫu của khuc_tuan

//{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q+,R+,S+,T-,U-,V+,W-,X+,Y+,Z1}
// {$APPTYPE CONSOLE}
 {$mode delphi}

var
    a : array[0..110] of char;
    n : integer;

function get(l,r:integer):integer;
var
   i, c, sl: integer;
begin
    if l>r then begin get := 0; exit; end;
    if l=r then
    begin
        if a[l]='H' then get := 1;
        if a[l]='C' then get := 12;
        if a[l]='O' then get := 16;
        exit;
    end;
    if a[r] in ['C','H','O'] then
    begin
        get := get(l,r-1) + get(r,r);
        exit;
    end
    else if a[r] = ')' then
    begin
        c := 0;
        for i:=r downto l do
        begin
            if a[i]='(' then dec(c);
            if a[i]=')' then inc(c);
            if c=0 then
            begin
                get := get(i+1,r-1) + get(l,i-1);
                exit;
            end;
        end;
    end
    else
    begin
        sl := ord(a[r]) - ord('0');
        if a[r-1] in ['C','O','H'] then
        begin
            get := get(l,r-2) + get(r-1,r-1) * sl;
            exit;
        end;
        c := 0;
        for i:=r-1 downto l do
        begin
            if a[i]='(' then dec(c);
            if a[i]=')' then inc(c);
            if c=0 then
            begin
                get := sl * get(i+1,r-2) + get(l,i-1);
                exit;
            end;
        end;
    end;
end;

begin
    readln(a);
    n := Length(PChar(@a));
    writeln(get(0,n-1));
end.

Comments

Please read the guidelines before commenting.


There are no comments at the moment.