I have to do homework and have been sitting for 4 hours. I don't know what to do next. The program compiles correctly but spits out the wrong data. He's not finished because I can't get through this problem. I'm just a beginner in c ++. Would anyone be able to help me?
Safe Mountaineers
A group of mountaineers climbs to the top following a narrow trail. Mountaineers are following one another carefully, because the path is steep and dangerous. Some of the climbers have a rope protection, and others unfortunately do not have it. The equipment of the group of mountaineers can be described using a series of letters:
• B means a safe climber (equipped with a rope);
• Z means mountaineer at risk (without rope).
For example, the string BZZBZ means a group of five mountaineers, two of whom have ropes. The group is headed by the first mountaineer on the right (in this case without a rope). Colleagues, however, are not left without help. A climber with a rope can pass the rope to a colleague walking directly in front of him (and then they are both safe), and this gesture is performed at once by all climbers who can do it. So in this case the string will change as follows:
BZZBZ- → BBZBB
Mountaineers who have become safe are marked in red. Such friendly gestures are repeated as long as possible:
BBZBB- → BBBBB
At this point, all mountaineers are safe - after two mate gestures. Of course, it will not be possible for every group of mountaineers to ensure safety for everyone. A mountaineer who walks at the beginning of the group cannot lend his rope to a friend because nobody walks in front of him.
Write a program that for a given group of mountaineers calculates the number of gestures needed to make everyone become safe, or declares that it is impossible.
Entrance
The first line of the standard input contains a positive integer denoting the number of data sets or groups of mountaineers (N≤200).
Each subsequent line contains a series of letters B or Z describing the equipment of mountaineers in a given group (according to the task). Each group has between 1 and 1,000,000 people, and it can be assumed that the total number of all groups will not exceed 20,000,000.
Exit
For each group, the program should write a line of text containing the number of gestures needed that will lead to a situation when all climbers in the group will become safe, or the number -1 if this is impossible.
Example
For the example input below:
3
BZZBBZ
Bzzz
ZBBB
the correct result is:
2
3
-1
Explanation of the example: The first case is described in the body of the task. In the second case, the rope feed proceeds as follows:
BZZZ- BBZZ- → → → BBBB BBBZ-
In the third case, the last climber cannot receive the rope.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int N, przejscia;
string a,b,c;
main()
{
N=3;
przejscia=0;
cout << N<<endl;
a="BZZBB\n";
char a1=(a[1]);
b="ZBBZBZBB\n";
c="BBZZZZBZBZBZ\n";
int ailosc = a.length();
int bilosc = b.length();
int cilosc = c.length();
cout <<a;
cout <<b;
cout <<c;
if (a[0]=='Z')
{cout << "-1";
}
if (b[0]=='Z')
{cout << "-1";
}
if (c[0]=='Z')
{cout << "-1";
}
int h, k;
h=0;
k=0;
if (a[0]=='B')
{for (int i=1; i<=ailosc; i++)
{h++;
if ((a[h]=='Z')and(a[k]=='B'))
{a[h]='B';
przejscia++;
}
k++;
}
}
cout << przejscia;
}
Related
Ok so i have been learning binary search. My teacher has given me a problem on codeforces and it always fails on test 2. Here is the problem:
In this problem jury has some number x, and you have to guess it. The number x is always an integer from 1 and to n, where n
is given to you at the beginning.
You can make queries to the testing system. Each query is a single integer from 1
to n
. Flush output stream after printing each query. There are two different responses the testing program can provide:
the string "<" (without quotes), if the jury's number is less than the integer in your query;
the string ">=" (without quotes), if the jury's number is greater or equal to the integer in your query.
When your program guessed the number x
, print string "! x", where x
is the answer, and terminate your program normally immediately after flushing the output stream.
Your program is allowed to make no more than 25 queries (not including printing the answer) to the testing system.
Input
Use standard input to read the responses to the queries.
The first line contains an integer n
(1≤n≤pow(10,6) — maximum possible jury's number.
Following lines will contain responses to your queries — strings "<" or ">=". The i
-th line is a response to your i-th query. When your program will guess the number print "! x", where x
is the answer and terminate your program.
The testing system will allow you to read the response on the query only after your program print the query for the system and perform flush operation.
Output
To make the queries your program must use standard output.
Your program must print the queries — integer numbers xi
(1≤xi≤n), one query per line (do not forget "end of line" after each xi
). After printing each line your program must perform operation flush.
And here is my code:
#include <iostream>
using namespace std;
int main()
{
int n;
string s;
int k=0;
cin>>n;
int min=1,max=n;
int a;
while(k==0)
{
if(max==min+1)
{
cout<<"! "<<min;
k=1;
break;
}
a=(min+max)/2;
cout<<a<<endl;
cin>>s;
if(s==">=")
min=a;
else
max=a;
}
}
I dont know what test 2 is, but i would be happy to hear ideas as to where my programs is wrong. My guess is its something with the number of guesses. Thanks in advance!
I have tried variations of the loop written above, but they all give the same result.
hello i am a beginner in programming and am in the array lessons ,i just know very basics like if conditions and loops and data types , and when i try to solve this problem.
Problem Description
When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one.
Input Specification
The first line contains a single integer n (1⩽n⩽105) — the length of the string. The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'.
It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either "zero" which corresponds to the digit 00 or "one" which corresponds to the digit 11.
Output Specification
Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.
Sample input:
4
ezor
Output:
0
Sample Input:
10
nznooeeoer
Output:
1 1 0
i got Time limit exceeded on test 10 code forces and that is my code
#include <iostream>
using namespace std;
int main()
{
int n;
char arr[10000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
if (arr[i] == 'n') {
cout << "1"
<< " ";
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == 'z') {
cout << "0"
<< " ";
}
}
}
Your problem is a buffer overrun. You put an awful 10K array on the stack, but the problem description says you can have up to 100K characters.
After your array fills up, you start overwriting the stack, including the variable n. This makes you try to read too many characters. When your program gets to the end of the input, it waits forever for more.
Instead of putting an even more awful 100K array on the stack, just count the number of z's and n's as you're reading the input, and don't bother storing the string at all.
According to the compromise (applicable to homework and challenge questions) described here
How do I ask and answer homework questions?
I will hint, without giving a code solution.
In order to fix TLEs you need to be more efficient.
In this case I'd start by getting rid of one of the three loops and of all of the array accesses.
You only need to count two things during input and one output loop.
I'm trying to write a program that alerts a user if a certain puppy owner has a puppy that is beyond a certain distance from the puppy owner.
Specifically, the following program:
first asks the user to input the location of the owner, namely inputting 2 integers a and b.
then asks the user to input the number of puppies the owner has. This is a positive integer n.
for each puppy i of the n puppies, the program asks the user to input the location of puppy i. This is 2 integers x and y, both of which are of course dependent on i.
If puppy i is calculated to have reached a distance greater than 10 units from its owner, then the program should inform the user by printing i.
Finally, the program should tell the user the total number of puppies whose numbers have been printed. This number is represented by the variable count, a positive integer.
The following is an example case
Inputs:
(Owner location) 2 1
(number puppies) 4
(location puppies) (15 15), (14 -2), (1 3), (0 4)
Outputs:
Puppy 1 and Puppy 2 too far away
Total 2 puppies too far away
When I try to run the program, the program outputs a question mark instead of the instead of the i's. What did I do wrong, please?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
string puppies;
int a,b;
cin>>a>>b;
int n;
cin>>n;
int i,x,y,count=0;
for (i=1;i<=n;i++){
cin>>x>>y;
int dist;
dist=abs(a-x)+abs(b-y);
if (dist>10){
count++;
puppies += i;
}
}
if (count==1){
cout<<"Puppy "<<puppies[0]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppy too far away";
}
if (count>1){
int j;
for (j=0;j<=(count-2);j++){
cout<<"Puppy "<<puppies[j]<<" and"<<" ";
}
cout<<"Puppy "<<puppies[count-1]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppies too far away";
}
if (count==0){
cout<<"No puppies too far away";
}
}
Here is the copied output (for the same case as above)
Puppy and Puppy too far away
Total 2 puppies too far awayPress any key to continue . . .
Here is a screenshot
enter image description here
You're trying to use a std::string as a container for numbers. It can do that for small enough ones, sure, but you'll probably want to switch to std::vector<int>.
The issue you're actually observing is that puppies[0] is a char, which in std::cout << puppies[0] is interpreted as a single text character, which in your case is a low codepoint corresponding to a non-printable character, hence the question mark displayed by your shell. You can fix it by converting explicitly with static_cast<int>(puppies[0]), but again using a suitable container for actual numbers would be more advisable.
puppies += i;
If you take a look at the overload set, you'll find that there is no overload for int. However, there is an overload for char. All integer types are implicitly convertible to other integer types, and in this case int is converted to char. The character that is appended to the string is the one which is represented by the integer value. What integer value represent what character depends on the character set that your system uses.
You may have intended to append the integer in a textual representation instead. You can convert an integer to a string for example using std::to_string.
That said, using string for this purpose seems backwards, as you don't appear to use it as a character string, but more like an array of integers. As such, a vector of integers might be a more sensible choice.
I was given an assignment to make a program in cpp where you give it the width and length of the square and it will go into a pattern of *s and #s but I can't wrap my head around it. So I'm asking you people at stackoverflow to help me if you can. For reference when you would give an input of
6 6
the output would be:
######
#****#
#*##*#
#*##*#
#****#
######
and an input of
4 3
will give an output of:
####
#**#
####
This assignment is meant to help you understand how to use nested for loops.
for (int i = 0; ...)
{
for (int j = 0; ...)
{
// Do stuff here
}
}
Think about how a row can be formed by repeating a specific action. Then think about how a square can be formed by repeating the action of creating a row.
As for '*' vs '#', you always have access to both the x and y coordinate of the "current cell" you are about to print from inside the inner loop, because they are the counter variables for the two for loops. Remember: i and j are just arbitrary names. You could name those variables anything you want. You also have access to the length of each row and column, because they are given as input. Ask yourself: "what test can I make on the x or y coordinate that will tell me if it is the first or last column"?
Looping (or "iteration") is an extremely important concept in programming. Hopefully this gives you a hint in the right direction without giving too much away. :)
I'm a software engineering student, and i need some help with an assignment i was given
i need a code that corrects a sentence written inside out
example;
i love programming
rp evol i gnimmargo
(what i mean by inside out .... that the sentence gets cut into half and each half is flipped)
i need to correct the scrambled sentence
i already started it by counting the charterers in the string that the user enters and cutting the sentence .... but i just cant figure out how to flip each half then join them
any ideas??
#include <iostream>
#include <string>
using namespace std;
//prototype
int NumberOfChar(string testCase);
int main()
{
// declaration
int N, halfCharNum, halfTestCase, size;
string testCase;
// input
cout<<"please enter an integer number, that will represent the number of test cases: "<<endl;
//cin>>N;
cout<< NumberOfChar(testCase)<<endl;
halfCharNum=size/2;
return 0;
}
int NumberOfChar(string testCase)
{
cout << "Enter your string: " <<endl;
getline(cin,testCase);
const int size=testCase.length();
cout << "The total number of characters entered is: " << endl;
return size;
}
This takes all of two lines of C++ code.
1) reverse the string (std::reverse)
2) Rotate the string left n characters where n is half the number of characters in the string. (std::rotate)
First, replace each element in the string with the element on the other end and work your way in.
i.e. in the string "rp evol i gnimmargo" exchange the first character "r" with the last character "o" and then work your way in, next exchanging "p" with "g" and so on.
That leaves you with "ogramming i love pr"
Then, swap the first and second halves of the string.
i.e. "ogramming i love pr" can be split into "ogramming" and " i love pr" -- just swap them for " i love pr" and "ogramming" and combine them
Just use a large dictionary of english words and implement an evolutionary algorithm. Might not be the shortest path to glory but definitely a fun task :-)
Sample dictionaries (you might need anyway) can be found
here,
here,
or here