Guys, even if any of those hideous solutions work, which is a lot to say for the very first and easiest problem ever to exist, they will land you a time of 0.01.
The I/Ostream library is SLOW as hell - depending on what you read/write to the stream, they are at LEAST 10 times slower than the standart reading/writing functions in stdio. Use stdio.h.
About the problem - try the following:
#include <stdio.h>
int main() {
int x; //= 0 init
scanf("%d", &x);
while(x != 42) {
printf("%d\n", x);
scanf("%d", &x);
}
return 0;
}
There are still better approaches to the problem.
[quote="noix"]
#include <iostream>
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}
[/quote]
This TEST solution is wrong, isn'it?
In case of input 8, 99, 4, 2, 10, 42, 3 it will output 8, 99.
I see that it's enough that it works for the test case, but as example answer it is not the best...
Ok i've manged to find a solution but when i submit it i got wrong answer even though it's doing what it's supposed to do
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x;
do
{
scanf ("%d", &x);
printf (" %d \n", x);
}
while (x != 42);
return 0;
}
so what i'm doing wrong?
#include <iostream>
using namespace std;
int main()
{
int size = 0;
int n[size];
do
{
cin>>n[size];
size++;
}while(n[size-1] != 42);
cin>>n[size];
for(int i = 0; i < (size-1); i++)
{
cout<<n[i]<<endl;
}
return 0;
}
Hello everyone,
When i compile it it outputs the correct answer, however judge points me out to a runtime error (SIGSEGV).
What is wrong with the code then?
Thanks in advance, and glad to join this community.
When you declared n size was equal to zero, so n has zero elements. c and c++ don't check array bounds, they only throw errors when the program tries to access illegal memory (memory outside the defined space for the program).
For this problem the number of inputs are undefined. There could be ten, there could be a million. The amount doesn't matter.
Input a number. If it's 42 stop. Otherwise, output the number. Repeat.
I'm sorry this is actually my first program in C++. I've done this program in pascal before, and i just 'translated' in C++. The sample input and output works, but time limit exceeds. Am i doing something wrong? It works fine on Ideone.
#include <iostream>
using namespace std;
int main ()
{
int a, b;
bool c;
c=false;
while (true)
{
cin >> b;
if (b == 42)
c = true;
else if (c == false)
cout << b << "\n";
}
return 0;
}