通过键盘输入一个高精度的正整数n(n的有效位数≤240),去掉其中任意s个数字后,剩下的数字按原左右次序将组成一个新的正整数。编程对给定的n和s,寻找一种方案,使得剩下的数字组成的新数最小。 【样例输入】 178543 S=4 【样例输出】 13
第1题:
阅读以下说明和C程序代码,将应填入______处的语句写在答题纸的对应栏内。
[说明]
函数MultibaseOutput(long n,int B)的功能是:将一个无符号十进制整数n转换成 B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
define MAXSIZE 32
typedef struct{
int * elem; /* 栈的存储区 */
int max; /* 栈的容量,即栈中最多能存放的元素个数 */
int top; /* 栈顶指针 */
}Stack;
[C代码]
int InitStack(Stack * S,int n) / * 创建容量为n的空栈 */
{ S->elem=(int *)malloc(n * sizeof(int));
if(S->elem==NULL)return-1;
S->max=n; (1)=O;return 0;
}
int Push(Stack * S,int item) / * 将整数item压入栈顶 * /
{ if(S->top==S->max){ printf(“Stack is full! \n”);return-1;}
(2)=item;return 0;
}
int StackEmpty(StackS) {return (! S.top)? 1:0;} / * 判断栈是否为空 * /
int Pop(Stack *S ) / * 栈顶元素出栈 * /
{ if(! S->top){printf(“Pop an empty stack! \n”);return-1;}
return (3);
}
void MultibaseOutput(long n,int B)
{ int m;StackS;
if (InitStack(&S,MAXSIZE)){printf(“Failure! \n”);return;}
do {
if(Push(&S, (4) )){printf(“Failure! \n”);return;}
n=(5);
}while(n!=0);
while(! StackEmpty(S)){ / * 输出B进制的数 * /
m=Pop(&S);
if(m<10)printf(“%d”,m); / * 小于10,输出数字 * /
else printf(“%c”,m+55); / * 大于或等于10,输出相应的字符 * /
}
printf(“\n”);
}
第2题:
( 9 ) 下面的程序执行时 , 可以从键盘输入一个正整数 , 然后把该数的每位数字按逆序输出 。 例如 : 输入 7685 ,则输出 5867 ,输入 1000 ,则输出 0001 。请填空。
Private Sub Command1_Click()
Dim x As Integer
x=InputBox( “ 请输入一个正整数 ” )
While x>_ _[11] ___
Print x Mod 10;
x=x\10
Wend
Print__ [12] __
End Sub
第3题:
以下程序的功能是:将输入的正整数按逆序输出。例如:若输入 135 则输出 531 。请填空。
#include <stdio.h>
main()
{ int n,s;
printf("Enter a number : "); scanf("%d",&n);
printf("Output: ");
do
{ s=n%10; printf("%d",s); 【 10 】 ; }
while(n!=0);
printf("\n");
}
第4题:
下列程序为求Sn=a+aa+aaa+……+aa…a(n个a),其中a为一个随机数产生的1~9(包括1、 9)中的一个正整数,n是一个随机数产生的5~10(包括5、10)中的一个正整数,请在空格处填入适当的内容,将程序补充完整。
Private Sub Form_CUCk()
Dima As Integer,n As Intege,S As Double,Sn As Double
a=Fix(9*Rnd)+1
n=Pix(6*Rnd)+5
Sn=0
S=0
Fori
第5题:
如果一个正整数从高位到低位上的数字依次递减,则称其为降序数(如:9632是降序数,而8516则不是降序数)。现编写如下程序,判断输入的正整数是否为降序数。 Private Sub Command1 Click( ) Dim n As Long Dim flag As Boolean n=InputBox("输入一个正整数") S=Trim(Str(n)) For i=2 To Len(s) If Mid(s,i-1,1)<Mid(S,i,1)Then Exit For Next i If i=Len(S)Then flag=True Else flag=False If flag Then Print n;"是降序数" Else Print n;"不是降序数" End If End Sub 运行以上程序,发现有错误,需要对给flag变量赋值的If语句进行修改。以下正确的修改是( )。
A.If i=Len(s)+1 Then flag=False Else flag=True
B.If i=Len(s)+1 Then flag=True Else flag=False
C.If i=Len(s)-1 Then flag=False Else flag=True
D.If i=Len(s)-1 Then flag=True Else flag=False
第6题:
( 6 ) 下面程序的功能是从键盘输入 1 大于 100 的整数 m ,计算并输出满足不等式
1+22+32 +42 + …… .+n2<m
的最大的 n 。请填空
Private Sub Command1__Click ()
Dim s ,m,n AS Integer
m=Val ( InputBox ( “ 请输入一个大于 100 的整数 ” ))
n=_________
s=0
Do While s<m
n=n+1
s=s+n*n
Loop
Print “ 满足不等式的最大 n 是_____
End Sub
第7题:
在集合S={0,1,…,n-1}(n为任意给定的正整数)上定义了二元运算*和,其中 *为模n乘法,?为模n加法,则<S,*,?>构成的代数系统为
A.域
B.格
C.环,但不一定是域
D.布尔代数
第8题:
试题四
阅读以下说明和C代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
函数MultibaseOutput(long n, int B)的功能是:将一个无符号十进制整数n转换成B(2≤B≤16)进制数并输出。该函数先将转换过程中得到的各位数字入栈,转换结束后再把B进制数从栈中输出。有关栈操作的诸函数功能见相应函数中的注释。C代码中的符号常量及栈的类型定义如下:
#define MAXSIZE 32
typedef struct {
int *elem; /* 栈的存储区 */
int max; /* 栈的容量,即栈中最多能存放的元素个数 */
int top; /* 栈顶指针 */
}Stack;
[C代码]
int InitStack(Stack *S, int n) /* 创建容量为n的空栈 */
{ S->elem = (int *)malloc(n * sizeof(int));
if(S->elem == NULL) return -1;
S->max = n; (1) = 0 ; return 0;
}
int Push(Stack *S, int item) /* 将整数item压入栈顶 */
{ if(S->top == S->max){ printf("Stack is full!\n"); return -1;}
(2) = item ; return 0;
}
int StackEmpty(Stack S) { return (!S.top) ? 1 : 0; } /* 判断栈是否为空 */
int Pop(Stack *S) /* 栈顶元素出栈 */
{ if(!S->top) { printf("Pop an empty stack!\n"); return -1;}
return (3) ;
}
void MultibaseOutput(long n, int B)
{ int m; Stack S;
if (InitStack(&S, MAXSIZE)) {printf("Failure!\n"); return;}
do {
if (Push(&S, (4) )) {printf("Failure!\n"); return;}
n = (5) ;
}while(n != 0);
while(!StackEmpty(S)) { /* 输出B进制的数 */
m = Pop(&S);
if(m < 10) printf("%d", m); /* 小于10,输出数字 */
else printf("%c", m + 55); /* 大于或等于10,输出相应的字符 */
}
printf("\n");
}
第9题:
下面程序的功能是从键盘输入—个大于100的整数m,计算并输出满足不等式
1+22+32+42+…+n2<m
的最大的n。请填空。
Private Sub Command1_Click()
Dim s,m,n As Integer
m=Val(InputBox("请输入一个大于100的整数"))
n=【 】
s=0
Do While s<m
n=n+1
s=s+n*n
Loop
Print "满足不等式的最大n是";【 】
End Sub
第10题:
已知在文件IN13.DAT中存有若干个(个数〈200)4位数字的正整数,函数ReadDat()的功能是读取这若干个正整数并存入数组xx中。请编制函数CalValue(),其功能要求: (1)求出该文件中共有多少个正整数totNum; (2)求这些数右移1位后,产生的新数是偶数的数的个数totCnt,以及满足此条件的这些数(右移前的值)的算术平均值totPjz,最后调用函数WriteDat()把所求的结果输出到文件OUT13.DAT中;
注意:部分源程序已给出。
请勿改动主函数main()、读函数ReadDat()和写函数WriteDat()的内容。
试题程序:
include〈stdio.h>
include〈conio.h>
define MAXNUM 200
int xx [MAXNUM];
int totNum = 0; /* 文件IN13.DAT 中共有多少个正整数 */
int totCnt = 0; /* 符合条件的正整数的个数*/
double totPjz = 0.0; /* 平均值 */
int ReadDat (void);
void Writedat(void);
void CalValue(void)
{
}
main ( )
{
int i;
clrscr ( );
for(i = 0; i〈 MAXNUM; i++)
xx[i] = 0;
if (ReadDat ())
{
print f ( "数据文件 IN13.DAT 不能打开! \007\n" );
return;
}
CalValue ( );
printf("文件IN13.DAT 中共有正整数= %d 个\n", totNum);
printf("符合条件的正整数的个数 = %d 个\n", totCnt);
printf("平均值=%.21f\n", totPjz);
Writedat ( );
}
int ReadDat (void)
{
FILE *fp;
int i = 0;
if((fp = fopen ("IN13.DAT", "r") == NULL)
return 1;
while(! feof(fp) )
{
fscanf(fp, "%d,", &xx[i++]);
}
fclose{fp);
return 0;
}
void WriteDat(void)
{
FILE *fp;
fp = fopen("OUT13.DAT", "w");
fprintf(fp, "%d\n%d\n%.21f\n", totNum, totCnt, totPjz);
fclose(fp);
}