水仙花數(shù)
python版
k = int(input("請輸入一個數(shù)"))
bw = k // 100
sw = k // 10 % 10
gw = k % 10
print(bw, sw, gw)
if k == bw ** 3 + sw ** 3 + gw ** 3:
print(f"{k}是水仙花數(shù)")
else:
print(f"{k}不是水仙花數(shù)")
C
#include <stdio.h>
int main()
{
int bw, sw, gw, k;
printf("請輸入一個正整數(shù):");
scanf("%d",&k);
bw = int(k / 100);
sw = int(k / 10) % 10;
gw = k % 10;
if(k == bw*bw*bw + sw*sw*sw + gw*gw*gw)
printf("%d是水仙花數(shù)", k);
}else{
printf("%d不是是水仙花數(shù)", k);
}
return 0;
}
VB
Private Sub Form_Click()
Dim q As Integer
Dim b As Integer
Dim s As Integer
Dim n As Integer
For n = 100 To 999
q = n \ 100
b = (n - q * 100) \ 10
s = n Mod 10
If ((q * q * q + b * b * b + s * s * s) = n) Then
Print n
End If
Next
素?cái)?shù)
Python版本
k = int(input("請輸入一個大于2的數(shù)"))
for i in range(2, k):
if k % i == 0:
print(f"{k}不是素?cái)?shù)")
break
else:
print(f"{k}是素?cái)?shù)")
#include <stdio.h>
int main(){
int a=0; // 是否素?cái)?shù)
int num=0; // 輸入的整數(shù)
printf("輸入一個整數(shù):");
scanf("%d",&num);
for(int i=2;i<num;i++){
if(num%i==0){
a = 1
break
}
}
if(a==0){
printf("%d是素?cái)?shù)优烧。\n", num);
}else{
printf("%d不是素?cái)?shù)媳瞪。\n", num);
}
return 0;
}
VB
Private Sub Command1_Click()
Dim yn As Boolean '為真表示是素?cái)?shù)
Dim i As Integer, j As Integer, x As Integer
For i = 1 To 100
yn = True '為真表示是素?cái)?shù)婚夫,否則就不是,先默認(rèn)為是素?cái)?shù)
For j = 2 To i - 1 '素?cái)?shù)要大小1,所以循環(huán)從2開始赘娄,素?cái)?shù)不包括本身故响,所以循環(huán)終值減小1
If i Mod j = 0 Then yn = False '如果能整除就不是素?cái)?shù)
Next j
If yn = True Then Print i; '如是素?cái)?shù),就在窗體上輸出素?cái)?shù)
Next i
End Sub
輸入十個數(shù)肯污,從小到大
k = []
for _ in range(10):
n = eval(input("請輸入一個數(shù)"))
k.append(n)
for i in range(10):
for j in range(0, n-i-1):
if k[j] > k[j+1]:
k[j], k[j+1] = k[j+1], k[j]
print(k)
C
#include <stdio.h>
int main(){
int i,j,a[10],t;
printf("輸入數(shù)");
for (i = 0; i < 10; i++)
scanf("%d",&a[i]);
for (i = 0; i < 10; i++){
for (j = i + 1; j < 10;j++)
if (a[i] < a[j]){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
printf("從大到小");
for (i = 0; i < 10; i++)
printf("%2d", a[i]);
return 0;
}
VB
Dim a As String
Dim arr(1 To 10) As Integer
Dim i, j, t As Integer
Private Sub Command1_Click()
For i = 1 To 10
a = InputBox("請輸入第" & i & "個值")
arr(i) = Val(a)
Next i
For i = 1 To 9
For j = 1 To 9
If arr(j) > arr(j + 1) Then
t = arr(j + 1): arr(j + 1) = arr(j): arr(j) = t
End If
Next j
Next i
For i = 1 To 10
Print arr(i)
Next i
End Sub
菱形
for i in range(5):
print(" "*(5-i-1), end="")
print("* "*(i+1))
for i in range(4):
print(" " * (i + 1), end="")
print("* " * (5 - i - 1))
C
#include <stdio.h>
int main(){
int i = 0
int k = 0
for (i = 0; i < 5;i++){
for (k=0; k<5-i-1;k++){
printf(" ")
}
for (k=0; k<i+1;k++){
printf("* ")
}
printf("\n")
}
for (i = 0; i < 4;i++){
for (k=0; k<i+1;k++){
printf(" ")
}
for (k=0; k<5-i-1;k++){
printf("* ")
}
printf("\n")
}
return 0;
}
VB
Private Sub Form_Click()
Dim i As Integer
Dim q As Integer
For i = 1 To 5
For k = 1 To 5-i
print " ";
Next k
For k = 1 To i
print "* ";
Next k
print ""
Next i
For i = 1 To 4
For k = 1 To i
print " ";
Next k
For k = 1 To 5-i
print "* ";
Next k
print ""
Next i
斐波拉契
python
k = int(input("請輸入斐波拉契數(shù)列的項(xiàng)數(shù)"))
n = []
for i in range(k):
if i < 2:
n.append(1)
else:
n.append(n[-1]+n[-2])
print(n)
C
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 0;
int a = 1;
int b = 1;
int c = 0;
int i = 0;
scanf("%d", &n);
printf("%5d %5d ", a, b);
for (i = 0; i < n - 2;i++)
{
c = a + b;
a = b;
b = c;
printf("%5d ", c);
}
printf("\n");
system("pause");
return 0;
}
VB
Function f(ByVal n As Double) As Double '定義求第n項(xiàng)值的函數(shù)
If n = 1 Then
f = 0
ElseIf n = 2 Then
f = 1
Else
f = f(n - 1) + f(n - 2)
End If
End Function
最大公約數(shù)
k = input("請輸入兩個數(shù)翘单,用空格隔開")
a, b = k.split(" ")
c, d = max(int(a),int(b)), min(int(a), int(b))
while c % d != 0:
c, d = d, c % d
print(f"最大公約數(shù)是6sgkou8, 最小公倍數(shù)是{int(a) * int(b) // d}")
C
int main(void)
{
printf("這是兩個整數(shù)求最大公約數(shù)的算式,請輸入兩個不相等的正整數(shù)蹦渣,并按回車鍵確認(rèn):\n"); //操作提示
intm,n,r,i; //定義四個整形變量
scanf("%d %d",&m,&n); //輸入m和n的值
if(m<n) {i=m;m=n;n=i;} //如果m<n哄芜,借用變量i進(jìn)行m和n的數(shù)值互換
while(m%n!=0) //m取模n 賦值給r
{r=a%b;m=n;n=r;} //余數(shù)不等于0,則n的值給m柬唯,r的值給n认臊,再次進(jìn)入循環(huán)
printf("它們的最大公約數(shù)是%d\n",n);
system("PAUSE");
return 0;
}
VB
Private Sub Command1_Click()
Dim a As Long, b As Long, t As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
If a < b Then
t = a
a = b
b = t
End If
r = a Mod b
Do Until r = 0 Or r = 1
a = b
b = r
r = a Mod b
Loop
If r = 1 Then MsgBox "最大公約數(shù)為1", vbOKOnly, "提示"
If r = 0 Then MsgBox "最大公約數(shù)為" & b, vbOKOnly, "提示"
End Sub