I have a C++ problem:
Input an sequence of digit [ 0 - 9 ] and terminated by three 9 consecutivly, print on standard output the number of subsequences consisting of three consecutive equal digits on standard output.
Example: Given the sequence { 1 2 2 2 2 0 0 3 3 3 7 9 9 9 }, the subsequence are identified:
{ 2 2 2}, { 2 2 2 }, { 3 3 3 } .
Therefore, the program should print on standard output the number 3, equal to sequences present.
I try to use an array. My code ended up like this:
int main(){
int i;
int N = 0, A[100];
while( (A[i] && A[i+1] && A[i+2]) != 9 ){
N++;
for( i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:";
cin >> A[i];
}
for(int i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:" << A[i];
}
}
}
My problem is, I have no idea how to terminate the sequence by three 9's consecutively. So I try to use an array. I hope someone can help me to elaborate the idea.
You can do that by breaking the loop when three consecutive 9 is found.
#include <iostream>
const int ARRAY_SIZE = 100;
int main(){
int i;
int N = ARRAY_SIZE, A[ARRAY_SIZE];
for( i = 0; i < ARRAY_SIZE; i++ ){
std::cout << "A[" << i + 1 << "]:";
std::cin >> A[i];
// stop when three consecutive 9 is found
if (i >= 2 && A[i - 2] == 9 && A[i - 1] == 9 && A[i] == 9){
N = i + 1;
break;
}
}
for(int i = 0; i < N; i++ ){
std::cout << "A[" << i + 1 << "]:" << A[i] << '\n';
}
return 0;
}
Instead of an array with the number of elements equal to the magic number 100 what you need is an array of exactly three elements.
Here is a demonstrative program.
#include <iostream>
int main()
{
const size_t N = 3;
int a[N];
size_t count = 0;
for ( size_t i = 0, j = 0; std::cin >> a[j++]; )
{
j %= N;
if ( i != N - 1 )
{
++i;
}
else
{
size_t k = 1;
while ( k < N && a[k] == a[k-1] ) k++;
if ( k == N )
{
if ( a[0] == 9 ) break;
else ++count;
}
}
}
std::cout << "count = " << count << '\n';
return 0;
}
If to enter the sequence of numbers
1 2 2 2 2 0 0 3 3 3 7 9 9 9
then the program output will be
count = 3
instead of the inner while loop you could use for example the algorithm std::all_of.
Related
Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.
Output:
For each testcase, in a new line, print the starting and ending positions(1 indexing) of first such occuring subarray from the left if sum equals to subarray, else print -1.
Constraints:
1 <= T <= 100
1 <= N <= 107
1 <= Ai <= 1010
Example:
Input:
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output:
2 4
1 5
My Code:
#include <iostream>
using namespace std;
int main() {
int t, n, s, a[1000], result[1000];
cin >> t;
for (int i = 0; i < t; i++) {
result[i * 2] = -1;
cin >> n >> s;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
int flag = 0;
for (int j = 0; j < n; j++) {
if (flag == 0) {
int sum = 0;
for (int k = j; k < n && sum < s; k++) {
sum += a[k];
if (sum == s) {
result[i * 2] = j + 1;
result[(i * 2) + 1] = k + 1;
flag = 1;
break;
}
}
}
}
}
for (int i = 0; i < t * 2; i += 2) {
if (result[i] != -1) {
cout << result[i] << " " << result[i + 1] << endl;
} else {
cout << result[i];
}
}
}
Result:
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
4 225
9 45 10 190
Its Correct output is:
-1
And Your Code's output is:
-1-1-1-138 42
I've just found this:
https://www.youtube.com/watch?v=G0ocgTgW464
However I believe that the time complexity is O(n*log(n)) given the fact that map::find is O(log(n))
I'm asking for help with a problem implying an array sorting in the following manner: all even numbers must be in front of the odd ones. I've partially made the problem, but I did the sorting in the opposite manner and I can not manage to fix it.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int v[100], n, i, aux = 0, inv;
cout << "Number of elements: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
do
{
inv = 0;
for (i = 0; i < n; i++)
{
if (v[i] % 2 == 1 && v[i + 1] % 2 == 0)
{
inv = 1;
aux = v[i];
v[i] = v[i + 1];
v[i + 1] = aux;
}
}
} while (inv != 0);
cout << endl;
for (i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
system("pause");
return 0;
}
The output for this would be:
n = 8
1 3 2 4 7 8 4 2
Result: 2 4 8 4 2 -858993460 1 3
In the expression v[i + 1], you access v[n] when i = n - 1, this will result in an out-of-bounds memory access which results in undefined behaviour.
You should change the for loop to this:
for (i = 0; i < n - 1; i++)
The output for the given input is:
a.exe
Number of elements: 8
v[0]=1
v[1]=3
v[2]=2
v[3]=4
v[4]=7
v[5]=8
v[6]=4
v[7]=2
2 4 8 4 2 1 3 7
Desired output:
2
4 2
6 4 2
8 6 4 2
Here is my code:
for(int i = 0; i <= 7; i += 2) {
for (int j = 0; j <= i; j += 2)
cout << j + 2 << " ";
cout << endl;
}
and I get a wrong answer.
Your approach with the double for loop was a good idea. Here is my generalized attempt, which allow to print what you want in n lines, rather than just 4:
#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i = 0; i < n; ++i) {
for(int j = i; j >= 0; --j) {
cout << 2 * (j + 1) << " ";
}
cout << "\n";
}
return 0;
}
Output:
2
4 2
6 4 2
8 6 4 2
Live Demo
PS: This approach minimizes (if not exterminates) the usage of magic numbers.
Welcome to Stack overflow.
You just went the wong way around with your inner for loop
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 7; i += 2){
for (int j = i; j >= 0; j -= 2) {
cout << j + 2 << " ";
}
cout << endl;
}
return 0;
}
this gives you the desired output:
2
4 2
6 4 2
8 6 4 2
I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!
How do i achieve this format when i keyed in int of ' 245 ':
(where if it is odd number it will be a rectangle, and even number will be triangle)
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
This is my code so far:
(I can't seemed to output triangle and rectangle at the same time)
int n;
int lastDigit;
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == '0');
cout << endl;
// If even digit - tri
do
{
lastDigit = n%10;
if (lastDigit / 2 ==0)
{
for (int i = 1; i <= lastDigit; ++i)
for (int tri = 1; tri <= i; ++tri)
cout << "\t" << tri;
cout << endl;
}
// if odd digit - rect
else if (lastDigit / 2 != 0)
{
for (int i = 1; i <= lastDigit; i++)
{
for (int rect = 1; rect <= i; rect++)
cout << "\t" << rect;
cout << endl;
}
n = n/10;
}
cout << endl;
}while (lastDigit != 0);
n = n/10;
cout << endl;
return 0;
And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?
Any help would be appreciated!
Following is the complete code.
Step1: Take user input and check whether it's odd or even.
Step2: If it is odd then perform triangle else rectangle.
#include<iostream>
int main () {
int n;
cout<<"Enter number: ";
cin>>n;
if (n % 2 != 0)
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= i; j++)
{
cout<<j<<" ";
}
}
}
return 0;
}
Both of your print is similar. I suggest:
#include<iostream>
using namespace std;
int main () {
int n;
cout << "Enter number: ";
cin >> n;
int i = 1;
int& rowLim = ((n % 2) ? n : i);
for(i = 1; i <= n; i++)
{
cout << endl;
for(int j = 1; j <= rowLim; j++)
{
cout<<j<<" ";
}
}
return 0;
}
Simplest would be to use a string, iterate char by char and output accordingly. For instance
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
int n = c - '0';
bool k = n % 2;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= (k ? n : i); ++j)
cout << " " << j;
cout << endl;
}
cout << endl;
}
return 0;
}
Output
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
See a DEMO