Codeforces Sheet #1

💻 Codeforces Sheet #1 (Data Type - Conditions) Solutions

This post contains the problem list and accepted C code solutions for Assiut University Training - Newcomers Sheet #1, focused on Data Types and Conditional statements. The design is fully responsive for all devices.

📝 Problem List

Problem Name
**A**Say Hello With C++
**B**Basic Data Types
**C**Simple Calculator
**D**Difference
**E**Area of a Circle
**F**Digits Summation
**G**Summation from 1 to N
**H**Two numbers
**I**Welcome for you with Conditions
**J**Multiples
**K**Max and Min
**L**The Brothers
**M**Capital or Small or Digit
**N**Char
**O**Calculator
**P**First digit!
**Q**Coordinates of a Point
**R**Age in Days
**S**Interval
**T**Sort Numbers
**U**Float or int
**V**Comparison
**W**Mathematical Expression
**X**Two Intervals
**Y**The last 2 digits
**Z**Hard Compare

💡 Accepted Solutions (C Code)

Problem A: Say Hello With C++

#include <stdio.h>
int main() {
char s[500];
scanf("%s",s);
printf("Hello, %s\n",s);
return 0;
}

Problem B: Basic Data Types

#include <stdio.h>
int main() {
int a;
long long int b;
char c;
float d;
double e;
scanf("%d%lld %c%f%f",&a,&b, &c, &d, &e);
printf("%d\n%lld\n%c\n%.2f\n%.1f\n", a, b, c, d, e);
return 0;
}

Problem C: Simple Calculator

#include <stdio.h>
int main() {
long long X,Y;
scanf("%lld%lld", &X ,&Y);
printf("%lld + %lld = %lld\n",X,Y,X+Y);
printf("%lld * %lld = %lld\n",X,Y,X*Y);
printf("%lld - %lld = %lld\n",X,Y,X-Y);
return 0;
}

Problem D: Difference

#include <stdio.h>
int main() {
long long int A,B,C,D;
scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
printf("Difference = %lld", (A*B)-(C*D));
return 0;
}

Problem E: Area of a Circle

#include <stdio.h>
#define pi 3.141592653
int main() {
double R;
scanf("%lf",&R); 
printf("%.9f\n",pi*R*R);
return 0;
}

Problem F: Digits Summation

#include <stdio.h>
int main() {
long long int a,b;
scanf("%lld%lld",&a,&b);
printf("%lld", a%10+b%10);
return 0;
}

Problem G: Summation from 1 to N

#include <stdio.h>
int main() {
long long int N, Sum;
scanf("%lld", &N);
Sum = N * (N + 1) / 2;
printf("%lld", Sum);
return 0;
}

Problem H: Two numbers

#include <stdio.h>
#include <math.h>
int main() {
float a,b;
scanf("%f%f",&a,&b);
printf("floor %.0f / %.0f = %.0f\n", a, b, floor(a/b));
printf("ceil %.0f / %.0f = %.0f\n", a, b, ceil(a/b));
printf("round %.0f / %.0f = %.0f\n", a, b, round(a/b));
return 0;
}

Problem I: Welcome for you with Conditions

#include <stdio.h>
int main() {
int A,B;
scanf("%d%d", &A, &B);
if (A >= B){
printf("Yes");
}
else{
printf("No");
}
return 0;
}

Problem J: Multiples

#include <stdio.h>
int main() {
int A,B;
scanf("%d%d",&A,&B);
if (A % B == 0 || B % A == 0){
printf("Multiples");
}
else{
printf("No Multiples");
}
return 0;
}

Problem K: Max and Min

#include <stdio.h>
int main() {
int A,B,C;
scanf("%d%d%d", &A, &B, &C);

if (A <= B && A <= C) {
printf("%d ", A);
}
else if (B <= C && B <= A){
printf("%d ", B);
}
else if (C <= A && C <= B){
printf("%d ", C);
}

if (A >= B && A >= C) {
printf("%d\n",A);
}
else if (B >= C && B >= A){
printf("%d\n",B);
}
else if (C >= A && C >= B){
printf("%d\n", C);
}
return 0;
}

Problem L: The Brothers

#include <stdio.h>
#include <string.h>
int main() {
char F1[100], S1 [100], F2[100],S2[100];
scanf("%s %s", F1, S1);
scanf("%s %s", F2, S2);
if (strcmp(S1, S2) == 0) {
printf("ARE Brothers\n");
}
else{
printf("NOT\n");
}
return 0;
}

Problem M: Capital or Small or Digit

#include <stdio.h>
int main(){
char X;
scanf("%c",&X);
if (X >= 48 && X <= 57) { 
printf("IS DIGIT");
}
else if (X >= 65 && X <= 90){ 
printf("ALPHA\nIS CAPITAL");
}
else if (X >= 97 && X <= 122) { 
printf("ALPHA\nIS SMALL");
}
return 0;
}

Problem N: Char

#include <stdio.h>
int main(){
char X;
scanf("%c",&X);
if (X >= 'a' && X <= 'z'){ 
printf("%c",X-32); 
}
else if (X >= 'A' && X <= 'Z'){ 
printf("%c",X+32); 
}
else {
printf("Not an Alphabet");
}
return 0;
}

Problem O: Calculator

