两道UVaOJ(489、490)ACM疑问求解~ 为什么不能AC?

neicole 2012-04-04 02:51:36
表示很悲剧地说,一道未想到,另外一道的问题又来了..
输入输出怎么看都与Example的一样,然后,看了网上几个例子,思路也基本一致,但是就是不能AC~ 
有注释,有分析思路,求大侠为我看一下程序~

490_RotatingSentences.cpp
// 490_RotatingSentences.cpp

/**
490 - Rotating Sentences
Time limit: 3.000 seconds

Rotating Sentences
In ``Rotating Sentences,'' you are asked to rotate a series of input sentences 90 degrees clockwise.
So instead of displaying the input sentences from left to right and top to bottom,
your program will display them from top to bottom and right to left.

Input and Output
As input to your program, you will be given a maximum of 100 sentences, each not exceeding 100 characters long.
Legal characters include: newline, space, any punctuation characters, digits, and lower case or upper case English letters.
(NOTE: Tabs are not legal characters.)
The output of the program should have the last sentence printed out vertically in the leftmost column;
the first sentence of the input would subsequently end up at the rightmost column.

Sample Input
Rene Decartes once said,
"I think, therefore I am."

Sample Output
"R
Ie
n
te
h
iD
ne
kc
,a
r
tt
he
es
r
eo
fn
oc
re
e
s
Ia
i
ad
m,
.
"
**/

/**
* 题目分析:
* 这题的题目要求(90 degrees clockwise)将输入的字符组顺时针旋转90度,
* 思路很简单,定义二维数组,输入的时候 先行->后列 输入,输出的时候 先列 -> 后行 输出。
* 下面程序采用了获取字符保存于二维字符数组,然后再将二维数组中的字符提取输出的方法。
* 在输入二组数组的同时,还对数组的行列大小做了一个记录,
* 使用lineMaxNumber记录一行里面最大的列数(此时不含下标0),这样,就可以减少输出时循环的次数,
* 使用rowArray[]记录每一行的列数,当顺时针90度输出时,将列数与这个每行的列数比较,当其超过记录数,输出空格。
* 使用rowMax记录总行数(含0下标),这样就可以知道整个矩阵的大小了,方便输出的时候可以从最后行开始输出。
**/


#include <cstdio>
using namespace std;

char cArray[120][120];
int rowArray[120] = {0};
int lineMaxNumber = 0;

int main()
{
int row = 0;
int column = 0;
char c;
while(EOF != (c = getchar())){
// if ('\\' == c) break;
if ('\n' == c){ // 遇上换行符时
column = 0; // 列数清零
++row; // 行数加1
}
else{
cArray[row][column++] = c;
++rowArray[row]; // 记录行的字符数(一行有多少列元素)
if (rowArray[row] > lineMaxNumber){
lineMaxNumber = rowArray[row];
}
}
}
const int rowMax = row; // 记录最大行数(这里包含了下标0)
// 开始timewise(顺时针)旋转90度输出
for (int column = 0; column < lineMaxNumber; ++column){
for (row = rowMax; row >= 0; --row){
c = column < rowArray[row] ? cArray[row][column]: ' ';
// c = !cArray[row][column] ? ' ': cArray[row][column];
printf("%c", c);
}
printf("\n");
// printf("BBB\n");
}
return 0;
}


// 489_HangmanJudge.cpp

/**
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 0. Getting Started
489 - Hangman Judge
Time limit: 3.000 seconds

Hangman Judge
In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games.
For each game, the answer to the puzzle is given as well as the guesses.
Rules are the same as the classic game of hangman, and are given as follows:
1. The contestant tries to solve to puzzle by guessing one letter at a time.
2. Every time a guess is correct, all the characters in the word that match the guess will be ``turned over.''
For example, if your guess is ``o'' and the word is ``book'', then both ``o''s in the solution will be counted as ``solved.''
3. Every time a wrong guess is made, a stroke will be added to the drawing of a hangman, which needs 7 strokes to complete.
Each unique wrong guess only counts against the contestant once.
4. ______
5. | |
6. | O
7. | /|\
8. | |
9. | / \
10. __|_
11. | |______
|_________|
12. If the drawing of the hangman is completed before the contestant has successfully guessed all the characters of the word,
the contestant loses.
13. If the contestant has guessed all the characters of the word before the drawing is complete,
the contestant wins the game.
14. If the contestant does not guess enough letters to either win or lose, the contestant chickens out.
Your task as the ``Hangman Judge'' is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.

Input
Your program will be given a series of inputs regarding the status of a game. All input will be in lower case.
The first line of each section will contain a number to indicate which round of the game is being played;
the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant.
A round number of -1 would indicate the end of all games (and input).

Output
The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game.
There are three possible results:
You win.
You lose.
You chickened out.

Sample Input
1
cheese
chese
2
cheese
abcdefg
3
cheese
abcdefgij
-1

Sample Output
Round 1
You win.
Round 2
You chickened out.
Round 3
You lose.
**/

