ACM刷題打卡-151215

UVa 272 - TEX Quotes

#include <iostream>
using namespace std;
int main() {
    string line;
    bool isleft = true;
    while(getline(cin, line)){
        for(int i = 0; i < line.length(); i++){
            if(line[i] == '"'){
                if(isleft)
                    cout << "``";
                else
                    cout << "''";
                isleft = !isleft;
            }
            else
                cout << line[i];
        }
        cout << endl;
    }
    return 0;
}

水題。字符替換。

UVa 10082 - WERTYU

#include <iostream>
#include <string>
using namespace std;

int main() {
    const string letter = {"`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./"};
    string line;
    int i , j;
    while(getline(cin, line)){
        for(i = 0; i < line.length(); i++){
            for(j = 0; j < letter.length(); j++){
                if(line[i] == letter[j]){
                    cout << letter[j - 1];
                    break;
                }
            }
            if(j >= letter.length()){
                cout << line[i];
            }
        }
        cout << endl;
    }
    return 0;
}

第一次提交WA,對著書檢查發(fā)現(xiàn)是字符數(shù)組存在問題。用數(shù)組的方式來解決這類非常龐大的鍵盤鍵位替換問題真是一種好方法……

UVa 401 - Palindromes

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

const string letter = {"A   3  HIL JM O   2TUVWXY51SE Z  8 "};
const string msg[] = {  " -- is not a palindrome.",
                        " -- is a regular palindrome.",
                        " -- is a mirrored string.",
                        " -- is a mirrored palindrome." };

char tomir(char c) {
    if(isalpha(c))
        return letter[c - 'A'];
    return letter[c - '0' + 25];
}

int main() {
    string line;
    int i , j;
    while(getline(cin, line)){
        int len = line.length();
        int p = 1, m = 1;
        for(i = 0; i < (len + 1) / 2; i++){
            if(line[i] != line[len - i - 1])
                p = 0;
            if(tomir(line[i]) != line[len - i - 1])
                m = 0;
        }
        cout << line << msg[p + m * 2] << endl;
        cout << endl;
    }
    return 0;
}

這道題目要總結(jié)的東西太多了,感覺都可以拿出來單獨寫……首先思路上我是完全參考書本,看完題目之后想過許多方案,但是范例的解答實在太讓人佩服。把握整體之后自己碼了一遍。
字符串letter存儲的即是26個字母和9個數(shù)字的鏡像,沒有以空格替代;
字符串數(shù)組msg存儲的是題目要求輸出的四句話,最后以非常巧妙的方式簡化了輸出;
值得注意的是以上均為全局變量;
函數(shù)tomir將字符轉(zhuǎn)化為對應的鏡像字符;
cctype下包含的多個函數(shù)需要了解。

UVa 1586 - Molar mass

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;

double mass(char c){
    if(c == 'C') return 12.01;
    else if(c == 'H') return 1.008;
    else if(c == 'N') return 14.01;
    else if(c == 'O') return 16.0;
    else return 0;
}
int toInt(char c){
    return c - '0';
}

int main() {
    int t, i;
    cin >> t;
    getchar();
    while(t--){
        double total = 0, tmp = 0;
        int cnt = 0;

        string line;
        getline(cin, line);

        for(i = 0; i < line.length(); ++i){
            if(isalpha(line[i])){
                if(cnt != 0){
                    total += cnt * tmp;
                    cnt = 0;
                    tmp = 0;
                }
                if(isdigit(line[i + 1]))
                    tmp += mass(line[i]);
                else
                    total += mass(line[i]);
                //cout << cnt << " " << tmp << " " << total << endl;
            }
            else if(isdigit(line[i])){
                if(isdigit(line[i - 1])){ continue; }
                else if(isdigit(line[i + 1])){
                    cnt = toInt(line[i]) * 10 + toInt(line[i + 1]);
                }
                else{
                    cnt = toInt(line[i]);
                }
                //cout << cnt << " " << tmp << " " << total << endl;
            }
        }
        if(cnt == 0)
            total += tmp;
        else
            total += cnt * tmp;


        printf("%.3f\n",total);
    }
    return 0;
}

求化學式的原子量。值得注意的是在有限量輸入后的getline之前,要把數(shù)組組數(shù)后面的換行符吃掉,使用getchar()需包含cstdio

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • (轉(zhuǎn)載者)【邏輯引擎】簡序:雖然關(guān)于超限數(shù)的一些理論(特別是大基數(shù))遭到某些直覺主義者或構(gòu)造主義者的詬病,但對我個...
    guozhao1985閱讀 956評論 0 1
  • 來源:NumPy Tutorial - TutorialsPoint 譯者:飛龍 協(xié)議:CC BY-NC-SA 4...
    布客飛龍閱讀 33,003評論 6 98
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,270評論 0 4
  • 我對能量來源外傾的認識原來一直停留在淺層,比如外向,開朗,人緣不錯等,最近一個月,體征之后有了另外的一番解讀! 分...
    胡永群閱讀 598評論 0 50
  • 謝唯就此便在錦江飯店住下,帶了他十歲的弟弟謝正,姐弟倆從此有了一個安穩(wěn)的住處。 董君竹將謝正送進了學校,雖說十歲才...
    延緩厭倦閱讀 325評論 0 0