N=a number introduced by us, and N natural numbers.Display the triplets of numbers read consecutive which have the last number 3 and display their number. One number can be part of multiple triplets.
Example:
for n=10, a=23 5 13 123 6113 7 123 83 553 393 => 3 triplets: 13,123,6113 & 123,83,553 & 83,553,393.
That's what I tried to do but it doesn't work.
I'm not 100% certain on what you are trying to do here, but if I understand correctly you want to input n numbers, then look through those numbers and find each unique set 3 numbers that end in 3. Is that right?
If so, to start off you might want to create an array (or other container) of n numbers first. Use prompts to ask the user for input and fill your array (or other container). Then you can go about your logic looking for triplets.
The requirements you have listed for the triplets are pretty vague, do you need every possible combination of triplets (in your example you only listed a partial list of the possible triplets)? Do you need to account for numbers that are used twice (for example what if you have 3 of the same number in your list of n numbers?)?
#include <iostream>
#include <deque>
int main()
{
unsignedint n ;
std::cout << "how many numbers? " ;
std::cin >> n ;
// to print out three consecutive numbers that end in three,
// we need to only look at a 'window' containing the most recent three numbers
// std::deque - to maintain a queue that we can iterate over
// http://en.cppreference.com/w/cpp/container/deque
std::deque< unsignedint > sliding_window ;
std::cout << "enter " << n << " non-negative integers\n" ;
int number ;
for( unsignedint i = 0 ; i < n && std::cin >> number ; ++i )
{
if( number%10 == 3 ) // if the number ends in three
{
sliding_window.push_back(number) ; // add it to the back of the window
if( sliding_window.size() == 3 ) // we now have three consecutive numbers ending in three
{
staticint cnt = 0 ;
// print out the three numbers
std::cout << ++cnt << ". " ;
for( int v : sliding_window ) std::cout << v << ' ' ;
std::cout << '\n' ;
// and remove the oldest number in the window; we are then left with
// the most recent consecutive two numbers that end in three
sliding_window.pop_front() ;
}
}
else sliding_window.clear() ; // not ending in three; clear the window
}
}
#include <iostream>
#include <vector>
int main(int argc, constchar * argv[]) {
int n = 0; ////integer to store: "N=a number introduced by us"
std::vector<int> numbers; ////integer container to store: "N natural numbers"
std::vector<int> triplets; ////integer container to store: "triplets of numbers"
std::cout<<"Enter a natural number: "; ////<- Get n number from user
std::cin>>n;
if(n < 3) ////<- Checking if we have a joker user!
{
std::cout<<"\nYour computer has a virus! D:\n.....erasing your disk drive!\n\n";
return 0; ////<- Finishing program!
}
std::cout<<"Now you'll enter " << n <<" numbers!\n"; ////<- Get n numbers from user
for(int i = 0; i < n; ++i)
{
std::cout<<"Enter number " << i+1 <<" : ";
int number;
std::cin>>number;
numbers.push_back(number);
}
for(size_t i = 0; i < numbers.size(); ++i) ////<- We'll check each number and-
////the next two consecutive numbers (only if there is two more numbers to check)
{
if(i+2 < numbers.size()) ////<- Checking if there is two more numbers than-
//// the current number
{ ////Checking a,b,c numbers are triplets?
if(numbers[i]%10 == 3 && numbers[i+1]%10 == 3 && numbers[i+2]%10 == 3)
{
////<- then: store these 3 numbers in "triplets" container!
triplets.push_back(numbers[i]);
triplets.push_back(numbers[i+1]);
triplets.push_back(numbers[i+2]);
}
}
}
////Calculating the number of triplets!
std::cout<<"\nNumber of triplets: " << triplets.size()/3 <<'\n';
int counter = 1;
for(constauto number : triplets) ////<- We'll iterate through our triplets container
{
if(counter%3 != 0)////<- if this is not the third number print number + ',' comma
std::cout<<number <<',';
else
////if this is the third number print number and a new line
std::cout<<number <<'\n';
++counter; ////<- pour counter he has the most boring job :(
}
return 0;
}
Enter a natural number: 10
Now you'll enter 10 numbers!
Enter number 1 : 23
Enter number 2 : 5
Enter number 3 : 13
Enter number 4 : 123
Enter number 5 : 6113
Enter number 6 : 7
Enter number 7 : 123
Enter number 8 : 83
Enter number 9 : 553
Enter number 10 : 393
Number of triplets: 3
13,123,6113
123,83,553
83,553,393
Enter a natural number: -1
Your computer has a virus! D:
.....erasing your disk drive!