如果有一个或多个行或列是由星星填充的,我希望程序说是
I want the program to say yes if there is one or more row or column that is filled by stars
如果有一行或多列被星星填满,我希望程序说是,所以我的主要目标是有水平线*或垂直线*或多个
#include <iostream>
using namespace std;
int main()
{
char arr[10][10];
int sum = 0;
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
cin >> arr[i][j];
}
}
for (int x = 0; x <= 9; x++)
{
for (int y = 0; y <= 9; y++)
{
if (arr[x][y] == '*')
{
sum = sum + 1;
}
}
}
if (sum >= 10)
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
这里有一些问题,其中之一是sum变量不准确。您可以继续添加到它,而不会在行/列之间重置它。另一个是,您只在一个方向上进行检查,您需要同时检查行和列。考虑在检测到10颗星后立即返回,以提高你的表现。
for (int x = 0; x <= 9; x++)
{
sum = 0;
for (int y = 0; y <= 9; y++)
{
if (arr[x][y] == '*')
{
sum = sum + 1;
if (sum >= 10)
{
cout << "YES" << endl;
return;
}
}
}
}
您还需要在另一个方向上循环。
我希望这就是你的尝试。我还提供了一个非用户输入的数组版本,这样你就可以使用多种输入类型轻松地查看和测试代码。您需要分别检查行和列。如果一颗恒星不见了,那么那一行或那一列就没有完全被恒星填满。
#include <iostream>
using namespace std;
char arry[10][10] = {
{'*', '*', '*', ' ', '*', '*', '*', '*', '*', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'},
{'*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*'}
};
int main()
{
char arr[10][10];
/*for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
cin >> arr[i][j];
}
}*/
bool rowHasStar = true;
for (int x = 0; x <= 9; x++)
{
rowHasStar = true;
for (int y = 0; y <= 9; y++)
{
if (arry[x][y] != '*')
{
rowHasStar = false;
}
}
if(rowHasStar)
break;
}
bool columnHasStar = true;
for(int y = 0; y <= 9; y++)
{
columnHasStar = true;
for (int x = 0; x <= 9; x++)
{
if (arry[x][y] != '*')
{
columnHasStar = false;
}
}
if(columnHasStar)
break;
}
if (rowHasStar || columnHasStar)
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
-这是我的第一个答案!:D
另一种选择是使用bool
变量:
bool column_is_all_stars = true;
for (unsigned int column = 0; column < 10; ++column)
{
column_is_all_stars = true;
for (unsigned int row = 0; row < 10; ++row)
{
if (arr[column][row] != '*')
{
column_is_all_stars = false;
break; // Column has at least 1 character that is not '*'.
}
}
if (column_is_all_stars)
{
cout << "Column " << column << " is all stars.n";
break; // A column has been found, no more searching required.
}
}
注意:您可能需要将订单切换到arr[column][row]
,具体取决于您的订购方式
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char arr[10][10];
int RowSum = 0;
int colSum = 0;
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++)
cin >> arr[i][j];
for (int x = 0; x <= 9 && colSum<10 && RowSum<10; x++)
{
RowSum = 0; colSum = 0;
for (int y = 0; y <= 9 && colSum < 10 && RowSum < 10; y++){
if (arr[x][y] == '*'){
RowSum = RowSum + 1;
}
if (arr[y][x] == '*'){
colSum = colSum + 1;
}
}
}
if (colSum == 10 || RowSum == 10)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
char c = getchar();
fflush(stdin);
return 0;
}
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我希望定义两个具有相同代码的不同名称的库
- 我希望改进或要求我目前的延迟/睡眠方法。C++
- 视觉我希望一个函数在另一个函数C++中进行计算
- 当我希望 1 时,c ++ int 会变为大约 4198321
- 精灵不显示我希望它们显示的方式
- 告诉编译器我希望变量始终存储在寄存器中的正确方法是什么
- 我希望通过使用模板元编程从变量ARGS中进行剥离参数
- 调用填充我的主体数组的函数不起作用
- 我希望在字符串功能上提供一些帮助
- 在Mac OSX中,文本的大小比使用Qt的Windows小。我希望到处都有相同的尺寸
- 我希望循环运行直到数字为:123
- 我希望QT-App看起来像QT-App,而不是Android-native
- 无法使我的getChar()函数能够工作,我希望它能正常工作,输出IS10而不是2 C
- 制作主机游戏.我希望游戏在他们获得 0 点生命值时结束
- 例外情况下,我希望在没有任何堆栈展开的情况下进行调试
- 当我运行此代码时,它"stopped working"。我希望从 API 调用这个简单的函数
- 如何用c/c++填充我的md5消息
- 操作员过载.q = q1+q2,q1 正在修改,但我希望 q1 和 q2 完好无损
- 如果有一个或多个行或列是由星星填充的,我希望程序说是