電腦軟體設計練習卷(共10回)答案
2020年2月19日 星期三
精進練習題(答案)
1.輸入n,求1到n之間質數的個數,最大的質數為何?
Dim n, s, cnt As Long
Console.Write("請輸入一數n,求1到n之間最大質數=")
n = Console.ReadLine
For i = 1 To n
For j = 1 To i
If i Mod j = 0 Then cnt =
cnt + 1
Next
If cnt = 2 Then s = i
cnt = 0
Next
Console.WriteLine("最大質數為" & s)
2.魔術矩陣:設計一個奇數行列的矩陣,每一行、每一列與斜線的和均為相同。
例如:8 1 6
3
5 7
4
9 2
Dim intSI, intSJ, intI, intJ, intK,
intT, iX, iY, Num(,), N As Integer
Do
Console.Write("輸入一個奇數:")
N = Val(Console.ReadLine())
Loop While (N And 1) = 0
ReDim Num(N - 1, N - 1)
intSI = (N - 1) / 2
intSJ = -intSI
For intT = 0 To N - 1
intI = intSI
intJ = intSJ
For intK = 1 To N
iX = IIf(intI < 0, N + intI,
IIf(intI >= N, intI - N, intI))
iY = IIf(intJ < 0, N + intJ,
IIf(intJ >= N, intJ - N, intJ))
Num(iX, iY) = intT * N + intK
intI = intI + 1
intJ = intJ + 1
Next
intSI = intSI - 1
intSJ = intSJ + 1
Next
For iY = N - 1 To 0 Step -1
For iX = 0 To N - 1
Console.Write(Num(iX, iY) &
vbTab)
Next
Console.WriteLine()
Next
Console.ReadKey()
3.將10位數字轉換為N進位數。
Dim n, a, d As Integer, q As
String
Console.Write("請輸入一數=")
a = Console.ReadLine
Console.Write("將數字轉換成N進位數(N<10),N=")
n = Console.ReadLine
d = a
Do While d > 0
q = (d Mod n) & q
d = d \ n
Loop
Console.WriteLine("結果=" & q)
4.利用輾轉相除法,求二數的最大公因數。
(輸入二數a,b,假如b>a,則二者交換,a除以b之後得餘數r,若餘數不為0,則以除 數設定為被除數,餘數設定為除數,之後重複執行)
If b > a then temp=a :
a=b : b=temp
Do
r = a Mod b : a=b : b=r
Loop While r>0
Dim a, b, temp, r As Integer
Console.WriteLine("請輸入2數利用輾轉相除法求最大公因數")
Console.Write("請輸入A=")
a = Console.ReadLine
Console.Write("請輸入B=")
b = Console.ReadLine
If b > a Then temp = a :
a = b : b = temp
Do
r = a Mod b : a = b : b = r
Loop While r > 0
Console.WriteLine("最大公因數=" & a)
5.輸入三角形三邊長,判斷三角形為正三角形、鈍角三角形、銳角三角形、直角三角形。
Dim a, b, c As Single, s As
String
Console.WriteLine("請輸入三角形三邊長ABC")
Console.Write("請輸入A=")
a = Console.ReadLine
Console.Write("請輸入B=")
b = Console.ReadLine
Console.Write("請輸入C=")
c = Console.ReadLine
If a + b > c And b + c
> a And a + c > b Then
If a = b And b = c Then
s = s & "正三角形"
ElseIf (a ^ 2 + b ^ 2 = c ^ 2) Or (a ^ 2 +
c ^ 2 = b ^ 2) Or (c ^ 2 + b ^ 2 = a ^ 2) Then
s = s & "直角三角形"
ElseIf (a ^ 2 + b ^ 2 > c ^ 2) Or (a ^ 2
+ c ^ 2 > b ^ 2) Or (c ^ 2 + b ^ 2 > a ^ 2) Then
s = s & "鈍角三角形"
ElseIf (a ^ 2 + b ^ 2 < c ^ 2) Or (a ^ 2
+ c ^ 2 < b ^ 2) Or (c ^ 2 + b ^ 2 < a ^ 2) Then
s = s & "銳角三角形"
End If
Else
s = "不是三角形"
End If
Console.WriteLine("三角形為" & s)
6.迴文判斷:輸入一字串,檢查是否為迴文(例如:ABCDCBA是迴文,123456不是)
Dim a As String
Console.WriteLine("請輸入一字串判斷是否為迴文")
Console.Write("請輸入字串:")
a = Console.ReadLine()
If a = StrReverse(a) Then
Console.WriteLine("輸入字串為迴文")
Else
Console.WriteLine("輸入字串不是迴文")
End If
7.排序:請輸入3個數,請由小到大排序這三個數字。
Dim
a(3), temp As Single
Console.WriteLine("請輸入3個數字由小到大排列")
Console.Write("請輸入數字一:")
a(1) =
Val(Console.ReadLine)
Console.Write("請輸入數字二:")
a(2) =
Val(Console.ReadLine)
Console.Write("請輸入數字三:")
a(3) =
Val(Console.ReadLine)
For i =
1 To 2
For j = i To 3
If a(j)
< a(i) Then temp = a(j) : a(j) = a(i) : a(i) = temp
Next
Next
Console.WriteLine("結果為=" & a(1) & " " & a(2) & "
" & a(3))
8.輸入正整數 N,請將 N 的所有正因數由小到大印出來。
Dim n
As Long, s As String
Console.WriteLine("請輸入一數N,將N的所有正因數由小到大印出來")
Console.Write("N=")
n =
Console.ReadLine
For i =
1 To n
If n Mod i = 0 Then s = s & "
" & i
Next
Console.WriteLine("結果=" & s)
9.輸入正整數 N,請將N轉成二進位輸出。
Dim a, d As Integer, q As String
Console.Write("將數字轉換成2進位數")
Console.Write("請輸入一數=")
a =
Console.ReadLine
d = a
Do
While d > 0
q = (d Mod 2) & q
d = d \ 2
Loop
Console.WriteLine("結果=" & q)
10.寫出ABCDE-WXYZ=66666的程式碼,其中ABCDEWXYZ為1-9不同的數字,找出所有ABCDE與WXYZ的組合
Dim a(9), t As String, b, c As Integer
c = 1000
For b = 66666 To 99999
Do While c < 9999
If b - c = 66666 Then
t = Trim(Str(b)) &
Trim(Str(c))
For i = 1 To 9
a(i) = Mid(t, i, 1)
Next
For i = 1 To 8
For j = 2 To 9
If a(i) = a(j) Then
Exit Do
Next
Next
Console.WriteLine(b &
"-" & c & "=66666")
End If
c = c + 1
Loop
Next
11.請印出九九乘法表
For i = 1 To 9
For j = 1 To 9
Console.Write(i & "X"
& j & "=" & i * j & " ")
Next
Console.WriteLine()
Next
12.計算輸入的年份是否為閏年,閏年的條件為:
可被 1000 整除。
可被 400 整除。
可被 4 整除。
不可被 100 整除。
口訣:逢四百年或千年閏,逢百年不閏,逢四年又閏。
Dim a As Integer
Console.Write("閏年判斷,請輸入年份=")
a = Val(Console.ReadLine())
If a Mod 400 = 0 Then
Console.WriteLine(a & "年是閏年")
ElseIf a Mod 4 = 0 And a Mod 100 <> 0 Then
Console.WriteLine(a & "年是閏年")
Else
Console.WriteLine(a & "年不是閏年")
End If
訂閱:
文章 (Atom)