I need to do a pyramid with stars and dots. The first half of the code is doing correct but the other half is doing wrong. I have tried this.
The input is: size=4 and n = 7
the output is:
*...
**..
***.
****
.***
..**
...*
I want it to be
*...
**..
***.
****
***.
**..
*...
#include < iostream >
using namespace std;
int main() {
int size, n;
cin >> size;
cin >> n;
for (int R = 1; R <= size; R++) {
if (R > n) {
break;
}
for (int i = 1; i <= size; i++) {
if (i > R) {
cout << ".";
} else {
cout << "*";
}
}
cout << endl;
}
for (int R = 1; R <= size - 1; R++) {
if (R > n) {
break;
}
for (int i = 1; i <= size; i++) {
if (i > R) {
cout << "*";
} else {
cout << ".";
}
}
cout << endl;
}
return 0;
}
Related
My code is trying to find the beginning and ending indices of a section of a matrix that when added, together would equal 20. For each instance this occurs, it would then populate a matrix with said beginning and end indices in the format {beginning row index, beginning column index, ending row index, ending column index} for each row. Each row would represent separate instances. It works fine for one instance but when introduced to other instances it wouldn't populate the matrix. Please help.
#include <cstddef> // size_t
#include <iostream>
using namespace std;
// Populates matrix
void filler(int bIndr, int bIndc, int eIndr, int eIndc, size_t**matrix, const size_t kIndices_size2, const size_t kIndices_size) {
int t = 0;
int matrix2[4] = {0,0,0,0};
for(int i = 0 ; i < kIndices_size2; i++) {
for (int j = 0; j < 2; j++) {
for (int ii = t; ii < kIndices_size; ii++) {
if(j == 0) {
matrix2[ii] = bIndr;
matrix2[ii+1] = bIndc;
cout << matrix2[ii+1] << endl;
break;
}
if(j == 1) {
matrix2[ii] = eIndr;
matrix2[ii+1] = eIndc;
cout << matrix2[ii+1] << endl;
break;
}
}
t = 2;
}
}
for(int i = 0 ; i < kIndices_size; i++) {
matrix[kIndices_size2-1][i] = matrix2[i];
}
}
int main()
{
int goal = 20;
int array[2][8] = {{10,0,0,10,0,0,1,0},{0,0,10,0,0,0,10,0}};
int inst = 0;
int t=0;
int bIndr = 0;
int bIndc = 0;
int eIndr = 0;
int eIndc = 0;
const size_t kIndices_size = 4;
size_t**matrix;
for(int ii = 0; ii < 2; ii++) {
bIndc =0;
for(int j = bIndc; j < 8; j++) {
t = 0;
bIndr = ii;
bIndc = j;
for(int i = j; i < 8; i++) {
t += array[ii][i];
if((goal-t) == 0) {
inst++;
eIndc = i;
eIndr = ii;
matrix=new size_t*[inst];
matrix[inst-1]=new size_t [kIndices_size];
cout << bIndr << bIndc << eIndr << eIndc << endl;
filler(bIndr, bIndc, eIndr, eIndc, matrix, inst, kIndices_size);
break;
}
}
}
}
size_t actual_size = static_cast<size_t>(-1);
cout << actual_size << endl;
size_t* sums_found = &actual_size;
*sums_found = inst;
cout << actual_size << endl;
cout << matrix[0][0] << endl;
for(int i = 0; i < inst; i++) {
for(int ii = 0; ii < kIndices_size; ii++) {
cout << matrix[i][ii] << " ";
}
cout << endl;
}
return 0;
}
https://cses.fi/problemset/task/1649
I'm solving this problem using Segment Trees and the solution I've written is
#include <bits/stdc++.h>
#define MAX 1000000001
using namespace std;
int n;
vector<int> tree;
int sum(int a, int b)
{
a += n;
b += n;
int s = INT_MAX;
while(a <= b) {
if (a % 2 == 1) s = min(s, tree[a++]);
if (b % 2 == 0) s = min(s, tree[b--]);
a>>=1;
b>>=1;
}
return s;
}
void update(int k, int change)
{
k += n;
tree[k] = change;
for(int i = k>>1; i >= 1; i>>=1) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
return;
}
int main()
{
int q;
cin >> n >> q;
n = pow(2, ceil(log2(n)));
tree.resize(2*n, INT_MAX);
for(int i = 0; i < n; i++) {
cin >> tree[i+n];
}
for(int i = n-1; i >= 1; i--) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
int type, a, b;
for(int i = 0; i < q; i++) {
cin >> type >> a >> b;
if (type == 1) {
update(a-1, b);
} else {
cout << sum(a-1, b-1) << endl;
}
}
return 0;
}
It works with first test case, but not with the second one. I've looked at other solutions online and they all look similar to mine. Please, help me spot the mistake.
Problem: Count the number of ways to place K Bishops on N*N chess board.
https://onlinejudge.org/external/8/861.pdf
I came up with the below backtracking solution but it is not fast enough. I do not understand the DP solution recommended on other sites. Can you please help.
In the below code I am exhaustively searching for all solutions using a recursive backtracking approach.
#include <bits/stdc++.h>
using namespace std;
using Row = vector<bool>;
using Board = vector<Row>;
Board gBoard;
bool isOk(int n, int r, int c) {
for(int i = r-1, j = c-1; i >= 0 && j >= 0; --i, --j) {
if(gBoard[i][j]) {
return false;
}
}
for(int i = r+1, j = c+1; i < n && j < n; ++i, ++j) {
if(gBoard[i][j]) {
return false;
}
}
for(int i = r-1, j = c+1; i >= 0 && j < n; --i, ++j) {
if(gBoard[i][j]) {
return false;
}
}
for(int i = r+1, j = c-1; i < n && j >= 0; ++i, --j) {
if(gBoard[i][j]) {
return false;
}
}
return true;
}
void trace(const string &msg) {
cout << msg << "\n";
for(int i = 0; i < gBoard.size(); ++i) {
for(int j = 0; j < gBoard[i].size(); ++j) {
cout << gBoard[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void dfs(int n, int k, int r, int c, int cnt, int &sum) {
if(cnt == k) {
//trace("OK");
++sum;
return;
}
if(c >= n) {
c = 0;
r += 1;
}
int j = c;
for(int i = r; i < n; ++i) {
for( ; j < n; ++j) {
if(gBoard[i][j]) {
continue;
}
gBoard[i][j] = true;
if(isOk(n, i,j)) {
//trace("tmp");
dfs(n, k, i, j+1, cnt+1, sum);
}
gBoard[i][j] = false;
}
j = 0;
}
}
void solve(int n, int k) {
gBoard.assign(n, Row(n, false));
int sum = 0;
dfs(n, k, 0, 0, 0, sum);
cout << sum << "\n";
}
int main() {
while(true) {
int n,k;
cin >> n >> k;
if(!n && !k) {
break;
}
solve(n, k);
}
return 0;
}
Thank you
For some reason, whenever I run this code, it just opens; loads for a sec; then closes without doing anything. Whenever I try to narrow it down to a piece of code, it makes absolutely no sense, like the line int dirX.
#include <iostream>
#include <queue>
using namespace std;
void solve()
{
// ENTER CODE BELOW
struct Loc
{
int x, y;
Loc (int xx=0, int yy=0) : x(xx), y(yy) {}
};
int n=0, currX=1002, currY=1002, dx[]={-1,1,0,0},dy[]={0,0,-1,1}; string str=""; bool isFence[2010][2010]; queue<Loc> q;
int ret=-1;
for (int i = 0; i < 2005; i++) {
for (int j = 0; j < 2005; j++) {
isFence[i][j]=false;
}
}
cin >> n >> str;
isFence[currX][currY]=true;
int dirX, dirY;
for (auto i : str)
{
dirX=0; dirY=0;
if (i=='N') dirX=-1;
else if (i=='S') dirX=1;
else if (i=='W') dirY=-1;
else dirY=1;
for (int j = 0; j < 2; j++) {
currX += dirX;
currY += dirY;
isFence[currX][currY]=true;
}
}
Loc curr; int nx, ny;
for (int i = 0; i < 2005; i++)
{
for (int j = 0; j < 2005; j++)
{
cout << isFence[i][j] << endl;
if (isFence[i][j]) continue;
ret++;
q = std::queue<Loc>();
q.push(Loc(i,j));
isFence[i][j]=true;
while (!q.empty())
{
curr = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
nx = curr.x+dx[k]; ny=curr.y+dy[k];
if (nx >= 0 && nx < 2005 && ny >= 0 && ny<2005 && !isFence[nx][ny]) {
isFence[nx][ny]=true;
q.push(Loc(nx, ny));
}
}
}
}
}
cout << ret;
// ENTER CODE ABOVE
}
int main()
{
solve();
}
Also, the reason I have all my code in the solve() function was because this is an assignment and I have to do it this way.
Sidenote: I wrote this code very quickly, so it's very badly formatted.
I have to calculateand then display in OOP of C++
Minimum of the even elements of the matrix and the maximum of the prime elements
As i made up my code
My problem is that i have no idea how to make so that the max value from a matrix to be a prime number and I have no clue if I should work with a for( ) and then use a if (value%i==0) or if there can be a better method.
#include <iostream>
using namespace std;
class matrix3
{
int a[10][10], b[10][10], c[10][10], d[10][10], e[10][10], f[10][10], x, y, i, j, l1,l2,max, max2 = 0, min = 0, save, c1, c2;
public:
void values();
void transpose();
void sum();
void diff();
void linie();
void coloana();
void minimul_elementelor_pare();
void maximul_elementelor_prime();
};
void matrix3::values()
{
cout << "Enter the rows "; cin >> x;
cout << "Enter the columns "; cin >> y;
cout << "Enter elements of first matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> a[i][j];
}
}
cout << "Enter elements of second matrix\n\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
cin >> c[i][j];
}
}
}
void matrix3::sum()
{
cout << "Sum of Matrices 1 and 2 is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
e[i][j] = a[i][j] + c[i][j];
cout << e[i][j] << "";
}
cout << endl;
}
}
void matrix3::diff()
{
cout << "Difference of Matrices 1 and 2 (1-2) is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
f[i][j] = a[i][j] - c[i][j];
cout << f[i][j] << "";
}
cout << endl;
}
}
void matrix3::transpose()
{
cout << "transpose of the matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
b[i][j] = a[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
cout << "Transpose of the second matrix is\n";
for (i = 1; i <= x; i++)
{
for (j = 1; j <= y; j++)
{
d[i][j] = c[j][i];
cout << b[i][j] << "";
}
cout << endl;
}
}
void matrix3::coloana()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n c1: ";cin >> c1;
cout << "\n c2: ";cin >> c2;
for (i = 1; i <= x; i++)
{
save = a[i][c1];
a[i][c1] = a[i][c2];
a[i][c2] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::linie()
{
cout << "test is\n";
cout << "\n Numerele coloanelor care doriti sa le interschimbati";
cout << "\n l1: ";cin >> l1;
cout << "\n l2: ";cin >> l2;
for (i = 1; i <= x; i++)
{
save = a[l1][j];
a[l1][j] = a[l2][j];
a[l2][j] = save;
}
for (i = 1;i <= x;i++) {
for (j = 1;j <= y;j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void matrix3::minimul_elementelor_pare()
{
cout << "Minimul Elementelor Pare";
for (i = 1;i <= x;i++)
{
if (x % 2 == 0)
{
if (a[i][j] < min)
min = a[i][j];
}
min++;
}
cout << " " << min;
}
void matrix3::maximul_elementelor_prime()
{
cout << "max matrice" << endl;
max=a[1][1];
for (i = 1;i <= x/2;i++)
{
for (j = 1;j <= y/2;j++)
if (x%i != 0 && max < a[i][j])
max = a[i][j];
}
cout << " maximul din matrice este " << max;
}
int main()
{
int input;
char ch;
matrix3 m;
m.values();
do
{
cout << "Enter your choice\n";
cout << " 1. Sum of 1 and 2\n" << " 2. Difference of 1 and 2\n" << " 3. Transpose of both 1 amd 2\n" << "4. Linii\n" << "5. Minim\n" << "6.Linie\n"<<"7. Maxim\n";
cin >> input;
switch (input)
{
case 1:
m.sum();
break;
case 2:
m.diff();
break;
case 3:
m.transpose();
break;
case 4:
m.coloana();
break;
case 5:
m.coloana();
break;
case 6:
m.minimul_elementelor_pare();
break;
case 7:
m.maximul_elementelor_prime();
}
cout << "\nDo another y/n?\n";
cin >> ch;
} while (ch != 'n');
cout << "\n";
system("pause");
return 0;
}```