include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int n;
scanf("%d",&n);
int l,k;
for(int i=0;i<n;i++)
{
scanf("%d %d",&l,&k);
char ch[l+1];
/****/ scanf("%s",ch);
printf("Are we here");
char ci=ch[0];
int flips=0;
int count=0;
for(int j=0;j<l;j++)
{
if(ch[j]==ci)
{
count++;
if(count>k)
{
flips++;
count=1;
if(ci=='1')
{
ci='0';
ch[j]='0';
}
else if(ci=='0')
{
ci='1';
ch[j]='1';
}
}
}
else if(ch[j]!=ci)
{
if(ci=='1')
ci='0';
else if(ci=='0')
ci='1';
count=1;
}
}
printf("\n%d",flips);
printf("\n%s",ch);
}
return 0;
}
An input with 3 test cases and each test case having two lines of input.
3
2 1
11
2 2
11
4 1
1001
should give the output
1
10
0
11
2
1010
This is basically a code that does some standard programming contest stuff. It takes in the number of test cases, and for each of the test cases prints out the required answer in two lines .Now when I type all the inputs line by line it works fine and I get all the outputs. But when I just copy paste all the inputs together I only get everything but the last output and I have to press the enter key to get the last output. Now I did some basic debugging and found out that it has something to do with the scanf("%s",ch) line of code. Any help.. Also I felt the logic of the program doesnt matter. I'm missing something far more basic here...
I think "\r\n" are not copied when you copy paste the text
Related
I have been debugging and seeing this code for nearly a week now still I couldn't find any solution and thus hoping to get one from the fellow developers in stack overflow.
This is the solution of the Faded Palindrome problem of September Challenge Codechef This is the problem page
#include<iostream>
#include<vector>
#include<iterator>
#include<stdlib.h>
#include<cstdio>
#include<string>
using namespace std;
string def="-1";
int main()
{
int t;
cin>>t;
//vector<string> out(t);
string out[100];
for(int i=0;i<t;i++)
{
string inp;
cin>>inp;
if(inp.size()%2) //Checking wether the number of letters is odd
{
int counter=inp.size()/2;
int counter_back=counter-1;
int counter_front=counter+1;
if(inp[counter]=='.')
inp[counter]='a';
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front=='.']){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
else{ //Checking the number of letter is even
//cout<<inp.size();
int counter_front=(inp.size())/2;
//cout<<counter_front;
int counter_back=counter_front-1;
//cout<<counter_back;
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{//cout<<"*-";
counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front]=='.'){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
//cout<<"--";
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
}
/*vector<string>::iterator itr;
for(itr=out.begin();itr!=out.end();itr++)
cout<<*itr<<endl;*/
for(int j=0;j<t;j++)
if(j==t-1)
cout<<out[j];
else
cout<<out[j]<<"\n";
return 0;
}
While your code may pass all the test cases presented in the problem statement, there are still more edge cases that your code doesn't cover. Here's a sample test case:
6
..
...
..e
a..a.v.
p..p
t.xzt
This should print:
aa
aaa
eae
avaaava
paap
tzxzt
instead it prints:
..
.a.
aaa
aa.a.aa
p..p
taxat
Hints:
The first if statement is not correctly handling the case where inp[b] and inp[f] are both '.'.
There is no need for doing two different things when |s| is even or odd.
Also, in the first else if statement, you probably meant inp[counter_front]=='.' instead of inp[counter_front=='.'].
if(inp[counter_back]==inp[counter_front]) is true if they both are equal to '.' so you won't fill that gap.
My Need:
Using a while loop greet as many names as that are available on the stdin. Stop when you read the string '42' as a name.
My Coding:
#include<iostream>
using namespace std;
int main()
{
int input=1;
int i= 0;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Result:
For input 42, test case is passed. For other input, test case failed. Please post your answer.
Answer After ~1 Year:
Very sorry for this question . This is I asked when I have 0 knowledge about C++. This may be useful for the freshers.
Your loop is flawed
int input=1;
int i= 0;
string name;
while(input<=i)
as input is greater than i to start with
You think the test case works for 42 but actually the logic inside your loop is never executed. It is simply the case that the console output is the same (i.e. there is none) but your code never gets as far as the cin to check the input is 42
Your code doesn't ever enter while since the condition is always false.
Just use,
....
while(1)
{
....
This will run your loop indefinitely, and break whenever 42 is encountered.
Your code does not even run when you pass 42, because input is greater than i.
while(input<=i) // input = 1, i = 0, 1 > 0
What you probably want is an infinite loop:
while (true)
Your Code has these Problems:
It won't enter the while loop as input is initialized to 1 and i is initialized to 0.While checking the condition *while(input<=i)*i.e., while(1<=0) which is false it won't execute the statements below.
-
My Solution:
#include<iostream>
using namespace std;
int main()
{
int input=0;
int i= 1;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Initializing input with 0 and i with 1 will give you the desired output.
First of all, Sorry for my basic question. The loop is not initiated. Because the input is 1 and i is 0. But the condition I given is input<=i. Because of the false condition, the control is not entering in loop
I'm a begineer in C++ programming but I know the basics.
I recently started writing a simple game. The program chooses a random number
(1-100) and you have to guess it. There are 2 modes:
Normal - whenever you enter a number program tells you if it's bigger than the random or smaller.
hard - no clues, just pure luck.
Everything was running ok but when I added some fixes to the displayed text the program won't compile. I use CODE::BLOCKS.
Screenshot: http://scr.hu/81tw/m6cm0
I really apreciate your help.
Full code below:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{
{
cout<<"Choose your mode..."<<endl;
cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
cin>>mode;
if(mode=1)
cout<<"Normal mode chosen."<<endl;
goto normal;
if(mode=2)
cout<<"Hard mode chosen!"<<endl;
goto hard;
return 0;
}
{
hard:
cout<<"I chose a random number in a range from 1 to 100, can you guess it?"<<endl;
srand(time(NULL));
number_hard = rand()%100+1;
while(guess_hard!=number_hard)
tries_hard++;
cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
cin>>guess_hard;
if(guess_hard=number_normal)
cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
}
{
normal:
cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
srand(time(NULL));
number_normal = rand()%100+1;
while(guess_normal!=number_normal)
tries_normal++;
cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
cin>>guess_normal;
if(guess_normal==number_normal)
cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;
if(guess_normal<number_normal)
cout<<"Too low."<<endl;
else if(guess_normal>number_normal)
cout<<"That's too much!"<<endl;
system("pause");
return 0;
}
The code that works is here
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{
{
cout<<"Choose your mode..."<<endl;
cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
cin>>mode;
if(mode=1)
cout<<"Normal mode chosen."<<endl;
goto normal;
if(mode=2)
cout<<"Hard mode chosen!"<<endl;
goto hard;
return 0;
}
{
hard:
cout<<"I chose a random number in a range from 1 to 100, can you guess it?"<<endl;
srand(time(NULL));
number_hard = rand()%100+1;
while(guess_hard!=number_hard)
{
tries_hard++;
cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
cin>>guess_hard;
if(guess_hard==number_normal)
cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
}
}
{
normal:
cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
srand(time(NULL));
number_normal = rand()%100+1;
while(guess_normal!=number_normal)
{
tries_normal++;
cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
cin>>guess_normal;
if(guess_normal==number_normal)
cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;
if(guess_normal<number_normal)
cout<<"Too low."<<endl;
else if(guess_normal>number_normal)
cout<<"That's too much!"<<endl;
//system("pause");
}
}
return 0;
}
Well, you didn't used braces across your while loops, and as #cowls suggested, there was a closing brace missing after main. Everything else was fine. Also you used a = for comparison between to variables, instead of ==, = is a assignment operator, while == is used for comparison.
You are missing a close brace at the end } that will close your main method.
I would also pay attention to the comments against the question addressing the other issues in your code.
Note: this could not be seen from the screenshot, showing why that was a bad format to post your code in.
#include <iostream>
#include <vector>
#define MAXX 1000
using namespace std;
int number[MAXX], digits=0;
int adjust(int i)
{
if(number[i]<9)
{
cout<<"i = "<<i<<" "; //PROBLEM
(number[i])++;
return i;
}
number[i]=0;
adjust(i-1);
}
void makePalindrome(int head,int tail)
{
int revert;
if(head>tail)
return;
if(number[head]==number[tail])
{
makePalindrome(head+1,tail-1);
}
if(number[tail]<number[head])
{
number[tail]=number[head];
makePalindrome(head+1,tail-1);
}
if (number[tail]>number[head])
{
number[tail]=number[head];
revert=adjust(tail-1);
if(revert<=head)
{
makePalindrome(revert,digits-revert-1);
}
else
{
makePalindrome(head+1,tail-1);
}
}
}
int main(int argc, char const *argv[])
{
long long int num,num_copy;
int head,tail;
int number_reverse[MAXX];
cout<<"Enter the number whose next greater palindrome you want to find" <<endl;
cin>>num;
num_copy=num;
while(num_copy>0)
{
number_reverse[digits]=num_copy%10;
digits++;
num_copy=num_copy/10;
}
//cout<<"Digits = "<<digits<<"\n";
for (int i = digits-1; i >=0; --i)
{
number[digits-i-1]=number_reverse[i];
//cout<<number[digits-i-1]<<" ";
}
head=0; tail=digits-1;
makePalindrome(head,tail);
cout<<"Answer : ";
for (int i = 0; i < digits; ++i)
{
cout<<number[i];
}
cout<<"\n";
return 0;
}
When I am running with an input : 94187978322, it is giving two different answers with and without a "cout" line (line with comment "PROBLEM").
Here's the output:
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
Answer : 94188078149
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
i = 7 i = 6 i = 4 Answer : 94188088149
The second output is the desired output. Can you point out the cause of this difference and incorrectness for the first one ?
I figured half of it out, it was a logical error.
I should have written return adjust(i-1).
I still don't know the cause for the peculiarity (There was no compilation or runtime error) but at least my code is running as desired now.
i was solving the problem
https://www.spoj.pl/problems/ACPC11A/
and here is my code :
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main()
{
int tc,i,n;
scanf("%d",&tc);
while(tc--)
{
vector<string> v1,v2;
string str,w;
scanf("%d",&n);
int flag=0;
for(i=0;i<n;i++)
{
cin>>str;
if(str[0]!='#')
{
flag=1;
w=str;
}
else if(flag==0)
{
v1.push_back(str);
}
else
v2.push_back(str);
}
//print v2-->w-->v1
for(i=0;i<v2.size();i++)
{
cout<<v2[i]<<" ";
}
if(w!="")
cout<<w<<" ";
for(i=0;i<v1.size()-1;i++)
cout<<v1[i]<<" ";
cout<<v1[v1.size()-1]<<endl;
v1.clear();v2.clear();str.clear();w.clear();
}
return 0;
}
i am getting the correct output for the sample test case...but on submission my code gives segmentation fault.
my logic is simple..
i took 2 vectors 1 for storing words before a English word arrives(v1) and other for storing worlds after a English word arrives(v2)
after that i print the contents of v2 followed by word and then content of v1.
please help me in understanding why is this code giving segmentation fault.
don't bother guys...i got my mistake
Error is in line for(i=0;i<v1.size()-1;i++)
when v1.size() is 0 ,then as size() returns unsigned value...hence 0-1 will be very large value and hence the SIGSEGV