2010年10月28日 星期四

科學記號

內容 : 正體->简体
科學上採用的數字常寫成 a × 10n 的形式﹐其中 1 ≤ a < 10﹐n 是整數﹐
例如:(一) 光速是每秒 300,000,000 公尺﹐若取一位有效數字,
記為 3 × 108 公尺/秒。
(二) 電子的質量是0.000,000,000,000,000,000,000,000,000,909 公克,
若取二位有效數字,記為 9.1 × 10-28 公克。
輸入說明 :
輸入檔中有多筆測試資料。每筆測試資料第一行有一個正整數 N, (1 ≦ N ≦ 100),代表有N筆測試資料。
接下來,有N行,每行有二個數字,第二數字表示取幾位有效數字。
須四捨五入!
輸出說明 :
對於每筆測資,輸出一行此筆資料的科學記號。
範例輸入 :

2
300,000,000 4
0.000,000,000,000,000,000,000,000,000,909 2
範例輸出 :

3.000x10(8)
9.1x10(-28)

2 則留言:

  1. Public Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Me.Hide
    For i = 1 To x
    Line Input #1, step
    step = Split(step)
    step(0) = Replace(step(0), ",", "")
    p = Val(InStr(step(0), "."))
    If p = 0 Then p = Len(step(0)) Else p = p - 1
    step(0) = Replace(step(0), ".", "")
    Call check(step(0), p, step(1), np)
    Next i
    End
    Close #2
    Close #1
    End Sub
    Public Sub check(st, p, k, np)
    forma = "#."
    For i = 1 To Len(st)
    wor = Mid(st, i, 1)
    If wor <> 0 Then
    st = Mid(st, i, k + (Len(st) - i - k) + 1)
    st = st / 10 ^ (Len(st) - 1)
    np = i
    For j = 2 To k
    forma = forma & "0"
    Next j
    Print #2, Format(st, forma) & "x10(";
    Print #2, p - np & ")"
    Exit For
    End If
    Next i
    End Sub

    BY Yung

    回覆刪除
  2. Dim StQ As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, XXX
    For KK = 1 To XXX
    K = 0
    Line Input #1, strA
    AA = Split(strA)
    StQ = AA(0): Step = AA(1)
    StQ = Replace(StQ, ",", "")
    For i = 1 To Len(StQ)
    If Mid(StQ, i, 1) = 0 Then
    K = K + 1
    ElseIf Mid(StQ, i, 1) <> "." And (Val(Mid(StQ, i, 1)) > 0 Or Val(Mid(StQ, i, 1)) < 9) Then
    Call RUD(StQ, Step, K)
    Exit For
    End If
    Next i
    Next KK
    Close #2
    Close #1
    End
    End Sub
    Public Sub RUD(ByVal Num As String, ByVal Step As Integer, ByVal K As Integer)
    For i = 2 To Step - 1
    Fmat = Fmat & "#"
    Next i
    Fmat = "0." & Fmat & "0"
    If K = 0 Then
    For i = 1 To Len(Num)
    If Mid(Num, i, 1) = "." Then X = i
    Next i
    If X = 0 Then X = Len(Num)
    Num = Replace(Num, ".", "")
    St = Val(Mid(Num, 1, Step + 1)) / (10 ^ Step)
    Print #2, Format(St, Fmat) & "x" & "10(" & X - 1 & ")"
    Else
    Num = Replace(Num, ".", "")
    St = Val(Mid(Num, K + 1, Step + 1)) / (10 ^ Step)
    Print #2, Format(St, Fmat) & "x" & "10(-" & K & ")"
    End If
    End Sub


    BY小白

    回覆刪除