Answer the question
In order to leave comments, you need to log in
How to solve a sorting problem?
Given an N × N table filled with integers. Peter the Great considers a column to be good if it contains the number X. It is required for each column to find out whether it is good.
Specifications Input
In the first line the number X, not exceeding modulo 2*10^9. The second line contains the number N (1 <= N <= 100). The next N lines contain N integers not exceeding 2*10^9 in absolute value – the numbers in the cells of the table.
Output
For each column print YES if it contains the number X, and NO otherwise. (each answer on a new line)
I wrote this code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int x, n, tmp;
bool m;
cin >> x >> n;
vector<vector<int> > matrix(n); // Обьявление матрицы векторов для хранения данных таблицы
for (int i = 0; i < n; i++){
matrix[i].resize(n); // Расширяем каждый столбец таблицы до значения n
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> matrix[i][j]; // получаем числа на ввод
}
}
for(int i = 0; i < n; i++){
m = 0;
for(int j = 0; j < n; j++){ //пробегаем циклом по столбцам, если в ячейке стобца есть x - выводим YES
if(matrix[i][j] == x){
m = true;
break;
}
}
if(m){
cout << "YES\n";
} else{
cout << "NO\n"; // если в столбце нет x, выводим NO и переходим к следующему столбцу
}
}
}
Answer the question
In order to leave comments, you need to log in
You can do input and check in one cycle with resize, you won't need to go through it twice more.
Also, you make passes along the lines when checking, and not columns
What are the tests? What are the mistakes? What kind of system, after all, that checks all this?
Alternatively, Check the input.
What if the input is not a number but a string, how will your program behave? What if N is greater than 100 or less than 0? Does the program need to protect itself from such input?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question