zoukankan      html  css  js  c++  java
  • 【C/C++】习题3-2 分子量/算法竞赛入门经典/字符串

    给出一种物质的分子式,求分子量。只包含4种原子,分别为C,H,O,N。
    【知识点】
    1.ASCII码
    【阿拉伯数字】48~57
    【大写字母】65~90
    【小写字母】97~122
    2.输入循环到n-1的原因
    因为我用的结束输入是enter+ctrlz+enter的结束方法,但是读入字符的话,会多一个enter。
    ->直接scanf("%s")就不会多一个enter,但是因为数组长度是n,而数组下标是从0开始,所以实际坐标是0到n-1.

    【代码】

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    const char* list = "CHON";
    float weight[] = {12.01, 1.008, 16.00, 14.01};
    const int maxn = 100;
    char s[maxn];
    
    int get_loc(char a)
    {
       for (int i = 0; i < 4; i++)
       {
          if(a == list[i]) return i;
       }
       return -1;
    }
    
    int main()
    {
       int T;
       float sum = 0.0;
       scanf("%d",&T);
       while(T--)
       {
          scanf("%s",s);
          printf("%s
    ",s);
          int n = strlen(s);
          int loc;
          //printf("%c
    ",s[n-1]);
          if( s[n-1]>=48 && s[n-1]<=57 ) //最后一位是数字
          {
             for(int i = 0; i < n ; i++) //可以读到最后一位
             {
                if (s[i] >= 65 && s[i]<= 90)
                {
                   loc = get_loc(s[i]);
                   if (s[i + 1] >= 65 && s[i + 1]<= 90) //如果下一位是字母
                   {
                      sum += weight[loc];
                   }                                                                                                                                                  
                   else //如果下一位是数字
                   {
                      printf("IN%d
    ",s[i+1]-48);
                      sum += (s[i+1]-48)*weight[loc];
                      //sum += int(s[i+1])*weight[loc];
                   } 
                }          
             }
          }
          else //最后一位是字母
          {
             for(int i = 0; i < n-1 ; i++) //少循环一位
             {
                if (s[i] >= 65 && s[i]<= 90)
                {
                   loc = get_loc(s[i]);
                   if (s[i + 1] >= 65 && s[i + 1]<= 90) //如果下一位是字母
                   {
                      
                      sum += weight[loc];
    
                      printf("1%f
    ", sum);
                   }
                   else  //如果下一位是数字
                   {                  
                      sum += (s[i+1]-48)*weight[loc];
                      printf("2%f
    ", sum);
                   }
                }
             }
             loc = get_loc(s[n-1]);
             sum += weight[loc];
             printf("3%f
    ", sum);
          }
       }
       printf("SUM %f",sum);
       system ("pause");
    }
    
  • 相关阅读:
    Java虚拟机一
    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException
    Java虚拟机
    topcoder srm 663 div1
    topcoder srm 694 div1 -3
    topcoder srm 695 div1 -3
    topcoder srm 696 div1 -3
    topcoder srm 697 div1 -3
    topcoder srm 661 div1
    topcoder srm 698 div1 -3
  • 原文地址:https://www.cnblogs.com/kinologic/p/13931951.html
Copyright © 2011-2022 走看看