1.QUESTION:
Consider a staircase of size :
#
##
###
####
Observe that its base and height are both equal to , and the image is drawn using
#
symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size .
SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,j,k;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=n-1;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
printf("#");
printf("\n");
}
return 0;
}
2.QUESTION:
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
SOLUTION:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
long long int a1,a;
long long int b1,b;
long long int c1,c;
long long int d1,d;
long long int e1,e;
scanf("%lld %lld %lld %lld %lld",&a1,&b1,&c1,&d1,&e1);
a=b1+c1+d1+e1;
b=a1+c1+d1+e1;
c=b1+a1+d1+e1;
d=b1+c1+a1+e1;
e=b1+c1+d1+a1;
if(a==b&&b==c&&c==d&&d==e)
printf("%lld %lld",a,b);
else
{
if((a<b&&a<c&&a<d&&a<e))
printf("%lld ",a);
else if((b<a&&b<c&&b<d&&b<e))
printf("%lld ",b);
else if((c<a&&c<b&&c<d&&c<e))
printf("%lld ",c);
else if((d<b&&d<c&&d<a&&d<e))
printf("%lld ",d);
else if((e<b&&e<c&&e<d&&e<a))
printf("%lld ",e);
if((a>b&&a>c&&a>d&&a>e))
printf("%lld ",a);
else if((b>a&&b>c&&b>d&&b>e))
printf("%lld ",b);
else if((c>a&&c>b&&c>d&&c>e))
printf("%lld ",c);
else if((d>b&&d>c&&d>a&&d>e))
printf("%lld ",d);
else if((e>b&&e>c&&e>d&&e>a))
printf("%lld ",e);
}
return 0;
}
3.QUESTION
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
SOLUTION:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
long long int a1,a;
long long int b1,b;
long long int c1,c;
long long int d1,d;
long long int e1,e;
scanf("%lld %lld %lld %lld %lld",&a1,&b1,&c1,&d1,&e1);
a=b1+c1+d1+e1;
b=a1+c1+d1+e1;
c=b1+a1+d1+e1;
d=b1+c1+a1+e1;
e=b1+c1+d1+a1;
if(a==b&&b==c&&c==d&&d==e)
printf("%lld %lld",a,b);
else
{
if((a<b&&a<c&&a<d&&a<e))
printf("%lld ",a);
else if((b<a&&b<c&&b<d&&b<e))
printf("%lld ",b);
else if((c<a&&c<b&&c<d&&c<e))
printf("%lld ",c);
else if((d<b&&d<c&&d<a&&d<e))
printf("%lld ",d);
else if((e<b&&e<c&&e<d&&e<a))
printf("%lld ",e);
if((a>b&&a>c&&a>d&&a>e))
printf("%lld ",a);
else if((b>a&&b>c&&b>d&&b>e))
printf("%lld ",b);
else if((c>a&&c>b&&c>d&&c>e))
printf("%lld ",c);
else if((d>b&&d>c&&d>a&&d>e))
printf("%lld ",d);
else if((e>b&&e>c&&e>d&&e>a))
printf("%lld ",e);
}
return 0;
}
3.QUESTION
Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where is the start point and is the end point. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point and the orange tree is at point .

