-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from rgarg-2711/master
adding Drawing_Book Problem to Add CPP README #8
- Loading branch information
Showing
4 changed files
with
343 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
Problem: | ||
A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed. | ||
The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy. | ||
For example, there are prisoners and pieces of candy. The prisoners arrange themselves in seats numbered to . Let's suppose two is drawn from the hat. Prisoners receive candy at positions . The prisoner to be warned sits in chair number . | ||
Function Description | ||
Complete the saveThePrisoner function in the editor below. It should return an integer representing the chair number of the prisoner to warn. | ||
saveThePrisoner has the following parameter(s): | ||
n: an integer, the number of prisoners | ||
m: an integer, the number of sweets | ||
s: an integer, the chair number to begin passing out sweets from | ||
Input Format | ||
Problem: | ||
The first line contains an integer, , denoting the number of test cases. | ||
The next lines each contain space-separated integers: | ||
- : the number of prisoners | ||
- : the number of sweets | ||
- : the chair number to start passing out treats at | ||
Constraints | ||
Output Format | ||
For each test case, print the chair number of the prisoner who receives the awful treat on a new line. | ||
Sample Input 0 | ||
2 | ||
5 2 1 | ||
5 2 2 | ||
Sample Output 0 | ||
2 | ||
3 | ||
Explanation 0 | ||
In first query, there are prisoners and sweets. Distribution starts at seat number . Prisoners in seats numbered and get sweets. Warn prisoner . | ||
In the second query, distribution starts at seat so prisoners in seats and get sweets. Warn prisoner . | ||
Sample Input 1 | ||
2 | ||
7 19 2 | ||
3 7 3 | ||
Sample Output 1 | ||
6 | ||
3 | ||
Explanation 1 | ||
In the first test case, there are prisoners, sweets and they are passed out starting at chair . The candies go all around twice and there are more candies passed to each prisoner from seat to seat . | ||
In the second test case, there are prisoners, candies and they are passed out starting at seat . They go around twice, and there is one more to pass out to the prisoner at seat . | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
char** split_string(char*); | ||
|
||
// Complete the saveThePrisoner function below. | ||
/*int saveThePrisoner(int n, int m, int s) { | ||
} | ||
int main() | ||
{ | ||
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | ||
char* t_endptr; | ||
char* t_str = readline(); | ||
int t = strtol(t_str, &t_endptr, 10); | ||
if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); } | ||
for (int t_itr = 0; t_itr < t; t_itr++) { | ||
char** nms = split_string(readline()); | ||
char* n_endptr; | ||
char* n_str = nms[0]; | ||
int n = strtol(n_str, &n_endptr, 10); | ||
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } | ||
char* m_endptr; | ||
char* m_str = nms[1]; | ||
int m = strtol(m_str, &m_endptr, 10); | ||
if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILURE); } | ||
char* s_endptr; | ||
char* s_str = nms[2]; | ||
int s = strtol(s_str, &s_endptr, 10); | ||
if (s_endptr == s_str || *s_endptr != '\0') { exit(EXIT_FAILURE); } | ||
int result = saveThePrisoner(n, m, s); | ||
fprintf(fptr, "%d\n", result); | ||
} | ||
fclose(fptr); | ||
return 0; | ||
}*/ | ||
int main() | ||
{ | ||
long int t; | ||
scanf("%ld",&t); | ||
while(t!=0) | ||
{ | ||
long int n,m,s,d,ans; | ||
scanf("%ld%ld%ld",&n,&m,&s); | ||
if(m>=n) | ||
{ | ||
d=m%n; | ||
ans=s+d-1; | ||
|
||
} | ||
else | ||
{ | ||
ans=m+s-1; | ||
} | ||
if(ans<=n&&ans>=1) | ||
{ | ||
printf("%ld\n",ans); | ||
} | ||
if(ans<1) | ||
{ | ||
printf("%ld\n",n); | ||
} | ||
if(ans>n) | ||
{ | ||
printf("%ld\n",(ans-n)); | ||
} | ||
t--; | ||
} | ||
return 0; | ||
} | ||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { break; } | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } | ||
|
||
size_t new_length = alloc_length << 1; | ||
data = realloc(data, new_length); | ||
|
||
if (!data) { break; } | ||
|
||
alloc_length = new_length; | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
} | ||
|
||
data = realloc(data, data_length); | ||
|
||
return data; | ||
} | ||
|
||
char** split_string(char* str) { | ||
char** splits = NULL; | ||
char* token = strtok(str, " "); | ||
|
||
int spaces = 0; | ||
|
||
while (token) { | ||
splits = realloc(splits, sizeof(char*) * ++spaces); | ||
if (!splits) { | ||
return splits; | ||
} | ||
|
||
splits[spaces - 1] = token; | ||
|
||
token = strtok(NULL, " "); | ||
} | ||
|
||
return splits; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
quesn:https://www.hackerrank.com/challenges/drawing-book/problem | ||
Problem: | ||
A teacher asks the class to open their books to a page number. A student can either start turning pages from the front of the book or from the back of the book. They always turn pages one at a time. When they open the book, page is always on the right side: | ||
image | ||
When they flip page , they see pages and . Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is pages long, and a student wants to turn to page , what is the minimum number of pages to turn? They can start at the beginning or the end of the book. | ||
Given and , find and print the minimum number of pages that must be turned in order to arrive at page . | ||
Example | ||
Untitled Diagram(4).png | ||
Using the diagram above, if the student wants to get to page , they open the book to page , flip page and they are on the correct page. If they open the book to the last page, page , they turn page and are at the correct page. Return . | ||
Function Description | ||
Complete the pageCount function in the editor below. | ||
pageCount has the following parameter(s): | ||
int n: the number of pages in the book | ||
int p: the page number to turn to | ||
Returns | ||
int: the minimum number of pages to turn | ||
Input Format | ||
The first line contains an integer , the number of pages in the book. | ||
The second line contains an integer, , the page to turn to. | ||
Constraints | ||
Sample Input 0 | ||
6 | ||
2 | ||
Sample Output 0 | ||
1 | ||
Explanation 0 | ||
If the student starts turning from page , they only need to turn page: | ||
Untitled Diagram(6).png | ||
If a student starts turning from page , they need to turn pages: | ||
Untitled Diagram(3).png | ||
Return the minimum value, . | ||
Sample Input 1 | ||
5 | ||
4 | ||
Sample Output 1 | ||
0 | ||
Explanation 1 | ||
If the student starts turning from page , they need to turn pages: | ||
Untitled Diagram(4).png | ||
If they start turning from page , they do not need to turn any pages: | ||
Untitled Diagram(5).png | ||
Return the minimum value, . | ||
*/ | ||
|
||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int pagecount(int n,int p); | ||
int main() | ||
{ | ||
cin>>n>>p; | ||
ans=pagecount(n,p); | ||
cout<<ans; | ||
return 0; | ||
} | ||
int pagecount(int n,int p) | ||
{ | ||
int g; | ||
if(n%2==0) | ||
{ | ||
if(p<=(n/2)&&p%2==0) | ||
{ | ||
g=(p/2); | ||
} | ||
if(p<=(n/2)&&p%2!=0) | ||
{ | ||
g=(p-1)/2; | ||
} | ||
if(p>(n/2)&&p%2==0) | ||
{ | ||
g=(n/2)-(p/2); | ||
} | ||
if(p>(n/2)&&p%2!=0) | ||
{ | ||
g=(n/2)-((p-1)/2); | ||
} | ||
} | ||
if(n%2!=0) | ||
{ | ||
if(p<=((n-1)/2)&&p%2==0) | ||
{ | ||
g=(p/2); | ||
} | ||
if(p<=((n-1)/2)&&p%2!=0) | ||
{ | ||
g=(p-1)/2; | ||
} | ||
if(p>((n-1)/2)&&p%2==0) | ||
{ | ||
g=((n-1)/2)-(p/2); | ||
} | ||
if(p>((n-1)/2)&&p%2!=0) | ||
{ | ||
g=((n-1)/2)-((p-1)/2); | ||
} | ||
} | ||
return(g); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters