在8086汇编语言中,运用子程序的调用编写一个完整程序,实现从键盘输入(带回显)一个长度在10字符以内的字符串,并保存在缓冲区;检测并删除字符串中的数字字符,将字符串中所有小写字母改为大写字母;将处理后的字符串在新的一行显示,若字符串为空串则显示‘?’。并且要求,以下两个功能使用两个子程序实现:(1)检测并且清除数字字符(2)字符串中的小写字母改成大写字母
; 字符串输入、处理和输出程序
.486
.model small
.stack 100h
; 定义缓存区
buffer db 10, ?, 10 dup ('$')
; 子程序1:删除数字字符
proc remove_digits
push ax
push bx
push cx
mov cx, 0
mov bx, 0
; 按字符循环处理
loop_start:
mov al, buffer[bx]
cmp al, 0 ; 判断是否为结束符
je done
; 判断是否为数字字符
cmp al, "0"
jb not_digit
cmp al, "9"
ja not_digit
; 如果是数字字符,将其替换为空格
mov buffer[bx], " "
not_digit:
inc bx
inc cx
cmp cx, 10 ; 判断是否到达缓存区的末尾
jne loop_start
done:
pop cx
pop bx
pop ax
ret
endp
; 子程序2:小写转大写
proc lowercase_to_uppercase
push ax
push bx
push cx
mov cx, 0
mov bx, 0
; 按字符循环处理
loop_start:
mov al, buffer[bx]
cmp al, 0 ; 判断是否为结束符
je done
; 判断是否为小写字母
cmp al, "a"
jb not_lowercase
cmp al, "z"
ja not_lowercase
; 如果是小写字母,将其转换为大写字母
sub al, 20h
mov buffer[bx], al
not_lowercase:
inc bx
inc cx
cmp cx, 10 ; 判断是否到达缓存区的末尾
jne loop_start
done:
pop cx
pop bx
pop ax
ret
endp
.code
main proc
mov ax, @data
mov ds, ax
; 清空缓冲区
mov cx, 10
mov di, 2
mov al, '$'
rep stosb
; 显示提示信息
mov ah, 9
lea dx, prompt1
int 21h
; 从键盘输入字符串
lea dx, buffer
mov ah, 0Ah
int 21h
; 检测并删除数字字符
call remove_digits
; 小写字母转大写字母
call lowercase_to_uppercase
; 显示处理后的字符串
mov ah, 9
lea dx, prompt2
int 21h
lea dx, buffer+2
mov ah, 9
int 21h
; 如果字符串为空,则输出'?'
mov bx, buffer+1
mov al, byte ptr [bx]
cmp al, 0
jne exit
mov ah, 9
lea dx, prompt3
int 21h
jmp exit
prompt1 db 'Please enter a string (maximum 10 characters): $'
prompt2 db 'The processed string is: $'
prompt3 db '?$'
exit:
mov ah, 4ch
int 21h
main endp
end main
.MODEL SMALL
.STACK 100H
.DATA
buffer DB 11 DUP ('$') ; 缓冲区,长度为11,多出来的一个字节用于存储字符串结束符'$'
prompt DB 'Enter a string (up to 10 characters): $'
output DB 13, 10, 'Processed string: $'
empty DB '?', 13, 10, '$'
digits DB '0123456789'
lowercase DB 'abcdefghijklmnopqrstuvwxyz'
uppercase DB 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; 显示提示信息
LEA DX, prompt
MOV AH, 09H
INT 21H
; 读取字符串
LEA DX, buffer
MOV AH, 0AH
INT 21H
; 检测并清除数字字符
CALL remove_digits
; 将小写字母改成大写字母
CALL to_uppercase
; 显示处理后的字符串
MOV SI, OFFSET buffer
MOV CL, [SI] ; 获取字符串长度
CMP CL, 0 ; 判断字符串是否为空串
JE empty_string ; 如果是空串则跳转到empty_string标签
LEA DX, output
MOV AH, 09H
INT 21H
MOV DL, '['
MOV AH, 02H
INT 21H
INC SI ; 跳过字符串长度字节
MOV CX, 10 ; 最多显示10个字符
LOOP1:
MOV DL, [SI]
CMP DL, '$' ; 判断是否到达字符串结尾
JE end_loop1
MOV AH, 02H
INT 21H
INC SI
LOOP LOOP1
end_loop1:
MOV DL, ']'
MOV AH, 02H
INT 21H
JMP exit_program
empty_string:
LEA DX, empty
MOV AH, 09H
INT 21H
exit_program:
MOV AH, 4CH
INT 21H
MAIN ENDP
; 子程序:检测并清除数字字符
remove_digits PROC
MOV SI, OFFSET buffer
MOV CL, [SI] ; 获取字符串长度
MOV DI, SI ; DI指向当前字符
INC SI ; 跳过字符串长度字节
LOOP2:
MOV DL, [SI]
CMP DL, '$' ; 判断是否到达字符串结尾
JE end_loop2
MOV AH, 0
MOV CX, 10 ; 数字字符的个数
LEA BX, digits
LOOP3:
CMP DL, [BX]
JE found_digit
INC BX
LOOP LOOP3
; 如果不是数字字符,则将其保留
MOV [DI], DL
INC DI
found_digit:
INC SI
LOOP LOOP2
end_loop2:
MOV [DI], '$' ; 添加字符串结束符
RET
remove_digits ENDP
; 子程序:将小写字母改成大写字母
to_uppercase PROC
MOV SI, OFFSET buffer
MOV CL, [SI] ; 获取字符串长度
MOV DI, SI ; DI指向当前字符
INC SI ; 跳过字符串长度字节
LOOP4:
MOV DL, [SI]
CMP DL, '$' ; 判断是否到达字符串结尾
JE end_loop4
MOV AH, 0
MOV CX, 26 ; 小写字母的个数
LEA BX, lowercase
LOOP5:
CMP DL, [BX]
JE found_lowercase
ADD BX, 1 ; 每个字母占一个字节,所以加1
LOOP LOOP5
; 如果不是小写字母,则将其保留
MOV [DI], DL
INC DI
JMP next_char
found_lowercase:
ADD BX, 26 ; 小写字母和大写字母的ASCII码值相差26
MOV DL, [BX]
MOV [DI], DL
INC DI
next_char:
INC SI
LOOP LOOP4
end_loop4:
MOV [DI], '$' ; 添加字符串结束符
RET
to_uppercase ENDP
END MAIN
回答:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] s1 = scan.nextLine().split(" ");
scan.close();
int[] nums = new int[s1.length];
for (int i = 0; i < nums.length; i++)
nums[i] = Integer.parseInt(s1[i]);
int res = 0;
int left, right;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) continue;
left = i-1;
right = i+1;
int tmpRes = 1;
while (left >= 0 || right < nums.length) {
if ((left >= 0 && nums[left] == 1) || (right < nums.length && nums[right] == 1)) {
res = Math.max(res, tmpRes);
break;
}
if (left >= 0) left--;
if (right < nums.length) right++;
tmpRes++;
}
if (left < 0 && right >= nums.length)
res = Math.max(res, tmpRes);
}
System.out.println(res);
}
}