zoukankan      html  css  js  c++  java
  • ZOJ2812------2015年2月4日

    这道题很简单,但是涉及字符串的读取及常见处理。

    下面我贴出不同风格的代码:

    这个题目重要的就是如何读取有空格的字符串。

    代码1(我自己写的)

    #include<iostream>
    #include<cstdio>
    #include<string.h>
    using namespace std;
    char s[1000];
    int main()
    {
        while(gets(s))
        {
            int sum=0;
           if(s[0]=='#') break;
           int len=strlen(s);
           for(int i=0;i<len;i++)
           {
               if(s[i]!=' ')
               {
                   sum+=(i+1)*(s[i]-'A'+1);
               }
           }
           cout<<sum<<endl;
        }
        return 0;
    }

    代码2:

    #include<iostream>
    #include<fstream>
    #include<cstdio>
    #include<string.h>
    using namespace std;
    int main()
    {
        ifstream cin("aa.txt");
        char  ch[256];
        int i=1;
        int sum=0;
        //cin.get()一个一个读,就不会忽略任何字符
        //采用cin.getline()可以一行一行读取字符
        while(cin.getline(ch,256))
        {
            if(ch[0]!='#') break;
            for(int i=0;ch[i]!='';i++)
                if(ch[i]!=' ') sum+=(i+1)*(ch[i]-64);
            cout<<sum<<endl;
            sum=0;
        }
        return 0;
    }
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/khbcsu/p/4271676.html
Copyright © 2011-2022 走看看