求问电脑概论基础练习题…哭…

请输入图片描述

已知m=5,n=3 求最后m n k分别是多少。算的时候要从右边开始算嘛?n+1开始吗… 为啥感觉k一直都是1…

阅读 5.3k
11 个回答

这种比较时候用do while来做吧。先执行一次括号里的,再判断K。当k小于等于0时执行大括号里面的操作,否则退出。估计题主应该看得懂C语言吧:

int 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);
printf("m=%d, n=%d, k=%d", m, n,k); // m=3, n=-1, k=-1

打印结果:

m=3, n=3, k=3
m=3, n=1, k=1
m=1, n=3, k=3
m=3, n=-1, k=-1
m=3, 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]
新手上路,请多包涵

如果我没看错的话,应该是先算m-n然后再+1,最后赋值给k

新手上路,请多包涵

你只要深深的明白“=”不是等于,是赋值的意思就好了,把右边的值赋给左边。在纸上以m、n、k做三列,每做一个循环把他们的值标上。

logo
101 新手上路
子站问答
访问
宣传栏