1 条题解

  • 0
    @ 2025-4-12 21:47:19

    C :

    #include "stdio.h"
    #include "string.h"
    char strnum[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    int getnum(char str[]){
    	int i;
    	for(i=0;i<10;i++){
    	if(!strcmp(strnum[i],str))//strcmp两字符串相等是为0
    	  return i;
    	}
    	return 0;
    	}
    int main()
    {
    	char str[100];
    	while(scanf("%s",str)!=EOF){
    		int a=0;
    		int b=0;
    		while(strcmp(str,"+")){
    			a=a*10+getnum(str);
    			scanf("%s",str);}
    		while(strcmp(str,"=")){
    			
    			b=b*10+getnum(str);
    			scanf("%s",str);}
    		if(!a&&!b)
    		break;
    		printf("%d\n",a+b);
    		
    		
    		
    		}
    	
    	return 0;
        
    }
    

    C++ :

    #include <stdio.h>
    #include <string.h>
    
    char strNum[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    int GetNum(char str[]){
        for(int i=0; i<10; i++){
            if(!strcmp(str, strNum[i])){
                return i;
            }
        }
        return 0;
    }
    
    int main(){
        char str[100];
        while(scanf("%s", str) != EOF){
            int a = 0;
            int b = 0;
            while(strcmp(str, "+")){
                a = a*10 + GetNum(str);
                scanf("%s", str);
            }
            while(strcmp(str, "=")){
                b = b*10 + GetNum(str);
                scanf("%s", str);
            }
            if(!a && !b){
                break;
            }
            printf("%d\n", a+b);
        }
    
        return 0;
    }
    
    

    Pascal :

    var st,st1,sta,st2,st3:string;
        a,b,sum,p,p2:longint;
    
    begin
       while not eof do
         begin
           readln(st);
           st:=st+' ';
           sta:='';
           while st<>'' do
             begin
              p:=pos(' ',st);
              st1:=copy(st,1,p-1);
              if st1='one' then sta:=sta+'1';
              if st1='two' then sta:=sta+'2';
              if st1='three' then sta:=sta+'3';
              if st1='four' then sta:=sta+'4';
              if st1='five' then sta:=sta+'5';
              if st1='six' then sta:=sta+'6';
              if st1='seven' then sta:=sta+'7';
              if st1='eight' then sta:=sta+'8';
              if st1='nine' then sta:=sta+'9';
              if st1='zero' then sta:=sta+'0';
              if st1='+' then sta:=sta+'+';
              if st1='='then sta:=sta+'=';
              delete(st,1,p);
             end;
    
             p:=pos('+',sta);
             st2:=copy(sta,1,p-1);
             val(st2,a);
             p2:=pos('=',sta);
             st3:=copy(sta,p+1,p2-p-1);
             val(st3,b);
             sum:=a+b;
             if (a<>0) and (b<>0) then writeln(sum);
          end;
    end.
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner scanner=new Scanner(System.in);
    		while(scanner.hasNext())
    		{
    			String temp=scanner.nextLine();
    			calculate(temp);
    		}
    		
    
    	}
    
    	private static void calculate(String temp) {
    		String buffer[]=temp.split(" ");
    		if(buffer.length==4)
    		{   int r=translate(buffer[0])+translate(buffer[2]);
    		    if(r!=0)
    			System.out.println(r);
    		}
            else if (buffer.length==6) {
            	System.out.println(translate(buffer[0])*10+translate(buffer[1])+translate(buffer[3])*10+translate(buffer[4]));
    		}
    		else if (buffer.length==5) {
    			if(buffer[2].equals("+"))
    			{
    				System.out.println(translate(buffer[0])*10+translate(buffer[1])+translate(buffer[3]));
    			}
    			else if (buffer[1].equals("+")) {
    				System.out.println(translate(buffer[0])+translate(buffer[2])*10+translate(buffer[3]));
    			}
    		}
    	}
    
    	private static int translate(String string) {
    
    		switch (string) {
    		case "zero":
    	        return 0;
    		case "one":
    	        return 1;
    		case "two":
    	        return 2;
    		case "three":
    	        return 3;	        
    		case "four":
    	        return 4;
    		case "five":
    	        return 5;
    		case "six":
    	        return 6;        
    		case "seven":
    	        return 7;
    		case "eight":
    	        return 8;
    		case "nine":
    	        return 9;
    		}
    		return -1;
    	}
    
    }
    
    

    Python :

    #!/usr/bin/env python
    # -- coding: utf-8 --
    
    num_lists = {
        'zero'  : 0,
        'one'   : 1,
        'two'   : 2,
        'three' : 3,
        'four'  : 4,
        'five'  : 5,
        'six'   : 6,
        'seven' : 7,
        'eight' : 8,
        'nine'  : 9,
        }
    
    
    iszero = False  # 当 zero + zero 时,停止
    
    while not iszero:
    
        line = raw_input()
        chars = line.split()
    
        if chars[1] == '+' and chars[3] == '=':
            a = num_lists[chars[0]]
            b = num_lists[chars[2]]
        elif chars[1] == '+' and chars[4] == '=':
            a = num_lists[chars[0]]
            b = num_lists[chars[2]] * 10 + num_lists[chars[3]]
        elif chars[2] == '+' and chars[4] == '=':
            a = num_lists[chars[0]] * 10 + num_lists[chars[1]]
            b = num_lists[chars[3]]
        elif chars[2] == '+' and chars[5] == '=':
            a = num_lists[chars[0]] * 10 + num_lists[chars[1]]
            b = num_lists[chars[3]] * 10 + num_lists[chars[4]]
        else:
            print "input error!"
    
        if chars[0] == 'zero' and chars[2] == 'zero':
            iszero = True
        else:
            print a + b
    
    • 1

    信息

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