- 計算工資問題:每小時工資為 RATE,如果工作超過 40 小時,加班部分工資是正常時間工資的 1.5 倍。通過此案例可以練習基本的輸入輸出、條件判斷和數(shù)學運算。
- program payroll real :: rate, hours, pay read (*,*) rate, hours if (hours > 40) then pay = 40 * rate + (hours - 40) * 1.5 * rate else pay = hours * rate end if print *, "rate = ", rate print *, "hours = ", hours print *, "pay = ", pay end program payroll
- 學生成績分級:根據(jù)輸入的成績,將其分為 A(大于等于 80 分)、B(大于等于 60 分小于 80 分)、C(小于 60 分)等級。能鍛煉條件判斷和嵌套使用。
- program grades integer :: grade read *, grade if (grade >= 80) then print *, grade, " = A" else if (grade < 60) then print *, grade, " = C" else print *, grade, " = B" end if end program grades
- 三個數(shù)排序:輸入三個數(shù),將它們按從小到大的順序輸出。有助于理解數(shù)據(jù)的比較和交換操作
- program maxmin real :: a, b, c, t read *, a, b, c if (a > b) then t = a a = b b = t end if if (b > c) then t = b b = c c = t end if if (a > b) then t = a a = b b = t end if print *, a, b, c end program maxmin
- 計算階乘:計算一個數(shù)的階乘,例如求 5! ??梢允煜ぱh(huán)結(jié)構(gòu)的使用。
- program factorial integer :: n, fact = 1 read *, n do i = 1, n fact = fact * i end do print *, n, "! = ", fact end program factorial
- 打印九九乘法表:使用嵌套循環(huán)輸出九九乘法表,鞏固循環(huán)和輸出格式的掌握。
- program multiplication_table
do i = 1, 9
do j = 1, i
print *, i, " * ", j, " = ", i * j
end do
print *
end do
end program multiplication_table
- 判斷閏年:根據(jù)輸入的年份,判斷是否為閏年。涉及到條件判斷和邏輯運算。
- program leap_year integer :: year read *, year if (mod(year, 4) == 0.and. (mod(year, 100) /= 0.or. mod(year, 400) == 0)) then print *, year, " is a leap year." else print *, year, " is not a leap year." end if end program leap_year
- 數(shù)組求和與平均值:定義一個數(shù)組,計算數(shù)組元素的總和以及平均值,加強對數(shù)組操作和基本運算的運用。
- program array_stats
integer, dimension(10) :: arr
integer :: i, sum = 0
real :: average
do i = 1, 10
read *, arr(i)
sum = sum + arr(i)
end do
average = real(sum) / 10.0
print *, "Sum of array elements: ", sum
print *, "Average of array elements: ", average
end program array_stats
- 簡單的數(shù)據(jù)統(tǒng)計:輸入一組數(shù)據(jù),計算其中的*值、最小值和數(shù)據(jù)個數(shù)。鍛煉數(shù)據(jù)處理和條件判斷能力。
- program data_stats
integer :: num, max_num, min_num, count = 0
read *, num
max_num = num
min_num = num
do while (num /= -1)
count = count + 1
if (num > max_num) then
max_num = num
end if
if (num < min_num) then
min_num = num
end if
read *, num
end do
print *, "Maximum number: ", max_num
print *, "Minimum number: ", min_num
print *, "Number of data: ", count
end program data_stats