/**
* 题目大意:
*   先输入第几回合(改变一下格式输出,对计算无影响),再输入两个单词,假设第一个单词叫做first,
* 第二个单词叫做second,那么,将second中的每个字符与first比较,假如,second单词能从下标最小到最大
* 与first中的单词字符比较:
* 1. 不管是first还是second,只要有重复的字符,都不列入比较次数内。
* 2. 假如在比较过程中,比较了七次或以上还是没有将first的全部字符比较出来(即second中有first中的全部字母)。
* 那么,就输出 You lose.
* 3. 如果在七次内,将全部字符比较出来,就输出You win.
* 4. 如果在七次内,但又未将first中的全部字符比较出来,second就遍历完了,那么就输出 You chickened out.
*
* 题目分析:
* 1. 先按要求,输入三个数据,int, string1, string2;
* 2. 去除string1和string2的重复字母。
* 3. 将string2中的字符(从下标最小到最大)逐个与string1中的字符比较,查看是否有相同字符。
* 存在相同字符:win计数加1, 如果win达到string1的长度,输出 You win. 退出该循环,继续下一轮比赛。
* 没有相同字符:lose计数加1,如果lose达到7,输出 You lose.通出循环,继续下一轮比赛。
* 4. 比较结束后,还会有一种情况,当win计数未达string1的长度且lose计数小于7时,输出 You chickened out.
* 5. 注意:当round == -1 时,结束程序。
**/

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

int main()
{
int round;
while(scanf("%d", &round) && round != -1){
printf("Round %d\n", round); // 按题目要求,打印“第几轮比赛”
string hangman;
string contestant;
cin >> hangman >> contestant; // 继续输入,两个字符串

string deleteTheSameInString(string); // 删除重复字符
hangman = deleteTheSameInString(hangman);
contestant = deleteTheSameInString(contestant);

const int nh = hangman.length();
const int nc = contestant.length();

int winCount = 0;
int loseCount = 0;
for(int i = 0; i < nc; ++i){
bool noSame = true;
for (int j = 0; j < nh; ++j){ // contesant的单个字母与hangman内的全部字符比较
if (contestant[i] == hangman[j]){
if (++winCount == nh){
printf("You win.\n");
}
noSame = false;
break;
}
}
if (noSame){
if(7 <= ++loseCount){ // 超出猜的机会
printf("You lose.\n");
break;
}
}
}
if (winCount != nh && 7 > loseCount){ // 未全部猜出而且还有猜的机会
printf("You chickened out.\n");
}
}
return 0;
}

string deleteTheSameInString(string source)
{
string copyString;
int n = source.length();
for (int i = 0; i < n; ++i){
if(' ' != source[i]){ // 非空白字符则将其加入字符串
copyString.push_back(source[i]);
}
else{
continue; // 发现空白字符,可以继续下一轮的循环
}
int copyStringLastIndex = copyString.length() - 1;
for (int j = i + 1; j < n; ++j){
if (copyString[copyStringLastIndex] == source[j]){
source[j] = ' '; // 发现重复的字符则将其换为' '
}
}
}
return copyString;
}
...全文
425 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
neicole 2012-04-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
第一题:for (row = rowMax; row >= 0; --row){
改为:for (row = rowMax-1; row >= 0; --row){
[/Quote]

[Quote=引用 3 楼 的回复:]
引用 1 楼 的回复:
第一题:for (row = rowMax; row >= 0; --row){
改为:for (row = rowMax-1; row >= 0; --row){

谢谢2楼了,太强大了,说中了,不过,还真想不明白是什么时候多了一行,3楼的说法是错在哪里呢?

能再帮忙看看第二题么?
[/Quote]

这题明白啦,我猜是这样的,EOF的位置是在一个换行的下面,才会出现这种情况,而我们在测试的时候,以为它以EOF作结束符。。。

即:
我的程序假设系统数据:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXx\n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXEOF
而实际上,系统的数据:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXx\n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n
EOF
neicole 2012-04-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
第一题:for (row = rowMax; row >= 0; --row){
改为:for (row = rowMax-1; row >= 0; --row){
[/Quote]
谢谢2楼了,太强大了,说中了,不过,还真想不明白是什么时候多了一行,3楼的说法是错在哪里呢?

能再帮忙看看第二题么?
neicole 2012-04-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
第一题:for (row = rowMax; row >= 0; --row){
改为:for (row = rowMax-1; row >= 0; --row){
[/Quote]

好像与这里无关,因为row初始化时是0,然后,它是每增加一行,才加1的,在计算时,已经是包含下标0,
也就是说,如果要计算行数的话,row = rowMax + 1;
这个程序关于row是:输入第一行, row = 0;
按下了回车,输入第二行, row = 1;
.
.
.
昵称很不好取 2012-04-04
  • 打赏
  • 举报
回复
第一题:for (row = rowMax; row >= 0; --row){
改为:for (row = rowMax-1; row >= 0; --row){

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