uri – Learn2Code https://learn2code.musitrature.com Learn to code : A programmers resource Wed, 25 Mar 2020 04:21:52 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.4 https://learn2code.musitrature.com/wp-content/uploads/2020/03/favicon-2.ico uri – Learn2Code https://learn2code.musitrature.com 32 32 174224216 Uri 1024 – Encryption solution https://learn2code.musitrature.com/uri-1024-encryption-solution-c-cpp/?utm_source=rss&utm_medium=rss&utm_campaign=uri-1024-encryption-solution-c-cpp Sat, 21 Mar 2020 11:47:29 +0000 https://learn2code.musitrature.com/?p=286 Read More »Uri 1024 – Encryption solution]]> This is a basic string manipulation problem to create a basic encryption program.

First of all, we see from the given inputs that inputs can be a sentence aka. more than one word in an input. So, we can’t use scanf, cin or any other formatted input functions of c/c++ as those will only take input until space is encountered. Instead, we can use getline function.

The first point of the problem is we have to right shift every alphabets 3 positions to the right. Ex: ‘a’ will be ‘d’, ‘b’ will be ‘e’ etc. This is very easy.

All the characters in c have their own integer value. The characters used in C are stored in a table called the ASCII table. And a specific character’s int value is actually the position of that character in the ASCII table. So, we can easily shift to any number of positions by adding that number to that character. Ex: ‘a’ + 3 = ‘d’.

Now, the second point is to reverse the string. Which is very easy. We can use the built-in C++ library function reverse().

Again we have to shift the modified string like before. But we have to only do that with the latter half of the string. Use this formula if the string length is even then half is length/2 else length/2 + 1.

Then do shifting to the truncated/half of the string. This time left shift by 1. So, like the previous step instead of adding 3 just subtract the char by one this time. Remember this time all characters must be shifted, not only alphabets, unlike the first step.
That is all.

#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main() {
    int t_case;
    string x;
    cin >> t_case;
    getchar();
    while(t_case--) {
        getline(cin,x);
        for(int i = 0; i<x.length(); i++) {
            if((x[i]>='A'&& x[i]<='Z')||(x[i]>='a' && x[i]<='z')) {
                x[i] += 3;
            }
        }
        reverse(x.begin(),x.end());
        if(x.length()%2!=0) {
            for(int i = (x.length()-1)/2; i < x.length(); i++) {
                x[i] -=1;
            }
        } else {
            for(int i = x.length()/2; i < x.length(); i++) {
                x[i] -=1;
            }
        }
        cout << x << endl;
    }

    return 0;
}

]]>
286
URI|1036 Bhaskara’s Formula solution https://learn2code.musitrature.com/uri1036-bhaskaras-formula-solution/?utm_source=rss&utm_medium=rss&utm_campaign=uri1036-bhaskaras-formula-solution Sat, 14 Mar 2020 17:12:56 +0000 http://learn2code.musitrature.com/?p=232 Read More »URI|1036 Bhaskara’s Formula solution]]>

Well, the problem says it all. You need to calculate the root of Bhaskara,s Formula.

Don’t know Bhaskara’s Formula? It is actually Algebra’s Quadratic Formula.

The Quadratic/ Bhaskaras formula is : ax2 + bx + c .

And Quadratic Formula’s root is :

So, That’s it. We have to just print the positive and negative value of the quadratic formula.

#include<sdtio.h>
#include<math.h>
int main(){
 double a,b,c;
 scanf("%lf %lf %lf",&a,&b,&c);
 if(a> 0 && (pow(b,2)-(4*a*c))>0){
   double r1 = (-b + sqrt(pow(b,2) - (4*a*c)) ) / (2*a);
   double r2 =  (-b - sqrt(pow(b,2) - (4*a*c)) ) / (2*a);
   printf("R1 = %.5lf\nR2 = %.5lf\n",r1,r2);
 }else{
   printf("Impossivel calcular\n");
 } 
 return 0;
 } 

]]>
232