几个UVa水题 ,紫书第三章例题1、2、3,UVa 272、10082、401, 提交ac了的

来源:互联网 发布:seo 百度移动搜索收录 编辑:程序博客网 时间:2024/05/01 19:48
例题3-1
//UVA - 272//TEX Quotes//Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu//Submit// //Status////Description//TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use `` and " to delimit quotations, rather than the mundane " which is what is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote ` and a right-single-quote '. Check your keyboard now to locate the left-single-quote key ` (sometimes called the ``backquote key") and the right-single-quote key ' (sometimes called the ``apostrophe" or just ``quote"). Be careful not to confuse the left-single-quote ` with the ``backslash" key \. TeX lets the user type two left-single-quotes `` to create a left-double-quote `` and two right-single-quotes '' to create a right-double-quote ''. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".////If the source contained////"To be or not to be," quoth the bard, "that is the question."////then the typeset document produced by TeX would not contain the desired form:////``To be or not to be," quoth the bard, ``that is the question."////In order to produce the desired form, the source file must contain the sequence:////``To be or not to be,'' quoth the bard, ``that is the question.''////You are to write a program which converts text containing double-quote (") characters into text that is identical except that double-quotes have been replaced by the two-character sequences required by TeX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by either `` if the " opens a quotation and by '' if the " closes a quotation. Notice that the question of nested quotations does not arise: The first " must be replaced by ``, the next by '', the next by ``, the next by '', the next by ``, the next by '', and so on.////Input//Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character.////Output//The text must be output exactly as it was input except that:////the first " in each pair is replaced by two ` characters: `` and//the second " in each pair is replaced by two ' characters: ''.//Sample Input//"To be or not to be," quoth the Bard, "that//is the question".//The programming contestant replied: "I must disagree.//To `C' or not to `C', that is The Question!"//Sample Output//``To be or not to be,'' quoth the Bard, ``that//is the question''.//The programming contestant replied: ``I must disagree.//To `C' or not to `C', that is The Question!''#include<stdio.h>int main(){int c, q = 1;while((c = fgetc(stdin)) != EOF) {if(c == '"') { printf("%s", q ? "``" : "''"); q = !q; }else printf("%c", c); }return 0;}
例题3-2
//10082 WERTYU //A common typing error is to place the hands on//the keyboard one row to the right of the correct//position. So ‘Q’ is typed as ‘W’ and ‘J’ is typed//as ‘K’ and so on. You are to decode a message//typed in this manner.//Input//Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except//Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp,//Control, etc.] are not represented in the input.//Output//You are to replace each letter or punction symbol by the one immediately to its left on the ‘QWERTY’//keyboard shown above. Spaces in the input should be echoed in the output.//Sample Input//O S, GOMR YPFSU///Sample Output//I AM FINE TODAY.#include<stdio.h>char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";int main() {int i, c;while((c = getchar()) != EOF) {for (i=1; s[i] && s[i] !=c; i++);{if (s[i]) putchar(s[i-1]);else putchar(c);}}return 0;}
例题3-3(我不知道刘汝佳大神哪里错了= =)
<pre name="code" class="cpp">//UVA - 401 课本上的提交了不能ac, 这个是搜的, 用了<string> 和 <algorithm> 来使用c++的reverse()//reverse函数的作用是:反转一个容器内元素的顺序。函数参数:reverse(first,last);//first为容器的首迭代器,last为容器的末迭代器。它没有任何返回值。//题目: //Palindromes//Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu//Submit// //Status////Description//A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.//// //////A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I" are their own reverses, and "3" and "E" are each others' reverses.//////A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.////Of course,"A","T", "O", and "Y" are all their own reverses.////A list of all valid characters and their reverses is as follows.////CharacterReverseCharacterReverseCharacterReverse//AAMMYY//B N Z5//C OO11//D P 2S//E3Q 3E//F R 4 //G S25Z//HHTT6 //IIUU7 //JLVV88//K WW9 //LJXX  ////Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.////Input//Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.////Output//For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.////STRINGCRITERIA//" -- is not a palindrome."if the string is not a palindrome and is not a mirrored string//" -- is a regular palindrome."if the string is a palindrome and is not a mirrored string//" -- is a mirrored string."if the string is not a palindrome and is a mirrored string//" -- is a mirrored palindrome."if the string is a palindrome and is a mirrored string//Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.////In addition, after each output line, you must print an empty line.////Sample Input//NOTAPALINDROME //ISAPALINILAPASI //2A3MEAS //ATOYOTA//Sample Output//NOTAPALINDROME -- is not a palindrome.// //ISAPALINILAPASI -- is a regular palindrome.// //2A3MEAS -- is a mirrored string.// //ATOYOTA -- is a mirrored palindrome.// UVA 401.cpp#include <cstdio>#include <string>   //reverse();#include <algorithm>//#include <iostream>using namespace std;int main(){//freopen("input.txt","r",stdin);    string s;    char m[] = {'A','E','H','I','J','L','M','O','S','T','U','V','W','X','Y','Z','1','2','3','5','8'};    char mr[]= {'A','3','H','I','L','J','M','O','2','T','U','V','W','X','Y','5','1','S','E','Z','8'};    while(cin>>s)    {        string a=""; string p = s; int c=0;        for(int i=0; i<s.size(); i++)        {            for(int j=0; j<=20; j++)            {                if(s[i]==m[j])                {                    a+=mr[j]; break;                }                else if(s[i]==mr[j])                {                    a+=m[j]; break;                }            }        }       reverse(p.begin(),p.end());        if(p==a)        {           if(s==a) cout<<s<<" -- is a mirrored palindrome."<<endl;           else cout<<s<<" -- is a mirrored string."<<endl;        }        else        {            if(p==s) cout<<s<<" -- is a regular palindrome."<<endl;            else cout<<s<<" -- is not a palindrome."<<endl;        }        cout<<endl;    }    return 0;}

