Problem-Solving in C (Part-1): Loop and Array Operations(Recap)
A. Winter Sale
The winter sale began, and Mrs. Sarah wants to buy a T-shirt for her son. The T-shirt has a ticket that indicates a discount of X% and its price P after the discount. She now wants to know the price before the discount to know if the discount was worthy. Help her!
Input
Only one line containing two numbers X and P (1≤X≤99,1≤P≤4×10⁴) — the discount percentage, and the price of the T-shirt after the discount.
Output
Print the price of the T-shirt before the discount rounded up to two decimal places.
Examples
input
20 80
output
100.00
input
15 20
output
23.53
Try on your own first.
Answer:
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int x, a;
scanf("%d %d", &x, &a);
float sum= 1-(x/100.0);
float price= a/sum;
printf("%.2f\n", price);
return 0;
}
B. Memo and Momo
Memo and Momo are playing a game. Memo will choose a positive number a, and Momo will choose a positive number b.
Your task is to tell them who will win according to the following rules:
- If both a and b are divisible by k, both of them win and you should print “Both”.
- If a is divisible by k but b isn’t, Memo wins and you should print “Memo”.
- If b is divisible by k but a isn’t, Momo wins and you should print “Momo”.
- If both a and b are not divisible by k, no one wins and you should print “No One”.
Input
Only one line containing three positive numbers a, b and k (1≤a,b,k≤10¹⁸).
Output
Print the answer as described in the statement.
Examples
input
15 7 3
output
Memo
input
22 10 2
output
Both
Try on your own.
Answer:
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
long long int a, b, k;
scanf("%lld %lld %lld", &a, &b, &k);
if(a%k==0 && b%k==0){
printf("Both\n");
}
else if(a%k==0){
printf("Memo\n");
}
else if(b%k==0){
printf("Momo\n");
}
else{
printf("No One\n");
}
return 0;
}
A. Square or rectangle
Given the width and the height of a shape determine whether it’s for a square or a rectangle?
Input
The first line contains a single integer t(1≤t≤1000)the number of test cases.
The first and only line of each test case contains two integers w,h(1≤w,h≤10⁶)representing the width and the height, respectively.
Output
For each test case print ‘Square’ if the shape is a square otherwise print ‘Rectangle’;
Example
input
3
10 10
13 200
300 300
output
Square
Rectangle
Square
Try yourself.
Answer:
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int test;
scanf("%d", &test);
for(int i=1; i<=test;i++){
int width, height;
scanf("%d %d", &width, &height);
if(width==height){
printf("Square\n");
}
else{
printf("Rectangle\n");
}
}
return 0;
}
Codeforces Problem Links:
- A. Winter Sale (use float for this problem, ignore double as codeforces C compiler have problem with double data type)
- B. Memo and Momo
- A. Square or rectangle
Please leave a clap and comment on my work. Your feedback will motivate me to create and share more content. Thank you!