Drawing a Vertical Pyramid in C - nested-loops

I have to get the desired output as follows:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
But I can't seem to figure out how to do it. All I get is:
1
2 6
3 7 6
4 8 7 6
5 9 8 7 6
Here is my code:
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<i;j++)
{
if((j+1)==1)
printf("%d ",i);
else
printf("%d ",i+n-j);
}
printf("\n");
}
return 0;
}
But I understood the desired output:
I have to print the numbers from 1 to 15 in ascending order like a right angled triangle.

#include <stdio.h>
#define SZ 5
int main()
{
int i,j, add = SZ, val[SZ+1] = {1};
for( j=1; j<=SZ; ++j )
{
for( i=0; i<j; ++i )
printf( "%2d ", val[i]++ );
printf( "\n" );
val[j] = val[j-1] + --add;
}
}

Related

Can't find the problem here with topological sort

I've just started a graph algorithm. There is a code of my topological sort algorithm with C++. It's a problem of UVA online judge.
my output doesn't match with the accepted output..
as if I give the input:->
8 25
8 6
5 2
7 4
7 1
8 6
6 1
7 1
6 2
8 6
8 6
5 7
6 2
8 4
1 3
7 1
1 3
6 7
1 3
6 4
2 3
5 2
6 3
8 4
4 3
5 6
0 0
My output is -> 5 8 6 2 7 4 1 3
but the accepted output is-> 8 5 6 7 4 2 1 3
as much as I know topological sort can have different acceptable outputs.
but I am still getting wrong answer.
Link to the problem (Ordering tasks)
Code:
#include<bits/stdc++.h>
using namespace std;
void file(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
vector<int>graph[102],out;
int n,e,u,v,inDeg[102];
void Back(){
memset(inDeg,0,sizeof inDeg);
for(int i=0; i<102; i++)graph[i].clear();
out.clear();
}
void init(int e){
for(int i=0; i<e; i++){
cin>>u>>v;
graph[u].push_back(v);
inDeg[v]++;
}
}
void topoSort(){
queue<int>q;
int u;
for(int i=1; i<=n; i++){
if(inDeg[i]==0)q.push(i);
}
while(!q.empty()){
u=q.front();
q.pop();
out.push_back(u);
int len=graph[u].size();
for(int i=0; i<len; i++){
int v=graph[u][i];
if(inDeg[v]>0){
inDeg[v]--;
if(inDeg[v]==0)q.push(v);
}
}
}
}
void print(){
int len=out.size();
for(int i=0; i<len; i++){
printf("%d ",out[i]);
}printf("\n");
}
int main(){
file();
while(scanf("%d %d",&n,&e) && n!=0 && e!=0){
init(e);
topoSort();
print();
Back();
}
return 0;
}
Should I keep a track of the visited node?

How to print (output) multiple lines from a loop?

I would like to know how to output multiple lines from a for loop.
For example, if I want to input a number of subsequent lines N and then another numbers. I am trying to output the inputs that I provided below, but whenever I do this it keeps returning only the last digits and not everything I entered.
This is what I know so far.
#include <iostream>
using namespace std;
int main()
{
int N, M, num;
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> M;
for (int k = 0; k < M; k++)
cout << M << endl;
}
}
return 0;
}
Input:
2 (This is for N)
1 2 3
4 5 6
or
3
10 20 30
50 100 500
1000 5 0
---------
Output:
1 2 3
4 5 6
10 20 30
50 100 500
1000 5 0

QuickSort Hoare's partitioning not working

Hi I'v been trying to write Hoare's partition function for like 5 hours now. I read this and this and even straight up copied the "correct" function from these questions and it's still nowhere near the answer. Here is my "translated" code from Cormen's book.
int Hpartition(int A[], int p, int r){
int x = A[p];
int i = p - 1;
int j = r + 1;
while(true){
do{
j--;
}while(A[j] <= x);
do{
i++;
}while(A[i] >= x);
if(i < j){
swap(A[i],A[j]);
}
else
return j;
}
}
QuickSort:
void qs(int A[],int p,int r){
if(p<r){
int q=Hpartition(A,p,r);
qs(A,p,q-1);
qs(A,q+1,r);
}
}
Main:
int main(int argc, const char * argv[]) {
int tab[10];
for(int k=0;k<10;k++){
cin >> tab[k];
}
qs(tab,0,9);
for(int i=0;i<10;i++){
cout << tab[i] << " ";
}
return 0;
}
For this data :
2 4 1 3 5 7 6 8 10 9
It produces this result:
2 4 9 3 5 7 6 8 10 1
Based on the previous questions regarding the topic I know there may be some mistakes in the book. But even when I apply the answers from the previous questions it just won't work .
Here is the algorithm from the book:
HOARE-PARTITION(A, p, r)
1 x = A[p]
2 i = p - 1
3 j = r + 1
4 while TRUE
5 repeat
6 j = j - 1
7 until A[j] <= x
8 repeat
9 i = i + 1
10 until A[i] >= x
11 if i < j
12 exchange A[i] with A[j]
13 else return j
Thanks in advance for any help.
You have 2 error translating code from book:
do{
j--;
}while(A[j] <= x);
You should inverse this:
do{
j--;
}while(A[j] > x);
The same with:
do{
i++;
}while(A[i] >= x);
And one more here:
qs(A,p,q-1);
qs(A,q+1,r);
Change to:
qs(A,p,q);
qs(A,q+1,r);

