2014年6月4日 星期三

  1. //  小數點處理例
  2. #include <iostream>  
  3. #include <cmath>  
  4. #include <iomanip>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     int x1,y1,x2, y2;  
  9.     while (cin >> x1 >> y1 >> x2 >> y2)  
  10.     {  
  11.         double ans = sqrt((double)(x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));  
  12.         cout.setf(ios::fixed, ios::floatfield);  
  13.         cout.precision(3);  
  14.         cout << ans << endl;  
  15.     }  
  16.     return 0;  
  17. }  

2014年5月30日 星期五

//故障計算機
#include <iostream>
using namespace std;
int main()
{
     int n;  
     while (cin >> n)
     {
         if (n<=9)
            cout <<'|' <<"_______" << n <<'|' << endl;
         else if (n<=99)
            cout <<'|' << "______" << n <<'|' << endl;
         else if (n<=999)
             cout <<'|' << "_____" << n<<'|' << endl;
         else if (n<=9999)
             cout <<'|' << "____" << n<<'|' << endl;
         else
         {
             n =  n%10000;
             if (n<=9)
                cout <<'|' <<"____000" << n <<'|' << endl;
             else if (n<=99)
                cout <<'|' << "____00" << n <<'|' << endl;
             else if (n<=999)
                 cout <<'|' << "____0" << n<<'|' << endl;
             else
                 cout <<"|____" <<  n<<'|' << endl;
          }
       }  
     system("PAUSE");
     return 0;
}

// 大數相加、比大小
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int ar[20], br[20], cr[20],dr[20];
memset(ar, 0, 20 * sizeof(ar[0]));
memset(br, 0, 20 * sizeof(br[0]));
memset(cr, 0, 20 * sizeof(cr[0]));
memset(dr, 0, 20 * sizeof(dr[0]));
int j = 0;
for (int i = a.length() - 1; i >= 0; i--)
{
ar[j] = a[i] - 48;
j++;
}
j = 0;
for (int i = b.length() - 1; i >= 0; i--)
{
br[j] = b[i] - 48;
j++;
}

//subtract
for (int i = 19; i >= 0; i--)
dr[i] = ar[i] - br[i];
for (int i = 0; i <= 18; i++)
if (dr[i] < 0)
{
dr[i + 1] = dr[i + 1] -1;
dr[i] = dr[i] + 10;
}
int i = 19;
while (dr[i] == 0) i--;
if (dr[i] > 0)
cout << "A ";
else if (dr[i] < 0)
cout << "B ";
else
cout << "  ";

//plus
for (int i = 19; i >= 0; i--)
cr[i] = br[i] + ar[i];
for (int i = 0; i <= 18; i++)
if (cr[i] >= 10)
{
cr[i + 1] = cr[i + 1] + cr[i] / 10;
cr[i] = cr[i] % 10;
}

i = 19;
while (cr[i] == 0) i--;
while (i >= 0)
{
cout << cr[i];
i--;
}
cout << endl;
}
return 0;
}

2014年4月25日 星期五

Maze Sample




Public Class Form1
    Dim a(16, 16) As Integer
    Dim str1 = ""

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\MData.txt")
        Dim lines() = Split(fileContents, vbNewLine)

        For i = 0 To 14
            Dim ba() = Split(lines(i), ",")
            For j = 0 To 14
                a(i + 1, j + 1) = ba(j)
            Next
        Next

        For i = 0 To 16
            a(0, i) = 2
            a(i, 0) = 2
            a(16, i) = 2
            a(i, 16) = 2
        Next

        showArray0()

        If visit(1, 1) Then
            showArray()
        Else
            MsgBox("NoPath")
        End If
        End
    End Sub

    Sub showArray0()
        Dim str3 = ""
        For i = 1 To 15
            For j = 1 To 15
                If Not a(i, j) = 2 Then
                    str3 = str3 & "○"
                Else
                    str3 = str3 & "●"
                End If
            Next
            str3 = str3 & vbNewLine
        Next
        MsgBox(str3)
    End Sub

    Sub showArray()
        Dim str3 = ""
        For i = 1 To 15
            For j = 1 To 15
                If a(i, j) = 1 Then
                    str3 = str3 & "○"
                Else
                    str3 = str3 & "●"
                End If
            Next
            str3 = str3 & vbNewLine
        Next
        MsgBox(str3)
    End Sub

    Function visit(ByVal i As Integer, ByVal j As Integer) As Boolean
        If a(15, 15) = 1 Then Return True
        If a(i, j) = 0 Then
            a(i, j) = 1
            ' Call showArray()
            If Not a(15, 15) = 1 And Not (visit(i + 1, j) Or visit(i, j + 1) Or visit(i - 1, j) Or visit(i, j - 1)) Then
                a(i, j) = 0
            End If
        End If
        Return a(15, 15) = 1

    End Function
End Class




2014年4月22日 星期二

Resurive Sample

'' 求1+3+5+ + 99 = ?
'Public Class Form1
'    Dim sumN = 0
'    Sub sum(sN, eN, stepN)
'        '判斷結束否
'        If sN > eN Then Return

'        '每次過程要做的事
'        sumN = sumN + sN

'        '往完成工作方向前進
'        sum(sN + stepN, eN, stepN)
'    End Sub

'    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'        Call sum(1, 100, 2)
'        MsgBox(sumN)
'    End Sub
'End Class

' 求1!+2!+3!+ + 10! = ?
'Public Class Form1
'    Dim sumN = 0
'    Sub sum(sN, eN, stepN)
'        '判斷結束否
'        If sN > eN Then Return

'        '每次過程要做的事
'        Dim tsum = 1
'        For i = 1 To sN
'            tsum = tsum * i
'        Next
'        sumN = sumN + tsum

'        '往完成工作方向前進
'        sum(sN + stepN, eN, stepN)
'    End Sub

'    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'        Call sum(1, 5, 1)
'        MsgBox(sumN)
'    End Sub
'End Class


'尋找最大值
'Public Class Form1
'    Dim maxValue = 0
'    Dim a() = {10, 200, 100, 50, 30}

'    Sub fMax(sN, eN)
'        '判斷結束否
'        If sN > eN Then Return

'        '每次過程要做的事
'        If a(sN) > maxValue Then maxValue = a(sN)

'        '往完成工作方向前進
'        fMax(sN + 1, eN)
'    End Sub

'    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'        Call fMax(0, a.Length - 1)
'        MsgBox(maxValue)
'    End Sub
'End Class