I am getting SIGCONT error while running the code in codechef ide whereas on my local ide the same code runs fine.
The problem code is "TYPING" and it is from Snackdown Practice Contest: Beginner.
The code is below:
#include <iostream>
#include <iterator>
#include <map>
#include <string>
typedef long long ll;
ll FindCount(const char* str,ll num){
char previous =str[0];
ll individual_count=2;
for(ll i =1 ;i<num;i++){
if(previous=='d'||previous=='f'){
if(str[i]=='d'||str[i]=='f'){
individual_count+=4;
}
else{
individual_count+=2;
}
}
else{
if(str[i]=='j'||str[i]=='k'){
individual_count+=4;
}
else{
individual_count+=2;
}
}
previous=str[i];
}
return individual_count;
}
int main()
{
ll t;
std::cin>>t;
while(t--)
{
std::map<std::string,ll> m;
ll count=0;
ll n=0;
std::cin>>n;
while(n--){
std::string str;
std::cin>>str;
std::map<std::string,ll>::iterator itr;
itr=m.find(str);
if(itr!=m.end()){
count+=(itr->second)/2;
}
else{
ll num = str.size();
ll temp = FindCount(str.c_str(),num);
count+=temp;
m.insert({str,temp});
}
}
std::cout<<count<<"\n";
}
return 0;
}
The link to the problem statement is: https://www.codechef.com/SDPCB21/problems/TYPING
I am getting an infinite number of '0' as output each on new line.
Please tell me if there is any logical error or error of any kind in the code.
Thankyou.
Hey guys this is the question's link from hackerrank
hackerrank problem
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long int sizeArr, operation;
cin>>sizeArr>>operation;
long int array[sizeArr];
for(long int i=0;i<sizeArr;i++)
array[i]=0;
for(long int i=0;i<operation;i++)
{
long int a,b,k;
cin>>a>>b>>k;
for(long int j=a-1;j<=b-1;j++)
array[j]+=k;
}
sort(array,array+sizeArr);
cout<<array[sizeArr-1];
}
I coded it like this and the another person code it like
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,M,a,b,k,i,j,max=0,x=0;
cin>>N>>M;
long int *Arr = new long int[N+1]();
for(i=0;i<M;i++)
{
cin>>a>>b>>k;
Arr[a]+=k;
if((b+1)<=N) Arr[b+1]-=k;
}
for(i=1;i<=N;i++)
{
x=x+Arr[i];
if(max<x) max=x;
}
cout<<max;
return 0;
}
Mine one didn't clear all the test cases but the second one's code did. Any suggestions.
The problem is that your colleague has this if((b+1)<=N) Arr[b+1]-=k; which means he sometimes subtracts but in your code, you only add in here array[j]+=k;.
And also your while function has a bigger range than your colleague (for example if a user gives as a=1 and b=3 you go through 0,1,2 but your colleague only goes at 1 and 3. I am not sure that this is the problem but you should check out.
I'm very new to C++, so I've probably made some really stupid mistakes. But I've looked online to solutions for this error, and I tried all I can think of.
I'm trying to have my whole program in one function, because it's going to be merged with other programs. The id error is the only error I have so far.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
void BookingSystem(int time, int k);
int time;
int k = 0;
int main()
{
BookingSystem(time, k);
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
return 0;
}
system ("pause");
return 0;
}
Any help is much appreciated, I've spent a long time trying to fix this on my own with no luck.
Thank you!
You make many mistakes in your program:
1) You mix C & C++ syntax.
using namespace std, is C++
2) You include useless header files
3) You declare variables that I don't understand what for
Why do you need this?
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
You don't use the variables above anywhere !!!!
4) You try to write a function inside main (this is not Pascal).
So, if I understand what you want, look at this:
#include <stdio.h>
void BookingSystem() // this is the function who does all job
{
int time = 0;
while ((time != 7) && (time != 9))
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
printf("\nYou have selected to book your meal for ");
scanf("%d", &time); // read the time
scanf("%*[^\n]"); // consume all caracters until the newline
scanf("%*c"); // consume the newline
if (time == 7)
{
printf("You have selected to book your slot at 7PM\n");
}
else if (time == 9)
{
printf("You have selected to book your slot at 9PM\n");
}
else
{
printf("You have selected an incorrect time, please try again\n");
}
} // end while
} // end function
int main(int argc, char *argv[])
{
BookingSystem(); // this is the calling to your function
return 0;
}
If I understand what you want, this works fine.
you can not to Define Function in to the main ..
you must to do like this...
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int , int , int ); //just say data type int or ....
int sevenbooking(int , int , int );
void BookingSystem(int , int);
int main() //and in to main just call he...
{
int time;
int k = 0;
BookingSystem(time, k);//call ....and send parameter time and k
system ("PAUSE");
return 0;
}
void BookingSystem(int time,int k)
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
// return 0;//if this function is void he can not to return any thing!!!!
}
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//......
}
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//........
}
the output I'm getting for the given code is "0" even though I
initialized the value of c as "1"...can somebody explain it...
Why is the value of "C" changing in the following code??
#include <iostream>
using namespace std;
int c=1; // global initialized 'c' as 1..
long long f(long long n){
if(n==6){
return 2;
}
else{
c=c+1;
f(n-2);
}
}
int main()
{
long long n,ans,p;
cin>>n;
ans=f((2*n)-2);
cout<<c; //printing out the value of 'c'
return 0;
}
Because value of c is being changed in below code :
else{
c=c+1;
f(n-2);
}
I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){