欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVa272,UVa401,UVa10082

程序员文章站 2024-03-19 08:24:28
...

UVa272 Tex Quotes

用来熟悉输入输出的题,给一段文字,用getchar一个一个读并判断。

使用q作为标志,碰到第一个双引号,变换一下输出,q取反

碰到第二个双引号,变换输出,q取反

代码:

#include <iostream>

using namespace std;

int main()
{
    bool q = 0;
    char it;
    while((it=getchar())!=EOF)
    {
        if (it == '"')
        {
            if (q == 0)
                cout << "``";
            else
                cout << "''";
            q = !q;
        }
        else
        {
            cout << it;
        }
    }
    return 0;
}

UVa401 Palindromes

回文串与镜像串的判断
对于回文串的判断比较简单,字符串头和尾比较然后往中间收缩即可
我写了这个函数用来判断回文

bool isPalindromes(const string &judge)
{
    int i = 0;
    int len = (int)judge.size();
    while (i < ((len+1) / 2))
    {
        if (judge[i] == judge[judge.size() - i - 1])
        {
            i = i + 1;
        }
        else
        {
            return false;
        }
    }
    return true;
}

对于镜像串来说,比较巧妙的是用了常量字符数组,定义翻转函数,先列出A-Z和1-9的镜像字符,输入要判断的字符,做翻转,返回翻转后的字符然后与要判断的字符串相对应的位置比较

翻转函数:

char reverse(char judgeChar)
{
    const string alphaRev = "A   3  HIL JM O   2TUVWXY5";
    const string numRev = "1SE Z  8 ";
    if (isalpha(judgeChar))
    {
        return alphaRev[judgeChar - 'A'];
    }
    else
    {
        return numRev[judgeChar - '1'];
    }
}

判断镜像串的函数:

bool isMirrored(const string &judge)
{
    int i = 0;
    int len = (int)judge.size();
    while (i < ((len + 1) / 2))
    {
        if (reverse(judge[i]) == judge[judge.size() - i - 1])
        {
            i = i + 1;
        }
        else
        {
            /*if ((judge[i] == '0'&&judge[judge.size() - i - 1] == 'O') || (judge[i] == 'O'&&judge[judge.size() - i - 1] == '0'))
                i = i + 1;
            else*/
                return false;
        }
    }
    return true;
}

因为题目要求输出非回文,回文非镜像,镜像非回文,镜像回文四种情况,所以就做个判断,分别调用以上函数就好了

整体代码如下

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

char reverse(char judgeChar)
{
    const string alphaRev = "A   3  HIL JM O   2TUVWXY5";
    const string numRev = "1SE Z  8 ";
    if (isalpha(judgeChar))
    {
        return alphaRev[judgeChar - 'A'];
    }
    else
    {
        return numRev[judgeChar - '1'];
    }
}

bool isPalindromes(const string &judge)
{
    int i = 0;
    int len = (int)judge.size();
    while (i < ((len+1) / 2))
    {
        if (judge[i] == judge[judge.size() - i - 1])
        {
            i = i + 1;
        }
        else
        {
            /*if ((judge[i] == '0'&&judge[judge.size() - i - 1] == 'O') || (judge[i] == 'O'&&judge[judge.size() - i - 1] == '0'))
                i = i + 1;
            else*/
            return false;
        }
    }
    return true;
}
bool isMirrored(const string &judge)
{
    int i = 0;
    int len = (int)judge.size();
    while (i < ((len + 1) / 2))
    {
        if (reverse(judge[i]) == judge[judge.size() - i - 1])
        {
            i = i + 1;
        }
        else
        {
                return false;
        }
    }
    return true;
}

void isPalindromesOutput(const string &output)
{
    cout << output << " -- is a regular palindrome."<<endl;
}

void notPalindromesOutput(const string &output)
{
    cout << output << " -- is not a palindrome."<<endl;
}

void isMirroredOutput(const string &output)
{
    cout << output << " -- is a mirrored string."<<endl;
}

void isMirAndPalOutput(const string &output)
{
    cout << output << " -- is a mirrored palindrome."<<endl;
}

int main()
{
    string needJudge;
    while (cin >> needJudge)
    {
        if (isPalindromes(needJudge))
        {
            if (isMirrored(needJudge))
            {
                isMirAndPalOutput(needJudge);
            }
            else
            {
                isPalindromesOutput(needJudge);
            }
        }
        else
        {
            if (isMirrored(needJudge))
            {
                isMirroredOutput(needJudge);
            }
            else
            {
                notPalindromesOutput(needJudge);
            }
        }
        cout << endl;
    }
    //system("pause");
    return 0;
}

UVA10082 WERTYU

这题也是常量字符的妙用,边输入,边判断,边输出,但要注意转义的问题,’\’字符特殊,需要变为‘\’
代码如下:

#include <iostream>

using namespace std;

const char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";//注意转义问题
int main()
{
    char it;
    while ((it = getchar()) != EOF)
    {
        int i = 0;
        while (s[i] != it&&i<=46)
            i++;
        if (s[i])
        {
            cout<<s[i - 1];
        }
        else
        {
            cout<<it;
        }
    }
    return 0;
}

上一篇: SSL P1861 无限序列

下一篇: