JXNVCE ALGO-LOG YouJin Jung

GFG BIRTHDAY PARADOX

#include<stdio.h>

int main(){
    float num = 365;
    float denom = 365;
    float pr;
    int n = 0;
    
    printf("Probability to find : ");
    scanf("%f", &pr);
    float p = 1;
    
    while (p > pr){
        p *= (num/denom);
        num--;
        n++;
    }
 
    printf("\nTotal no. of people out of which there is %0.1f probability that two of them have same birthdays is %d ", p, n);
    return 0;
}

GFG FOUR POINTS FORMING A SQUARE

#include <iostream>
using namespace std;
 
struct Point {
    int x, y;
};

int distSq(Point p, Point q) {
    return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
 
bool isSquare(Point p1, Point p2, Point p3, Point p4) {
    int d2 = distSq(p1, p2);
    int d3 = distSq(p1, p3);
    int d4 = distSq(p1, p4);
 
    if (d2 == 0 || d3 == 0 || d4 == 0)   
        return false;
 
    if (d2 == d3 && 2 * d2 == d4 && 2 * distSq(p2, p4) == distSq(p2, p3))
        return true;
    
    if (d3 == d4 && 2 * d3 == d2 && 2 * distSq(p3, p2) == distSq(p3, p4))
        return true;
    
    if (d2 == d4 && 2 * d2 == d3 && 2 * distSq(p2, p3) == distSq(p2, p4))
        return true;
    
 
    return false;
}
 
int main() {
    Point p1 = { 20, 10 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 };
    isSquare(p1, p2, p3, p4) ? cout << "Yes" : cout << "No";
    return 0;
}

GFG NUMBER OF BITS FLIPPED FOR NUMBER CONVERSION

#include <iostream>
using namespace std;

int countSetBits(int n) {
    int count = 0;
    while (n > 0) {
        count++;
        n &= (n-1);
    }
    return count;
}
 
int FlippedCount(int a, int b) {
    return countSetBits(a^b);
}
 
int main() {
    int a = 10;
    int b = 20;
    cout << FlippedCount(a, b)<<endl;
    return 0;
}

GFG CLOCK HAND ANGLE

시침과 분침 사이의 각도 재기! 📐

#include <bits/stdc++.h>
using namespace std;
 
int min(int x, int y) {
    return (x < y)? x: y;
     
}
 
int calcAngle(double h, double m) {
    if (h <0 || m < 0 || h >12 || m > 60)
        printf("Wrong input");
 
    else if (h == 12) h = 0;
    else if (m == 60) {
      m = 0;
      h += 1;
      if(h>12) h = h-12;
     }

    float hour_angle = 0.5 * (h * 60 + m);
    float minute_angle = 6 * m;
    float angle = abs(hour_angle - minute_angle);
    angle = min(360 - angle, angle);
 
    return angle;
}
 
int main() {
    cout << calcAngle(9, 60) << endl;
    cout << calcAngle(3, 30) << endl;
    return 0;
}

GFG SIEVE OF ERATHOSTHENES

#include <bits/stdc++.h>

using namespace std;
 
void SieveOfEratosthenes(int n) {
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }

    for (int p = 2; p <= n; p++)
        if (prime[p])
            cout << p << " ";
}

int main() {
    int n = 30;
    cout << "Following are the prime numbers smaller " << " than or equal to " << n << endl;
    SieveOfEratosthenes(n);
    return 0;
}