水仙花数
python版
k = int(input("请输入一个数"))
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}是水仙花数")
else:
print(f"{k}不是水仙花数")
C
#include <stdio.h>
int main()
{
int bw, sw, gw, k;
printf("请输入一个正整数:");
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是水仙花数", k);
}else{
printf("%d不是是水仙花数", 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
素数
Python版本
k = int(input("请输入一个大于2的数"))
for i in range(2, k):
if k % i == 0:
print(f"{k}不是素数")
break
else:
print(f"{k}是素数")
#include <stdio.h>
int main(){
int a=0; // 是否素数
int num=0; // 输入的整数
printf("输入一个整数:");
scanf("%d",&num);
for(int i=2;i<num;i++){
if(num%i==0){
a = 1
break
}
}
if(a==0){
printf("%d是素数。\n", num);
}else{
printf("%d不是素数。\n", num);
}
return 0;
}
VB
Private Sub Command1_Click()
Dim yn As Boolean '为真表示是素数
Dim i As Integer, j As Integer, x As Integer
For i = 1 To 100
yn = True '为真表示是素数,否则就不是,先默认为是素数
For j = 2 To i - 1 '素数要大小1,所以循环从2开始,素数不包括本身,所以循环终值减小1
If i Mod j = 0 Then yn = False '如果能整除就不是素数
Next j
If yn = True Then Print i; '如是素数,就在窗体上输出素数
Next i
End Sub
输入十个数,从小到大
k = []
for _ in range(10):
n = eval(input("请输入一个数"))
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("输入数");
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("请输入斐波拉契数列的项数"))
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项值的函数
If n = 1 Then
f = 0
ElseIf n = 2 Then
f = 1
Else
f = f(n - 1) + f(n - 2)
End If
End Function
最大公约数
k = input("请输入两个数,用空格隔开")
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"最大公约数是{d}, 最小公倍数是{int(a) * int(b) // d}")
C
int main(void)
{
printf("这是两个整数求最大公约数的算式,请输入两个不相等的正整数,并按回车键确认:\n"); //操作提示
intm,n,r,i; //定义四个整形变量
scanf("%d %d",&m,&n); //输入m和n的值
if(m<n) {i=m;m=n;n=i;} //如果m<n,借用变量i进行m和n的数值互换
while(m%n!=0) //m取模n 赋值给r
{r=a%b;m=n;n=r;} //余数不等于0,则n的值给m,r的值给n,再次进入循环
printf("它们的最大公约数是%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 "最大公约数为1", vbOKOnly, "提示"
If r = 0 Then MsgBox "最大公约数为" & b, vbOKOnly, "提示"
End Sub
网友评论