61 / 103
Aug 2014

#include <iostream>
using namespace std;
int main(){
int a = 0;
    while(a <= 100)
{
cin >> a;
if (a == 42){

break;
}
else {

cout << a << "\n";

}
}
}

While Delshire's code does work and gets AC, it has one interesting quirk.

while(a <= 100)

It is given in the problem statement that the numbers "are integers of one or two digits" so this works fine. It would be more acceptable to see:

while(true)

or, since it is given that there is always a 42 in the input statement:

while(a != 42)

This last segment also removes the need for the extra break, giving the loop a single exit point.

#include <iostream>
using namespace std;
int main() {
	int a = 0;
	while(a != 42) {
		cin >> a;
		if(a != 42)
			cout << a << endl;
	}
}

Hello! Thanks for the quick reply, i brought another piece of code, a little more 'formal' i think:

#include <iostream>
using namespace std;
void displayOutput(int myArray[], int qOfNumbers);
int main() 
{
int inputValues[100] = {};
for(int i = 0; inputValues[i] < 100; i++)
	{
		int holder;
		cin >> holder;
		inputValues[i] = holder; 
		if (inputValues[i] == 42){
		displayOutput(inputValues, i);
		break;
		}
    }
}
void displayOutput(int myArray[], int qOfNumbers)
{
	for(int a = 0; a < qOfNumbers; a++)
	{
		cout << myArray[a] << "\n";
	}
}

int inputValues[100] = {};

This is scary. There is nothing in the problem statement that tells you a specific constraint on the number of inputs in the test case. While it does work in this case, in a normal setting you would run the risk of buffer overrun.

So i should go with:

int inputValues[100] = {0};

Thanks for the reply.

No. You have declared an array of 100 elements. If there are 105 elements in the input then you have a serious problem.

Ok so i should limit the input entry so 'they' can not enter more than 99 values, right?

No. Nowhere in the problem statement does it declare what the maximum amount of numbers to be input is. Because there is no constraint you cannot safely declare an array.

If a constraint were given then it would not be your responsibility to enforce it.

10 months later

#include<iostream>
using namespace std;
int main()
{
   int a[10],i;
   for(i=0;i<5;i++)
     cin>>a[i];
   for(i=0;i<5;i++)
   {
       if(a[i]==42)
        break;
      cout<<"\n"<<a[i];
   }
}

I dont know why my code is not working..its giving the input and output perfectly..Kindly help....

4 years later

Tak innego przykładu ale nawet patrząc na twój widać że np. ulica o numerze 2 łączy się z ulicami o numerach : 15, 5, 1, 16, 4, 3. Czyli aż sześcioma ulicami.

No ale co z tego wynika? Czy nie da się wyznaczyć trasy jakiej szuka Franek?
Uwierz, to zadanie nie bez powodu jest w kategorii "łatwe". smile

18 1
1 2
2 3
3 3
3 2
2 4
4 5
5 6
6 5
5 7
7 8
8 9
9 10
10 11
11 12
12 2
2 1
1 2
2 1

Odpowiedź brzmi NIE ??

#include <stdio.h>
int main()
{
    int p, t, k;
    long long x, y;
    bool test = true;
    scanf("%d %d", &t, &p);    
    long long int tab[t][2];
    for(int i =0; i<t; i++)
    {
                  scanf("%lld %lld", &x, &y);
                  tab[i][0]=x;
                  tab[i][1]=y;
                  if(test == true)
                  {
                  if(i == 0)
                  {
                   if(x != p)
                   {
                        test = false;                        
                   }
                  }
                  if(i != 0) 
                  {
                   if(x != k)
                    {
                       test = false;                  
                    }
                  } 
                  k=y;
                  }
    }   
    if( y != p)
      test = false;             
    if(test==true)
    {
    for(int i =0; i<t; i++)
    {
     for(int j=0; j<t; j++)
     {
             if(i!=j)
             {
                     if((tab[i][0] == tab[j][0]) && (tab[i][1] == tab[j][1]))
                     {
                     test=false;
                     break;
                     }
             }
     }
     if(test==false)
      break;
    }
    }
    if(test == true)
      printf("TAK");
    else
      printf("NIE");
    return 0;
}

Mam coś takiego. Nie wiem co może być jeszcze źle

Co do testu - odpowiedzią jest "TAK".
Co do programu - masz błędny algorytm.
Zastanów się nad czymś takim: Jeśli, którąś ulicą dojdzie się do danego skrzyżowania, to jakąś inną trzeba z niego wyjść. Czyż nie?
A może próbowałeś kiedyś rysować pewne figury "jednym pociągnięciem ołówka"? Które figury da się tak narysować?
A tak w ogóle, to przeczytaj cały ten wątek, bo znajdziesz tu dużą podpowiedź. wink

Patrz co do tego testu.
"Denerwuje go to, że czasami musi przejeżdżać dwa razy jakąś ulicę lub jej fragment."
czyli odpowiedź brzmi Nie dal tego testu.

A co do twojego pytania. Tak da się tylko koło tak narysować.

A co do tego programu nie mam zastrzeżeń tylko czegoś mu jeszcze brakuje tylko nie wiem czego.

#include <stdio.h>
int main()
{
    int p, t, k;
    long long x, y;
    bool test = true;
    scanf("%d %d", &t, &p);    
    for(int i =0; i<t; i++)
    {
                  scanf("%lld %lld", &x, &y);
                  if(test == true)
                  {
                  if(i == 0)
                  {
                   if(x != p)
                   {
                        test = false;                        
                   }
                  }
                  if(i != 0) 
                  {
                   if(x != k)
                    {
                       test = false;                  
                    }
                  } 
                  k=y;
                  }
    }   
    if( y != p)
      test = false;             
    if(test == true)
      printf("TAK");
    else
      printf("NIE");
    return 0;
}

Coś mi zaświtało

patrz:
Zdjęcia2

zielony wejście
czerwony drugi test
czarny pierwszy test
purpurowy numery ulic

Czyli oba test są poprawne gdyż. Poczta leży przy ul o numerze 4. Więc zaczynając w zgodnie z ruchem wskazówek zegara czy przeciwnie, otrzymam tą sama trasę. Czy dobrze to rozumiem?

Oczywiście, że oba testy są poprawne, ale czytając Twój wpis naszła mnie wątpliwość, czy dobrze rozumiesz, jak w danych wejściowych określa się położenie poczty.
Liczba określa numer ulicy przy której znajduje się poczta, a nie skrzyżowania.
A co do tych figur, to poszukaj sobie w necie czegoś o figurach unikursalnych. wink

Graf nie będzie niespójny bo:

3 years later

I'm having a similar problem and submitted following solution. When I ran it on http://ideone.com/22 it worked fine but still SPOJ saying it as wrong answer frowning

Please correct me if anything wrong in my solution.

compiled with C++ 5.1

int main() {
	
	int i;
	std::cout<<"Enter your input number (42 is for exit)"<<endl;
	do
	{
		std::cin>>i;
		if( std::cin.fail() )
		{
			std::cout<<"Wrong input, numeric numbers only accepts"<<endl;	
		}
		else
		{
			std::cout<<i<<endl;
		}
	}while(42 != i);

	std::cout<<"Successfully exiting"<<endl;
	return 0;
}

Everything that you output is considered for the solution.

This line alone is enough to cause your wrong answer:

std::cout<<"Enter your input number (42 is for exit)"<<endl;

Only output what is required.