已知m=5,n=3 求最后m n k分别是多少。算的时候要从右边开始算嘛?n+1开始吗… 为啥感觉k一直都是1…
是不是要来一个PHP版的:
<?php
$m=5;
$n=3;
do {
$k=$m-$n+1;
$m=$n;
$n=$k;
}while($k>0);
echo "k=$k, m=$m, n=$n";
?>
可以说我是现学的objectivec吗:
NSInteger m = 5, n = 3;
do {
k = m - n + 1;
m = n;
n = k;
printf("m=%d, n=%d, k=%d", m, n,k);
} while( k > 0);
NSLog(@"m=%d,n=%d,k=%d",m,n,k);
再来个AS版本的
var k:int;
var m:int = 5;
var n:int = 3;
do {
k = m - n + 1;
m = n;
n = k;
trace("m=" + m + " ,n=" + n + " ,k=" + k);
} while( k > 0 )
trace("m=" + m + " ,n=" + n + " ,k=" + k);
直译为 Python 代码如下:
m = 5
n = 3
k = m - n + 1
m = n
n = k
while k > 0:
k = m - n + 1
m = n
n = k
print(m, n, k)
结果是 3 -1 -1
。
楼主说的java 的?
public class ForMeizhi {
public static void main(String[] args) {
int m=5,n=3,k=0;
do{
k=m-n+1;
m=n;
n=k;
}while(k>0);
System.out.println("m=" + m + ",n="+n+",k="+k);
}
}
结果:m=3,n=-1,k=-1
ps:k一开始k=5-3+1=3;等号右边是没有括号优先级,那就是先m-n再+1的
ruby:
m = 5
n = 3
k = m - n + 1
m = n
n = k
while k > 0
k = m - n + 1
m = n
n = k
end
puts m,n,k
Golang语言
package main
func main() {
m := 5
n := 3
k := m - n + 1
m = n
n = k
for k > 0 {
k = m - n + 1
m = n
n = k
}
println("m=",m,"n=",n, "k=",k) // m=3, n=-1, k=-1
}
诶?js
var m = 5, n = 3 ,k;
do {
k = m - n + 1;
m = n;
n = k;
console.log( 'm = ' + m + '; n = ' + n + '; k = ' + k );
} while( k > 0);
console.warn( 'm = ' + m + '; n = ' + n + '; k = ' + k );
aauto是啥……
begin //语句块开始
io.open();
var m,n = 5,3;
var k;
do {
k = m - n + 1;
m = n;
n = k;
io.print(string.format("m = %i; n = %i; k = %i",m, n, k));
} while( k > 0);
io.print(string.format("m = %i; n = %i; k = %i",m, n, k));
end; //语句块结束
一个稍微更pythonic的pytyhon:
def check(m,n):
k = m-n+1
m, n = n, k
if not k <= 0:
return check(m, n)
else:
return [m,n,k]
print check(5, 3)
输出:
[3, -1, -1]
这种比较时候用do while来做吧。先执行一次括号里的,再判断K。当k小于等于0时执行大括号里面的操作,否则退出。估计题主应该看得懂C语言吧:
打印结果: