2009/10/29

[ C/C++ ] Powers Of Two by C++

使用 C++ 輸出 2 的次方值 Power Of Two

// powersOfTwo.cpp
//
// Powers of Two, 2^i 
//
// Console-based
// Author Jim.lin 2009

#include <iostream>

using namespace std;

int main()
{
    int N;
    int v = 1;
    int i = 0;
    cin >> N;
    while(i <= N)
    {
        cout << i << " " << v << endl;
        v = 2 * v;
        i = i + 1;
    }
}

2009/10/25

[ C/C++ ] Leap Year Check

閏年定義(leap year or intercalary year) : 四年一閏,百年不閏,四百年閏,四千年閏

英文字典 leap year ?
A leap year is a year which has 366 days. The extra day is the 29th February. There is a leap year every four years.

// leapyear.cpp
//
// This program to determine leap year.
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>

using namespace std;

int main()
{
    int year;
        cin >> year;
    if(( year%4==0 && year%100!=0 ) || year%400==0 )
        cout << "Leap Year ";
    else
        cout << "Not Leap Year ";
    return 0;
}
Ref. By Google.

2009/10/15

[ C/C++ ] Quadratic by C++

使用 C++ 解一元二次方程式

// quadratic.cpp
//
// This program solves a quadratic equation in standard form.
// ax^2 + bx + c = 0
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

double a,b,c;
double x1,x2;
double i;

// main function
int main()
{
    cout<<"Please Input Number a,b,c:\n";
    cin>>a>>b>>c;
    i=sqrt(b*b-4*a*c);
    if(i<0)
        cout<<"This is imaginary roots";
    else if(i==0)
    {
        x1=-b/(2*a);
        cout<<"The quadratic equation has one root" << x1 << endl;
    }
    else
    {
        x1=(-b+i)/(2*a);
        x2=(-b-i)/(2*a);
        cout<<"The quadratic equation has two roots" << endl;
        cout<<"x1=" << x1 << "  x2=" << x2 << endl;
    }
}
Best Code : http://jblanco_60.tripod.com/c_pp_quadratic.html