When a fruit falls from its tree, it lands units of distance from its tree of origin along the -axis. A negative value of means the fruit fell units to the tree's left, and a positive value of means it falls units to the tree's right.
Given the value of for apples and oranges, can you determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )? Print the number of apples that fall on Sam's house as your first line of output, then print the number of oranges that fall on Sam's house as your second line of output.
SOLUTION
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n;
scanf("%d %d",&m,&n);
int *apple = malloc(sizeof(int) * m);
for(int apple_i = 0; apple_i < m; apple_i++){
scanf("%d",&apple[apple_i]);
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
}
int i,x,c=0,y,c1=0;
for(i=0;i<m;i++)
{
x=apple[i]+a;
if(x>=s&&x<=t)
c++;
}
for(i=0;i<n;i++)
{
y=b+orange[i];
if(y<=t&&y>=s)
c1++;
}
printf("%d\n%d",c,c1);
return 0;
}
4.QUESTION
There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location and moves at a rate of meters per jump. The second kangaroo starts at location and moves at a rate of meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?
SOLUTION
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int x1;
int v1;
int x2;
int v2,f=2;
scanf("%d %d %d %d",&x1,&v1,&x2,&v2);
while(1)
{
x1=x1+v1;
x2=x2+v2;
if(x1==x2)
{
f=1;
break;
}
else if(x1>x2){
f=0;
break;
}
}
if(f==0)
printf("NO");
else
printf("YES");
return 0;
}
5.QUESTION
You are given an array of integers, , and a positive integer, . Find and print the number of pairs where and + is evenly divisible by .
SOLUTION
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int i,j,n;
int k,c=0,a[100000];
scanf("%d %d",&n,&k);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
int t=a[i]+a[j];
if(t%k==0)
{
c++;
//printf("%d %d \n",i,j);
}
}
}
printf("%d",c);
return 0;
}
6.QUESTION
Anna and Brian order items at a restaurant, but Anna declines to eat any of the item (where ) due to an allergy. When the check comes, they decide to split the cost of all the items they shared; however, Brian may have forgotten that they didn't split the item and accidentally charged Anna for it.
You are given , , the cost of each of the items, and the total amount of money that Brian charged Anna for her portion of the bill. If the bill is fairly split, print
Bon Appetit
; otherwise, print the amount of money that Brian must refund to Anna.
SOLUTION
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,p,t,s=0,i;
cin>>n>>p;
int a[n];
for(i=0;i<n;i++){
cin>>a[i];
s=s+a[i];
}
cin>>t;
int x=s-a[p];
int y=x/2;
if(t>y)
cout<<t-y;
else
cout<<"Bon Appetit";
return 0;
}
7.QUESTION
John's clothing store has a pile of loose socks where each sock is labeled with an integer, , denoting its color. He wants to sell as many socks as possible, but his customers will only buy them in matching pairs. Two socks, and , are a single matching pair if .
Given and the color of each sock, how many pairs of socks can John sell?
SOLUTION
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n,i,s=0;
scanf("%d",&n);
int h[100000]={0};
int *c = malloc(sizeof(int) * n);
for(int c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
h[c[c_i]]++;
}
for(i=0;i<n;i++)
{
if(h[c[i]]!=0)
{
s=s+(h[c[i]]/2);
h[c[i]]=0;
}
}
printf("%d",s);
return 0;
}
8.QUESTION
The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.
Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How tall will her tree be after growth cycles?
SOLUTION
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,b=1,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j<n;j++)
{
for(i=1;i<=a[j];i++)
{
if(i%2==1)
{
b=b*2;
}
else
{
b=b+1;
}
}
printf("%d\n",b);
b=1;
}
return 0;
}
9.QUESTION
A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, he decides to cancel class if fewer than students are present when class starts.
Given the arrival time of each student, determine if the class is canceled.
SOLUTION
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t,i,j,count=0,n,m;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d%d",&n,&m);
int a[n];
for(j=0;j<n;j++)
{
scanf("%d",&a[j]);
if(a[j]<=0)
{
count++;
}
}
if(count>=m)
printf("NO\n");
else
{
printf("YES\n");
}
count=0;
}
return 0;
}
10.QUESTION
Lily likes to play games with integers and their reversals. For some integer , we define to be the reversal of all digits in . For example, , , and .
Logan wants to go to the movies with Lily on some day satisfying , but he knows she only goes to the movies on days she considers to be beautiful. Lily considers a day to be beautiful if the absolute value of the difference between and is evenly divisible by .
Given , , and , count and print the number of beautiful days when Logan and Lily can go to the movies.
SOLUTION
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b,n,y;
scanf("%d%d%d",&a,&b,&n);
int i,t,x,s=0,c=0;
for(i=a;i<=b;i++)
{
t=i;
s=0;
while(t!=0)
{
x=t%10;
s=(s*10)+x;
t=t/10;
}
y=abs(s-i);
if(y%n==0)
{
c++;
}
}
printf("%d",c);
return 0;
}
No comments:
Post a Comment