#include <stdio.h>
int main(){
int A,B;
char S;
scanf("%d %c %d", &A, &S, &B);
switch (S){
case '+': 
printf("%d\n", A+B);
break;
case '-': 
printf("%d\n", A-B);
break;
case '*': 
printf("%d\n", A*B);
break;
case '/': 
printf("%d\n", A/B);
break;
default: 
printf("Invalid Operator\n");
}
return 0;
}

Problem P: First digit!

#include <stdio.h>
int main(){
int X, First_Digit;
scanf("%d",&X);
First_Digit=X/1000;
if (First_Digit % 2 == 0){
printf("EVEN");
}
else {
printf("ODD");
}
return 0;
}

Problem Q: Coordinates of a Point

#include <stdio.h>
int main(){
float x,y;
scanf("%f%f",&x,&y);
if (x == 0 && y == 0){
printf("Origem\n");
}
else if(x == 0){
printf("Eixo Y\n");
}
else if(y == 0){
printf("Eixo X\n");
}
else if (x > 0 && y > 0) {
printf("Q1\n");
}
else if (x < 0 && y > 0){
printf("Q2\n");
}
else if (x < 0 && y < 0){
printf("Q3\n");
}
else if (x > 0 && y < 0){
printf("Q4\n");
}
return 0;
}

Problem R: Age in Days

#include <stdio.h>
int main(){
int n, year, month, day;
scanf("%d",&n);
year = n / 365;
n = n % 365;
month = n / 30;
day = n % 30;
printf("%d years\n", year);
printf("%d months\n", month);
printf("%d days\n", day);
return 0;
}

Problem S: Interval

#include <stdio.h>
int main(){
float x;
scanf("%f",&x);
if (x >= 0 && x <= 25) {
printf("Interval [0,25]\n");
}
else if (x > 25 && x <= 50){
printf("Interval (25,50]\n");
}
else if (x > 50 && x <= 75){
printf("Interval (50,75]\n");
}
else if (x > 75 && x <= 100) {
printf("Interval (75,100]\n");
}
else {
printf("Out of Intervals\n");
}
return 0;
}

Problem T: Sort Numbers

#include <stdio.h>
int main() {
int a, b, c;
int A, B, C; 
int temp;

scanf("%d%d%d", &a, &b, &c);

A = a;
B = b;
C = c;

if (a > b) { temp = a; a = b; b = temp; }
if (a > c) { temp = a; a = c; c = temp; }
if (b > c) { temp = b; b = c; c = temp; }

printf("%d\n%d\n%d\n", a, b, c); 
printf("\n");
printf("%d\n%d\n%d\n", A, B, C); 
return 0;
}

Problem U: Float or int

#include <stdio.h>
int main() {
float n;
int a;
float b;
scanf("%f",&n);
a = n;
b = n - a;
if (b == 0) {
printf("int %d\n", a);
}
else {
printf("float %d %.3f\n", a, b);
}
return 0;
}

Problem V: Comparison

#include <stdio.h>
int main() {
int A, B;
char S;
scanf("%d %c %d", &A, &S, &B);
switch (S) {
case '>': 
    if (A > B) {
        printf("Right\n");
    } else {
        printf("Wrong\n");
    }
    break;
case '<':
    if (A < B) {
        printf("Right\n");
    } else {
        printf("Wrong\n");
    }
    break;
case '=':
    if (A == B) {
        printf("Right\n");
    } else {
        printf("Wrong\n");
    }
    break;
default:
    printf("Invalid operator\n");
}
return 0;
}

Problem W: Mathematical Expression

#include <stdio.h>
int main() {
int a, b, c;
char S, Q;
scanf("%d %c %d %c %d", &a, &S, &b, &Q, &c);
switch (S) {
case '+': 
    if (a + b == c) {
        printf("Yes\n");
    } else {
        printf("%d\n", a + b);
    }
    break;
case '-':
    if (a - b == c) {
        printf("Yes\n");
    } else {
        printf("%d\n", a - b);
    }
    break;
case '*':
    if (a * b == c) {
        printf("Yes\n");
    } else {
        printf("%d\n", a * b);
    }
    break;
default:
    printf("Invalid Operator\n");
}
return 0;
}

Problem X: Two Intervals

#include <stdio.h>
int main() {
long long l1, r1, l2, r2;
long long left, right;
scanf("%lld %lld %lld %lld", &l1, &r1, &l2, &r2);

if (l1 >= l2) {
    left = l1;
} else {
    left = l2;
}

if (r1 <= r2) {
    right = r1;
} else {
    right = r2;
}

if (left <= right) {
    printf("%lld %lld\n", left, right);
} else {
    printf("-1\n");
}
return 0;
}

Problem Y: The last 2 digits

#include <stdio.h>
int main() {
long long a, b, c, d, result=1;
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);

result = (a % 100) * (b % 100) * (c % 100) * (d % 100);

printf("%02lld\n", result % 100);
return 0;
}

Problem Z: Hard Compare

#include <stdio.h>
#include <math.h>
int main() {
long long a, b, c, d;
double left, right;
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);

// Compare a^b vs c^d by comparing b*log(a) vs d*log(c)
left = (double)b * log(a);
right = (double)d * log(c);

if (left > right) {
    printf("YES\n");
} else {
    printf("NO\n");
}
return 0;
}
Post a Comment (0)
Previous Post Next Post