请输入头数:<input type="text" id="head"/><br/><br/><br/>
请输入脚数:<input type="text" id="foot"/><br/><br/><br/>
<input type="submit" id="sub"/>
<p id="ji"></p>
<p id="tu"></p>
</body>
<script>
var input1=document.getElementById("head")
var input2=document.getElementById("foot")
var sub=document.getElementById("sub")
var ji=document.getElementById("ji")
var tu=document.getElementById("tu")
sub.onclick=function(){
var txt1=input1.value
var txt2=input2.value
for(var i=1i<txt1i++){
if(i*2+(txt1-i)*4==txt2){
ji.innerText="鸡有"+i+"只"
tu.innerText="兔有"+(txt1-i)+"只"
}
}
}
</script>
就是个二元一次方程嘛,20年没碰的东西,很生疏但大概是这么推, j是鸡、t是兔:j + t = 1200
2j + 4t = 2800
2800 - 1200 = 2j + 4t - (j + t)
1600 = 2j + 4t - j - t
1600 = j + 3t
j + 3t - (j + t) = 1600 -1200
j + 3t - j - t = 400
2t = 400
t=200
1200 - 200 = j
j = 1000
然后 js 代码:
//条件A:鸡 + 兔 = 1200
//条件B:鸡*2 + 兔*4 = 2800
window.onload = function () {
//鸡兔都从0开始不断循环并加1,一直加到同时满足条件A、B时即为正确的值
//这个 j<2800 和 t<2800 累加的问题我也没办法解决
//js我也是半罐水,只能写个循环并给个2800这样比较大数再去暴力算解,肯定有更好的办法
for (var j = 0j <2800j++) {
for (var t = 0t <2800t++) {
if ( j + t == 1200 &&2*j+4*t==2800) {//同时满足条件A、B时即为正确的值
alert("鸡="+j+"只、"+"兔="+t+"只"+"\n"+"鸡腿="+j*2+"、兔腿="+t*4)
}
}
}
}
微机原理程序汇编语言,鸡兔同笼 H Fx+y=h
2x+4y=f
y = (f-2h)/2 = f/2-h
x = h-y = 2h-f/2
CODE SEGMENT
ASSUME CS:CODE
H DW ?
F DW ?
X DW ?
Y DW ?
MSG DB 'please input H,F=$'
MSG2 DB 'chicken,rabbit=$'
START: PUSH CS
POP DS
PUSH CS
POP ES
LEA DX,MSG
MOV AH,9
INT 21H
CALL INPUTNUM
CALL LFCR
MOV AX,WORD PTR ARRAY
MOV H,AX
MOV AX,WORD PTR ARRAY+2
MOV F,AX
SHR F,1
JC START 不能整除转
MOV AX,F
SUB AX,H
JS START 不够相减转
MOV Y,AX
MOV AX,H
SHL AX,1
SUB AX,F
JS START 不够相减转
MOV X,AX
LEA DX,MSG2
MOV AH,9
INT 21H
MOV AX,X
CALL DSPAX
MOV AX,Y
CALL DSPAX
MOV AH,4CH
INT 21H
===============================================
键盘输入数值数组子程序(数组名为array,元素个数存放在num中)
INPUTNUM PROC NEAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DI
PUSH BP
PUSHF
输入的数据以一个空格分隔,以回车符结束输入
LEA DI,CS:ARRAY
MOV WORD PTR CS:NUM,0
STIN:
MOV AX,0
PUSH AX
AGAIN1:
MOV AH,0CH
MOV AL,1
INT 21H
MOV BYTE PTR CS:CHAR,AL
CMP AL,13
JE LINE0
CMP AL,' '
JE LINE0
SUB AL,30H
MOV AH,0
MOV SI,AX
POP AX
MOV CL,10
MOV CH,0
MUL CX
ADD AX,SI
PUSH AX
JMP AGAIN1
LINE0:
POP AX
MOV WORD PTR CS:[DI],AX
INC WORD PTR CS:NUM
CMP BYTE PTR CS:CHAR,13
JE STINEND
INC DI
INC DI
JMP STIN
STINEND:
POPF
POP BP
POP DI
POP SI
POP DX
POP CX
POP BX
POP AX
RET
ARRAY DW 200 DUP(0)
NUM DW 0
CHAR DB ?
INPUTNUM ENDP
=====================================
DSPAXS PROC NEAR
PUSH AX
TEST AX,8000H
JNS @DSPAXS1
PUSH AX
PUSH DX
MOV AH,2
MOV DL,'-'
INT 21H
POP DX
POP AX
NEG AX
@DSPAXS1:
CALL DSPAX
POP AX
RET
DSPAXS ENDP
===================================
DSPAX PROC NEAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSHF
XOR CX,CX
MOV BX,10
@DSPAX1:
XOR DX,DX
DIV BX
INC CX
OR DX,30H
PUSH DX
CMP AX,0
JNE @DSPAX1
MOV AH,2
@DISPAX2:
POP DX
INT 21H
LOOP @DISPAX2
POPF
MOV DL,32
INT 21H
POP DX
POP CX
POP BX
POP AX
RET
DSPAX ENDP
===================================
LFCR PROC NEAR
PUSH AX
PUSH DX
MOV AH,2
MOV DL,13
INT 21H
MOV DL,10
INT 21H
POP DX
POP AX
RET
LFCR ENDP
=====================================
CODE ENDS
END START