中国大学MOOC: 下面代码执行后,变量s的值为______。 Dim i, n As Integer, s As String i = 2 : n = 12 : s = n & = Do Until n = 1 Do While n Mod i = 0 s &= i & * n = n i Loop i += 1 Loop s = Strings.Left(s, Len(s) - 1)
举一反三
- 假设整型n已输入值。以下不能正确计算1*2*3*...*n的程序段是_______。 (1) for(i=1,s=1; i<=n; i++) s*=i; (2) i=1;s=1; while(i<=n) s*=i++; (3) i=0,s=1; do{ i++,s*=i; }while(i<=n); (4) for(i=1,s=1; ; i++) { s = s*i, if(i==n) break; }
- 下列函数能够求n的阶乘n!的是 A: f = lambda n: n! B: def f(n): s=1 for i in range(n): s = s * i return s C: def f(n): s=1 for i in range(n,0,-1): s = s * i return s D: def f(n): if n == 1: return 1 else: return f(n-1)*n
- 有如下程序段:s = 0For i = 1 To 10 s = s + iNext iPrint s与上述程序段输出结果相同的程序段为( )。 A: s = 0: i = 0 While i <= 10 i = i + 1 s = s + i Wend Print s B: s = 0: i = 1 While i < 10 i = i + 1 s = s + i Wend Print s C: s = 0: i = 1 Do s = s + i i = i + 1 Loop While i < 10 Print s D: s = 0: i = 1 Do s = s + i i = i + 1 Loop Until i > 10 Print s
- 以下程序的功能是计算1到n的阶乘和,请填空。 long f(int n) { int i; long s; s=1; for(i=1; i <=n; i++) s=s*i; return s; } main() { long s; int k, n; scanf("%d", &n); s=0; for(k=0; k <=n; k++) s=s +_____ ;printf("%d\n",s); }
- 下列程序执行后s的值是____1____,i的值是____2____ s = 1: i = 1 Do While True s = s + i i = i + 2 If i = 7 Then Exit Do Loop