1
Good evening, folks. I need to implement a program that always prints a sequence of three numbers where there can be no repetition.
Ex.: t = 4.
Expected output: (0 1 2) (0 1 3) (0 2 3) (1 2 3)
The program runs for t = 4, however, for t = 10, it runs only to some extent (up to i = 6). I’ve tested every part of the code and I can’t find the error, I wonder if someone could help me?
Thank you! :)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct{
int v1;
int v2;
int v3;
}Triangulo;
int main(){
int n;
scanf("%d", &n);
Triangulo T[n];
int t;
t = (n*(n-1)*(n-2))/6;
printf("t = %d\n", t);
int c = 0;
int f = c+1;
int s = f+1;
for(int i = 0; i < t; i++){
T[i].v1 = c;
T[i].v2 = f;
T[i].v3 = s;
if(s != n-1){
s++;
}else if((s == n-1)&&(f == n-2)){
c++;
f = c+1;
s = f+1;
}else if(s == n-1){
f++;
s = f+1;
}
}
for(int j = 0; j < t; j++){
printf("%d %d %d\n", T[j].v1, T[j].v2, T[j].v3);
}
return 0;
}
Can you put the full code? In the post part is not being defined
n
,T
andt
. Do you only want triples with different values or permutations? For example, you want it to have(0 1 2)
and(2 1 0)
on the way out, or just(0 1 2)
?– Mauro Roberto
Hello. With the example you gave, just (0 1 2) enough. I will update the post and put the full code :)
– Julia Manuela
You want all permutations that are of the type
(i j k)
such thati < j < k
, Right? I left an answer that does that, but I’m still not sure if that’s exactly what you want.– Mauro Roberto