Triplet problem

I'm currently trying to program that finds the number of triplets of a number from
6-12345 and also shows the best set of triplets and processing speed for the program.
Best set means the diff between largest and smallest number is minimal.
E.g input number in data8.txt(file that the number is extracted from): 9
Output ---> Number of triplets:3
Best triplet: 3 4 5
Processing time in sec: 0

My teacher already gave some guidelines like how to show the processing speed,
(int t = time(0);) and (cout << "Processing time in Sec: " << time(0) - t << endl;)
I would prefer to use switch to lower the processing time but I do not understand what to program in the cases. Need some advice!

This is my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include<bits/stdc++.h>
using namespace std;
int main(){
int n, ct=0, k, m, t=time(0), i, j, variable;
ifstream in("data8.txt");
in>>n;
m=n%3;
for(int i=1;i<n;i++){
for(int j=i+1;j<n;j++){
k=n-i-j;
if(k>j&&i+j+k==n)ct++;

switch(variable){
case 1:
	


	


}


}
}

cout<< "Number of triplets: "<< ct <<endl;
cout<< "Best triplet: "<< endl;
cout << "Processing time in Sec: " << time(0) - t << endl; 
 
return 0;
}


How do I put in the cases and if there are any additional mistakes please point them out. Thanks in advance.
I am not sure I am following the problem at all.
Are you looking for 3 numbers in a row? What is a triplet here?

if so, why not loop like this...

for(I = 4; I < size-2; I++)
{
if( I ? I+1 ? I+2) //some condition that looks at 3 consecutive values
then hooray, you found one, count it, save it, is it the best??
}

3,4,5 //5-3 is 2?
6,7,8 // 8-6 is 2?

I don't get "best"

Sorry did not make myself clear, 'triplet' means a set of three numbers that add up to the number extracted from 'data8.txt'. As seen in the example, 3+4+5=9. Another triplet can also be 1+2+6. A number cannot be duplicated. So, best triplet means that the difference between the largest and smallest number in that particular triplet is the smallest out of all possible triplets. For example, the difference between 3 and 5 is the smallest possible, 2 as there must be 3 different numbers. Compared to the difference between 1 and 6, it is smaller.

Hope this clarifies your confusions with my questions. Any other advice?
Also if you can, please add on to my current code as I may not be able to understand new code that you introduce, not very proficient in C++.
3+4+5=9 ???

In what universe?

you only have to check 1/2 the numbers, by the way

if the input is 9, if you code it right, the third number (even if larger than 50% of 9) will already have been found.

choose 1, ok, 9-1 is 8.
choose 2, ok, 8-2 is 6.
the third # is 6.

It isn't possible to have 3 values with 2 of them larger than 50% of the input value that add up to the input value.

I am not sure a switch is the answer. Conditionals, nested loops, etc all break the u-v pipeline. But we can check to see. Do the values need to be unique? Or is 2+2+2 ok for 6?

There MAY be a direct computation for the # of triplets... it looks like the sort of thing that might...
Last edited on
Topic archived. No new replies allowed.