zoukankan      html  css  js  c++  java
  • 九度oj 1010 A+B

    题目描述:
    读入两个小于100的正整数A和B,计算A+B.
    需要注意的是:A和B的每一位数字由对应的英文单词给出.
    输入:
    测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
    输出:
    对每个测试用例输出1行,即A+B的值.
    样例输入:
    one + two =
    three four + five six =
    zero seven + eight nine =
    zero + zero =
    样例输出:
    3
    90
    96
    这本来是一到水题,但一开始不太会用strcmp函数,又知道scanf(%s)会按空格分开读,所以一开始写出了如下的代码
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <string>
     5 
     6 #define HIGH 11
     7 #define LEN 8
     8 #define MAX 102
     9 using namespace std;
    10 char s[HIGH][LEN] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    11 
    12 char source[MAX];
    13 char A[MAX];
    14 char B[MAX];
    15 int main(int argc, char const *argv[])
    16 {
    17     int a,b;
    18     while(gets(source)) {
    19         int i = 0;
    20         a = 0;
    21         b = 0;
    22         int state = 0;
    23         while(i < strlen(source)) {
    24             if(source[i] == '+') {
    25                 state = 1;
    26                 i++;
    27                 continue;
    28             }
    29             if(source[i] == ' ' || source[i] == '=') {
    30                 i++;
    31                 continue;
    32             }
    33             for(int j = 0; j < 10; j++) {
    34                 if(source[i] == s[j][0]) {
    35                     if(source[i] == 't') {
    36                         if(source[i+1] == 'w') {
    37                             j = 2;
    38                         }
    39                         else if(source[i+1] == 'h') {
    40                             j = 3;
    41                         }
    42                     }
    43                     else if(source[i] == 'f') {
    44                         if(source[i+1] == 'o') {
    45                             j = 4;
    46                         }
    47                         else if(source[i+1] == 'i') {
    48                             j = 5;
    49                         }
    50                     }
    51                     else if(source[i] == 's') {
    52                         if(source[i+1] == 'e') {
    53                             j = 7;
    54                         }
    55                         else if(source[i+1] == 'i') {
    56                             j = 6;
    57                         }
    58                     }
    59                     if(state == 0) {
    60                         a = 10 * a + j;
    61                     }
    62                     else if(state == 1) {
    63                         b = 10 * b + j;
    64                         
    65                     }
    66                     i = i + strlen(s[j]);
    67                     break;
    68                 }
    69                 
    70             }
    71             
    72         }
    73         if(a == 0 && b == 0) {
    74             break;
    75         }
    76         int c = a + b;
    77         printf("%d
    ",c);
    78     }
    79     return 0;
    80 }

    但后来发现,scanf的这个性质其实可以利用起来,所以写出如下的代码

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <string>
     5 
     6 #define HIGH 11
     7 #define LEN 8
     8 #define MAX 102
     9 using namespace std;
    10 char s[HIGH][LEN] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    11 
    12 int main(int argc, char const *argv[])
    13 {
    14     char temp[MAX];
    15     int state = 0;
    16     int a = 0, b = 0;
    17     while(scanf("%s",temp)) {
    18         if(strcmp(temp,"+") == 0) {
    19             state = 1;
    20             continue;
    21         }
    22 
    23         if(strcmp(temp,"=") == 0) {
    24             if(a == 0 && b == 0) {
    25                 break;
    26             }
    27             else {
    28                 printf("%d
    ",a+b);
    29                 state = 0;
    30                 a = 0;
    31                 b = 0;
    32                 continue;
    33             }
    34         }
    35         for(int i = 0; i < 10; i++) {
    36             if(strcmp(temp,s[i])== 0) {
    37                 if(state == 0) {
    38                     a = a * 10 + i;
    39                 }
    40                 else {
    41                     b = b * 10 + i;
    42                 }
    43                 continue;
    44             }
    45         }
    46 
    47     }
    48 
    49     return 0;
    50 }

    需要注意的是,strcmp这个函数,

    若str1=str2,则返回零;
    若str1<str2,则返回负数;
    若str1>str2,则返回正数。
    所以判断是否相等要判断其值是否为0
  • 相关阅读:
    [BZOJ2071] [POI2004]JAS
    [BZOJ1852] [MexicoOI06]最长不下降序列(dp+贪心转移)
    用Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)(转)
    Amazon云计算的一些实用应用(转)
    使用netbeans6.7.1开发webservice 服务端 和 客户端(转)
    深入探索SOAP1.1使用SAAJ1.2.1(转)
    推荐:PoolParty!一个管理EC2集群的Ruby Gem开源工具(转)
    max 加载 菜单项
    2011 新相
    重要的视图类型解释。
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5676056.html
Copyright © 2011-2022 走看看