Sei sulla pagina 1di 18

P 304 - The Electronics Store

There is a queue of n people in front of the local electronics store. All of them have their phones
at various battery percentages and are looking to buy a portable charger quickly before their
battery runs out. The server at the counter understands this and he doesn’t want any of his
customers’ phones to run out of charge. He, being an algorithmist, quickly determines that he
wants to implement bubble sort technique to sort the customers in the queue, such that, the
person whose phone is going to die the quickest, comes to the front of the line and gets a portable
charger.

The server at the counter is an algorithmist and not a programmer, hence he asks you to do the
job for him.

Formally, sort an array using bubblesort.

INPUT

The first line of input is n (1≤n≤100), the number of customers in the queue
The second line of input is the battery percentages of the n customers (positive numbers less than
100) each separated by a space.

OUTPUT

Print the battery percentages of the customers in a line after the sorting is completed.

Sample Input 0
8
3 4 5 2 4 10 18 1

Sample Output 0
1 2 3 4 4 5 10 18
Code:-

#include <stdio.h>

int main()

{
int array[100], n, c, d, swap;

scanf("%d", &n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)

for (d = 0 ; d < n - c - 1; d++)

if (array[d] > array[d+1]) /* For decreasing order use < */

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

for (c = 0; c < n; c++)

printf("%d ", array[c]);

return 0;

P 305 - New Customer at the Electronics Store


The server is finally happy that he sorted the line and now he gets to sell his portable chargers.
But alas, a new person enters with a battery percentage of k comes to the store and stands at the
back of the queue. Now he has to fit him in this already ascending ordered queue. He asks for
your help.

Formally, you are given an array of size n where the first n - 1 elements are sorted in ascending
order and the last element can be any value. Sort this array by sliding the last element into its
correct position.

INPUT

The first line of input is n (1≤n≤100), the number of customers in the queue. The second line of
input is the battery percentages of the n customers (positive numbers less than 100)
the first n - 1 of which are standing in ascending order according to their battery percentage.

OUTPUT

Print the battery percentages of the customers in a line after the new person has been put in his
right place in the queue.

Sample Input 0
6
135794
Sample Output 0
134579

Code:-

#include <stdio.h>

int main()

int array[100], n, c, d, swap;

scanf("%d", &n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{

for (d = 0 ; d < n - c - 1; d++)

if (array[d] > array[d+1]) /* For decreasing order use < */

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

for (c = 0; c < n; c++)

printf("%d ", array[c]);

return 0;

P 501 - Orange Partitioning

Tisha went to her orchard and plucked a bunch of oranges. Now she wants to keep aside some of
the oranges to for herself and her children but also sell the biggest ones on the market. She takes
a look at the last orange in her hand and decides that she would like to eat it. She seems to think
it fair that all the oranges smaller than the orange in her hand can go to her children. All the
oranges bigger than this one will be sold on the market. Write a program to help Tisha do this
partition. Assume the oranges are neatly stacked in a row.

Tisha does the following to partition the oranges:

k=0
Check orange number i = 0,1,2,3.. n - 1 :
for ith orange :
if (orange[i] is smaller than or equal to orange[n-1]) :
swap orange[i] with orange[k]
k++
swap orange[k] with orange[n-1]

Simulate the process used by Tisha and print the resultant array of the size of oranges.

INPUT

The first line of input is n (1≤n≤100), the number of oranges Tisha plucked from the orchard
The second line of input are the diameters of the oranges you just took (positive numbers) each
separated by a space. Assume the last orange (orange[n - 1]) to be the one Tisha took in her
hand.

OUTPUT

Print the sizes of the oranges in a row after the partition has been done

Sample Input 0
8
43861195
Sample Output 0
43115698

Code:-

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int main() {

int n,i,t,j=0;

scanf("%d",&n);

int a[n];

for(i=0;i<n;i++)

{
scanf("%d",&a[i]);

int p=a[n-1];

for(i=0;i<n-1;i++)

if(a[i]<=a[n-1])

t=a[i];

a[i]=a[j];

a[j]=t;

j++;

t=a[j];

a[j]=a[n-1];

a[n-1]=t;

for(i=0;i<n;i++)

printf("%d ",a[i]);

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

return 0;

Z 460 Date Sorting


Given a number of dates, sort them in such a way that the date that corresponds to the earliest
day comes first and the date that corresponds to the latest day comes last.

Input Format

First line contains N, the number of dates.


Next N lines contain one date each in the following format :
DD MM YYYY.
The date will be three integers separated by a space where the first integer is the day, the second
integer is the month and the third is the year.

Constraints

1 <= N <= 100


It is guaranteed that the date will be a valid date.

Output Format

Output N lines. Each line must contain one date. The dates must appear in a sorted format.

Sample Input 0
4
9 8 1996
31 4 1995
30 4 1996
25 12 1997
Sample Output 0
31 4 1995
30 4 1996
9 8 1996
25 12 1997

Code:-

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

struct date
{

int day;

int month;

int year;

};

int main()

int N,i,j;

scanf("%d",&N);

struct date input[N];

for(i=0;i<N;i++)

scanf("%d%d%d",&input[i].day,&input[i].month,&input[i].year);

for(i=0;i<N;i++)

for(j=i+1;j<N;j++)

if(input[i].year>input[j].year)

struct date temp = input[i];

input[i] = input[j];

input[j] = temp;

else if (input[i].year == input[j].year && input[i].month > input[j].month)

{
struct date temp = input[i];

input[i] = input[j];

input[j] = temp;

else if (input[i].year == input[j].year && input[i].month == input[j].month && input[i].day >


input[j].day)

struct date temp = input[i];

input[i] = input[j];

input[j] = temp;

for(int i=0; i<N; i++)

printf("%d %d %d\n",input[i].day,input[i].month,input[i].year);

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

return 0;

C PROGRAM TO SOLVE SUDOKU PUZZLE


C PROGRAM TO SOLVE SUDOKU PUZZLE
Here is a C program to solve sudoku. I have manually entered the puzzle in the array, if you want you can
write a two line code to take input from user or just manually change the values in the array before
running it.
Note: 0 represents the empty cell of sudoku puzzle

Here's the Code:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define RESET 0
int sudokuSolver();
int findEmptyCell();
int isValid();
void printGrid();
void inputGrid();

int grid[9][9]= { {0, 0, 5, 3, 0, 0, 0, 0, 0},


{8, 0, 0, 0, 0, 0, 0, 2, 0},
{0, 7, 0, 0, 1, 0, 5, 0, 0},
{4, 0, 0, 0, 0, 5, 3, 0, 0},
{0, 1, 0, 0, 7, 0, 0, 0, 6},
{0, 0, 3, 2, 0, 0, 0, 8, 0},
{0, 6, 0, 5, 0, 0, 0, 0, 9},
{0, 0, 4, 0, 0, 0, 0, 3, 0},
{0, 0, 0, 0, 0, 9, 7, 0, 0}
};

int row,col;
//this variable was used just to keep track of number of recursive calls
long int totalNumOfCalls=0;

void main(){

int i,j,solution=0;
char ch;
clrscr();
printf("You can change the puzzle before running the program \nby changing the values in the
\"grid\" array\n\n");
printf("The Entered Sudoku puzzle is: \n");
printGrid();
printf("Press 'c' to confirm and solve, or 'e' to exit: ");
ch=getch();
if(ch=='e')
exit(0);
else if(ch=='c'){
clrscr();
solution=sudokuSolver();
if(solution){
printf("\nThe Solved Sudoku is: \n\n");
printGrid();
}
else
printf("\nNo Possible Solution!!\n\n");
getch();
}

int findEmptyCell(){
int i,j;

for(i=row;i<=8;i++)
for(j=0;j<=8;j++){
if(grid[i][j]==0)
{
row=i;col=j;
return 1;
}
}
return 0;

int isValid(int cellRow, int cellCol, int num){


int i,j,trow,tcol,trow1,tcol1;
int rowStart = (cellRow/3) * 3;
int colStart = (cellCol/3) * 3;

// to check the presence of number in row and column


for(j=0;j<=8;j++){
if(grid[cellRow][j]==num)
return 0;
if(grid[j][cellCol]==num)
return 0;

}
// to check the presence of number in 3X3 box

for(i=rowStart;i<=rowStart+2;i++)
for(j=colStart;j<=colStart+2;j++)
if(grid[i][j]==num)return 0;

return 1;
}
int sudokuSolver(){
int digit;
int prevRow,prevCol; // for backtracking
totalNumOfCalls++;

if(!findEmptyCell())
return 1;

for(digit=1;digit<=9;digit++){
if(isValid(row,col,digit)){
grid[row][col]=digit;
prevRow=row;prevCol=col;
if(sudokuSolver())
return 1;
//while backtracking assigning previous values to row and col
row=prevRow;col=prevCol;
grid[row][col]=RESET;
}

return 0;

}
void printGrid(){

int i,j;

printf("\t-------------------------\n");
for(i=0;i<9;i++){
printf("\t");
for(j=0;j<9;j++){
if(j==0)
printf("| ");
if(grid[i][j]==0)
printf(". ");
else
printf("%d ",grid[i][j]);
if((j+1)%3==0 )
printf("| ");

if((i+1)%3==0 )
printf("\n\t-------------------------");
printf("\n");

}
}
Sample Input #1:

Sample Output #1:

Sample Input #2:


Sample Output #2:

CCT B3 - Team Queue


Your task is to implement a new data structure called a team queue.

In a team queue, each element is a part of a team. If a new element wants to enter the queue, it
first scans the queue to find his teammates and joins in the queue behind them. If the element has
no teammates already waiting in the queue then he joins at the end of the queue (Bad Luck
Element). Dequeing operation in a team queue is the same as that in a normal queue.
Input Format

The input file will contain one or more test cases. Each test case begins with the number of teams
t (1 ≤ t ≤ 1000).
Then t team descriptions follow, each one consisting of the number of elements belonging to the
team and the elements themselves. Elements are integers in the range 0..999999. A team may
consist of up to 1000 elements.

Finally, a list of commands follows.

There are three different kinds of commands:


• ENQUEUE x — enter element x into the team queue
• DEQUEUE — process the first element and remove it from the queue
• STOP — end of test case
The input will be terminated by a value of 0 for t.

Warning:
A test case may contain up to 200000 (two hundred thousand) commands, so the implementation
of the team queue should be efficient: both enqueing and dequeuing of an element should only
take constant time.
Output Format

For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case.
Then, for each ‘DEQUEUE’ command, print the element which is dequeued on a single line.
Print a blank line after each test case, even after the last one

Sample Input 0
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output 0
Scenario #1
101
102
103
201
202
203

C++ Code:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<queue>

using namespace std;

const int maxn = 1000000;

int id[maxn];

bool f[1002];

int main() {

int t, cas = 1, n, x;

char s[20];

while (scanf("%d",&t)!=EOF&&t!=0)

printf("Scenario #%d\n", cas++);


queue<int> q[1002];

queue<int> team;

for (int i = 1; i <= t; i++)

scanf("%d", &n);

f[i] = false;

for (int j = 0; j < n; j++)

scanf("%d", &x);

id[x] = i;

while (scanf("%s",s)!=EOF&&strcmp(s,"STOP")!=0)

if (!strcmp(s, "ENQUEUE"))

scanf("%d", &x);

if (!f[id[x]])

team.push(id[x]);

f[id[x]] = true;

q[id[x]].push(x);

else
{

int top = team.front();

printf("%d\n", q[top].front());

q[top].pop();

if (q[top].empty())

team.pop();

f[top] = false;

printf("\n");

return 0;

Potrebbero piacerti anche