Sunday, 26 January 2014

UVA sloved: problem number: 11479 - Is this the easiest problem?

11479 - Is this the easiest problem?

A triangle is a geometric shape with three positive sides. However, any given three sides won’t necessarily form a triangle. The three sides must form a closed region. Triangles are categorized depending on the values of the sides of a valid triangle. In this problem you are required to determine the type of a triangle.

 
  Input    
  The first line of input will contain a positive integer T<20, where T denotes the number of test cases. Each of the next T lines will contain three 32 bit signed integer.  
     
  Output  
  For each case of  input there will be one line of output. It will be formatted as:
Case {x}: {triangle type}. Where x denotes the case number being processed and {triangle type} is the type of the triangle..{triangle type} will be one of the following, depending on the values of the three sides:
    • Invalid - The three sides can not form a triangle
    • Equilateral  - All three sides of valid triangle are equal
    • Isosceles  - Exactly two of the sides of a valid triangle are equal.
    • Scalene - No pair of sides are equal in a valid triangle.
 
     
  Sample Input Sample Output    
  4
1 2 5
1 1 1
4 4 2
3 4 5
Case 1: Invalid
Case 2: Equilateral
Case 3: Isosceles
Case 4: Scalene
 
 


 

__________________________________________________________

#include<stdio.h>
int main()
{
    long int t,a,b,c,i;
    while(scanf("%ld",&t)==1)
    {
        i=1;
        while(i<=t)
        {
            scanf("%ld%ld%ld",&a,&b,&c);
           
            if((a+b)<=c || (b+c)<=a || (c+a)<=b)
                printf("Case %ld: Invalid\n",i);
           
            else if(a<=0 || b<=0 || c<=0)
                printf("Case %ld: Invalid\n",i);
           
            else if(a==b && b==c)
                printf("Case %ld: Equilateral\n",i);
           
            else if(a==b || b==c || c==a)
                printf("Case %ld: Isosceles\n",i);
           
            else
                printf("Case %ld: Scalene\n",i);
            i++;
        }
    }
    return 0;
}



Thursday, 9 January 2014

Determine whether the given number is a multiple of 3 or not.

Input

First line contains the number of test cases T (T <= 10), then T lines follow, each contains an integer N (0 <= N <= 1000).

Output

For each test case, if N is a multiple of 3, first print N, then print the string “ is a multiple of 3”, otherwise, first print N, then print “ is not a multiple of 3” without the quotes. See sample input output for more details.

Example

Input:
3
1
6
10

Output:

1 is not a multiple of 3
6 is a multiple of 3
10 is not a multiple of 3

______________________________

#include <cstdio>

int main()
{
    int n, t;
    scanf("%d",&t);
    while(t>0){
        scanf("%d",&n);
       
        if ((n % 3) == 0)
            printf("%d is a multiple of 3\n",n);
        else
            printf("%d is not a multiple of 3\n",n);

        t--;
    }
}

Saturday, 4 January 2014

Difference between “while” loop and “do while” loop?

do while: starts executing contents of the loop before looking at the condition. therefore, execution shall run at least for once even if condition doesn't math.

e.g:

int  x=11;
        do{
            System.out.println("human being VS being human");
            x++;
        }while(x<10)
this one will execute 1 time before it goes down to look for the condition, after that it will stop.

and 

while: looks for the condition at the very first before executing the content.

e.g:
int  x=11;
        while(x<10){
            System.out.println("human being VS being human");
            x++;
        };

this one won't go inside the loop since the condition doesn't match.


skeleton code: real number comperation using Conditional Operator in Java ( ? : ) [ simple, user friendly ]


import java.util.Scanner;

class grps{
    public static void main(String[] args){
        Scanner input= new Scanner(System.in);
       
        System.out.print("enter the first input: ");
        double a=input.nextDouble();
       
        System.out.print("enter the nextinput: ");
        double b=input.nextDouble();
       
        System.out.println(a>b ? "First one is the bigger.":"Second one is the bigger."); //( ? : )
    }
}



raw code: Compound Interest calculation in Java [ simple, user friendly]

Q: lets say, you to find out what will be the number of viewers in my blog in next 30 days. given that, for instance i have 10,000 views now, increasing rate is 1% each day. you have to calculate it for next 30 days and show the result in every other 10th day.
formula is: amount= pri(1+rate)^time

=>
import java.util.Scanner;

class grps{
    public static void main(String[] agrs){
        Scanner input= new Scanner(System.in);
        int y=0;
       
        System.out.print("enter the sum you have already: ");
        double principal=input.nextDouble(); //=10000
       
        System.out.print("how much is the growth rate: ");
        double rate=input.nextDouble();    //=.01
       
        System.out.print("for how many days you want to calculate: ");
        double day=input.nextDouble();    //=30
       
        System.out.print("how often you want to see the result [e.g: 1 being every day, 2 being every other day]: ");
        double show=input.nextDouble();    //=10
       
        for(int x=1; x<=day; x+=show){
            y+=(int)(principal*Math.pow(1+rate, day));
            System.out.println("In "+x+" days there will be "+y+" followers!");
        }
    }
}