如果有一个或多个行或列是由星星填充的,我希望程序说是

I want the program to say yes if there is one or more row or column that is filled by stars

本文关键字:填充 我希望 程序 星星 如果 有一个      更新时间:2023-10-16

如果有一行或多列被星星填满,我希望程序说是,所以我的主要目标是有水平线*或垂直线*或多个

#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;
    }