How to read from scanf values of the 01-knapsack algorithm and pass to a function

If I enter the numbers:
3 10
7 6
5 5
4 5
the output is : 9. OK — that's the correct value. But if I enter:
10 25
3 5
3 5
3 5
3 5
3 5
3 5
3 5
3 5
3 5
3 5
the correct output should be: 15 but I receive 2005985278.
What's the problem in this code?
#include<stdio.h>
#include<stdlib.h>
#define MA 29
int max(int a, int b) {
return (a > b)? a : b;
}
int knapsack(int W, int P[], int V[], int N)
{
int i, w;
int K[N+1][W+1];
for (i = 0; i <= N; i++)
{
for (w = 1; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (P[i-1] <= w)
K[i][w] = max(V[i-1] + K[i-1][w-P[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[N][W];
}
int main()
{
int N,W,i;// N: Quantidade de Objetos - W: Capacidade da Mochila; i: interação
int Val=1;//V: Valor;
int Pes=1;//P: Peso;
do{
scanf("%d %d",&N,&W);//ler N e W
for (i=1;i<=N;i++) // iteração
scanf("%d %d",&Val,&Pes);//ler V
//ler P
int V[]={Val};//declaração do vetor V e recebendo Val do scanf
int P[]={Pes};//declaração do vetor P e recebendo Pes do scanf
printf("%d",knapsack(W, P, V, N));
printf("\n");
}while(N!=0 && W!=0);
return 0;
}
I need to enter the number of items N and the capacity W:
When I enter N = 1, W - 7 and the objects P = 4, V = 5 the output is 4.
If I enter other values such as N = 10, W = 25 and P = 3 3 3 3 3 3 3 3 3 3, V = 5 5 5 5 5 5 5 5 5 5, I receive 2005985278 instead of 15.
Please what's the error in my code?
Now my code is this but I receive the erro in my output:
3 10
7 6
5 5
4 5
1 7
4 5
the correct output is : 9 and 4 and i received 7 and 0;
in this case how may i add a end the programm when enter N==0 && W==0?
#include<stdio.h>
#include<stdlib.h>
#define MA 29
int max(int a, int b) {
return (a > b)? a : b;
}
int knapsack(int W, int P[], int V[], int N)
{
int i, w;//interação;
int K[N+1][W+1];//declaração de K recebendo o valor de N e W +1;
for (i = 0; i <=N; i++) // para i=0 i< = N incrementa i;
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (P[i-1] <= w)
K[i][w] = max(V[i-1] + K[i-1][w-P[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[N][W];
}
int main()
{
int N,W,i;// N: Quantidade de Objetos - W: Capacidade da Mochila; i: interação
//P: Peso;
while (scanf("%d %d", &N, &W) == 2)
{
int V[N];
int P[N];
for (i = 1; i <= N; i++)
if (scanf("%d %d", &V[i], &P[i]) != 2)
break;
printf("%d\n", knapsack(W, P, V, N));
}
}
Your problem is that you are allocating arrays of size 1 and then trying to access 10 elements in those arrays. C won't stop you trying, but it will usually give you the wrong answer.
Existing code:
do{
scanf("%d %d",&N,&W); // Should test for success and terminate loop on failure
for (i=1;i<=N;i++)
scanf("%d %d",&Val,&Pes); // Should check for success; should store values
int V[]={Val}; // V is an array with one entry, the last value entered as Val.
int P[]={Pes}; // P is an array with one entry, the last value entered as Pes.
printf("%d",knapsack(W, P, V, N));
printf("\n");
} while (N!=0 && W!=0);
C99 code:
while (scanf("%d %d", &N, &W) == 2)
{
int V[N];
int P[N];
for (i = 1; i <= N; i++)
if (scanf("%d %d", &V[i], &P[i]) != 2)
break;
printf("%d\n", knapsack(W, P, V, N));
}
Note that if you'd printed the inputs to your knapsack() function, either in the calling code or in the function itself, you'd have seen the trouble very quickly. It is a powerful technique to print your input data after you've finished reading it. Note that printing it while you're reading can mask problems that printing after you've finished reading will reveal.
Clearly, if your knapsack algorithm is incorrect, you'll still get wrong answers; I've not reviewed that and have no particular plans to do so. However, you should be getting the correct input data, which improves enormously the chances of getting the right output.

Finding set of pairs that correspond to list of sums

Given two lists of numbers and a list of totals (none in any particular order):
a = [1,2,3]
b = [4,5,6]
c = [6,7,8]
How can I find all sets of pairs d where d[k] = (a[i], b[j]) such that c[k] = a[i] + b[j] where pairs are used from a and b without replacement? (all lists can have duplicates)
d = [(1,5), (3,4), (2,6)]
d = [(2,4), (1,6), (3,5)]
For c = [7,7,7]:
d = [(1,6), (2,5), (3,4)]
(1 answer because all permutations are essentially equivalent)
I'd like to do this with lists of length ~500, so a naive matching/backtracking search is out of the question.
Okay, there is the brute force approach with pruning. This takes O(N^3)
For ease of demonstration, I will go through an N-by-N square that has the sum of a and b
S:
+ | 4 5 6
--|-------
1 | 5 6 7
2 | 6 7 8
3 | 7 8 9
And I am looking to build c={6,7,8}
I find a '6' in S. I remove it, and mark its row and column as unavailable
S:
+ | 4 5 6
--|-------
1 | / X /
2 | 6 / 8
3 | 7 / 9
Solution = { (1,5) }
Then I try to find a '7'
S:
+ | 4 5 6
--|-------
1 | / X /
2 | / / 8
3 | X / /
Solution = { (1,5) (3,4) }
And finally the '6'
S:
+ | 4 5 6
--|-------
1 | / X /
2 | / / X
3 | X / /
Solution = { (1,5) (3,4) (2,6) }
The 1st loop ( the one for '6' ) will continue and find another match : (2,4). This will then form the second solution { (2,4) (1,6) (3,5) }
Now, One way to improve this is, use some dynamic-programming: find out all possible combinations that give the result beforehand.
Given c={ 6 7 8}, create sets S_x where x is {6,7,8} and
S_x = { (i,j) } such that S[i][j]=x
So:
S_6 = { (1,2) (2,1) }
S_7 = { (1,3) (2,2) (3,1) }
S_8 = { (2,3) (3,2) }
And now, the same algorithm with given heuristics will run in O(S_l1 * S_l2 * ... S_lN), where S_li denotes the length of S_i.
This may run a factor faster in the average case.
It will also handle the c={7,7,7} case properly.
That's pretty much all I got.
Here is a brute-force approach in C++. It doesn't prune equivalent permutations e.g. for c=[7,7,7].
#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;
// numerical 3d match: x + y + z = b where
// x = a, y = b, z = -c, b = 0
template <typename T>
vector<pair<vector<T>, vector<T> > > n3dmatch(vector<T> a, vector<T> b, vector<T> c) {
vector<pair<vector<T>, vector<T> > > result;
if (a.size() != b.size() || b.size() != c.size()) return result;
vector<vector<T> > ap, bp;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
do { ap.push_back(a); } while (next_permutation(a.begin(), a.end()));
do { bp.push_back(b); } while (next_permutation(b.begin(), b.end()));
for (int i = 0; i < ap.size(); i++) {
for (int j = 0; j < ap.size(); j++) {
bool match = true;
for (int k = 0; k < a.size(); k++) {
if ((ap[i][k] + bp[j][k]) != c[k]) {
match = false; break;
}
}
if (match) result.push_back({ ap[i], bp[j] });
}
}
return result;
}
int main(int argc, char *argv[]) {
vector<int> a = { 1, 2, 3 };
vector<int> b = { 4, 5, 6 };
vector<int> c = { 6, 7, 8 };
//vector<int> c = { 7, 7, 7 };
auto result = n3dmatch(a, b, c);
for (int i = 0; i < result.size(); i++) {
vector<int> &a = result[i].first;
vector<int> &b = result[i].second;
for (int j = 0; j < a.size(); j++) cout << a[j] << " "; cout << endl;
for (int j = 0; j < b.size(); j++) cout << b[j] << " "; cout << endl;
cout << "-" << endl;
}
return 0;
}

Resources