附上课本上的: 提交了WA, 测试用的结果是一样的, 不知道具体哪里不对

//UVA - 401 Ìá½»²»ÄÜac£¬×Ô¼º²âÊÔ²»³ö´íÎó //Palindromes//Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu//Submit// //Status////Description//A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.//// //////A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I" are their own reverses, and "3" and "E" are each others' reverses.//////A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.////Of course,"A","T", "O", and "Y" are all their own reverses.////A list of all valid characters and their reverses is as follows.////CharacterReverseCharacterReverseCharacterReverse//AAMMYY//B N Z5//C OO11//D P 2S//E3Q 3E//F R 4 //G S25Z//HHTT6 //IIUU7 //JLVV88//K WW9 //LJXX  ////Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.////Input//Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.////Output//For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.////STRINGCRITERIA//" -- is not a palindrome."if the string is not a palindrome and is not a mirrored string//" -- is a regular palindrome."if the string is a palindrome and is not a mirrored string//" -- is a mirrored string."if the string is not a palindrome and is a mirrored string//" -- is a mirrored palindrome."if the string is a palindrome and is a mirrored string//Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.////In addition, after each output line, you must print an empty line.////Sample Input//NOTAPALINDROME //ISAPALINILAPASI //2A3MEAS //ATOYOTA//Sample Output//NOTAPALINDROME -- is not a palindrome.// //ISAPALINILAPASI -- is a regular palindrome.// //2A3MEAS -- is a mirrored string.// //ATOYOTA -- is a mirrored palindrome.//palindrome#include<cstdio>#include<cstring>#include<ctype.h>const char* rev = "A   3  HIL JM O   STUVWXY51SE Z  8 ";const char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"};char r(char ch) {if(isalpha(ch)) return rev[ch - 'A'];return rev[ch - '0' + 25];}int main() {freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);char s[30];while(scanf("%s", s) == 1) {int len = strlen(s);int p = 1, m = 1;for (int i = 0; i < (len+1)/2; ++i) {if(s[i] != s[len-1-i]) p = 0;//!palindromeif(r(s[i]) != s[len-1-i]) m = 0;//!mirrored}printf("%s -- is %s.\n\n", s, msg[m*2+p]);}return 0;}


0 0
原创粉丝点击