1. [说明] 字符组合:程序界面如图示,在文本框Text1中输入若干个任意字符,单击“颠倒”按钮,将这些字符按相反的顺序显示在标签Lable1 中,并在标签Labs1 2处显示字符的个数。例如:输入abcabc2,显示为 2cbabca,组成字符数为4。 [Visual Basic 代码] Private Sub cmdshow_ Click ( Dim n As Integer Dim I As Integer Dim str1 As String N=Len ( Text1.Text ) For I=n To Step- 1 Str1= Next I Labe11. Caption=Str1 End Sub Private Sub cmdcount_ Click ( ) Dim n As Integer Dim in As Integer Dim i As Integer Dim j As Integer Dim flag As Integer Dim str1 As Integer Dim str2 As Integer N= Str1 =Mid$ ( Text1.Text, i. 1 For I =2 To n Str2=mid$ ( Text1,Text,I,1 M=Len (str1) For j=1 To m IfThen flag= 1; Exit For Next j If flag<>1 then str1=str1& str2 Next I Lable2.Caption= End Sub
1. [说明] 本程序求3~100之间的所有素数(质数)并统计个数;同时将这些素数从小到大依次写入顺序文件 E:\dataout.txt;素数的个数显示在窗体Form1上。 [Visual Basic 代码] Private Sub Command1 Click ( ) Dim count as integer, flag as Boolean Dim t1 as Integer, t2 as Integer Count=0 For t1=3 to 100 For t2=2 to Int (Sqr (t1)) IfThen flag=False Next t2 count=count +1 write #1, t1 End if Next t1 Close #1 End Sub
(1) open“ e:\dataout.txt”For Output as#1 (2) Flag=True (3) t1 mod t2=0 (4) If t2>Int (sqr (t1))Then (5) Form1. Print "素数个数";count
试题八 阅读以下说明和C++ 程序,将应填入 (n) 处的字句写在对应栏内。
1. [说明] 试从含有n个int 型数的数组中删去若干个成分,使剩下的全部成分构成一个不减的子序列。设计算法和编写程序求出数组的不减子序列的长。 [C++ 程序] #include<stdio.h> #define N 100 int b[]={9,8,5,4,3,2,7,6,8,7,5,3,4,5,9,1}; int a [N]; #define n sizeofb/sizeofb[0] void main ( ) { kit k,i,j; for (i=1;i<n; i++ ) { for ( j=k;; j--); ; /*长为 j+1 的子序列的终元素存储在 a[j+1]*/ if ( k++; /*最长不减子序列长 k 增1*/ } printf ( "K = %d\n ",k ); }