基本问题:(for)循环迭代多少次?
for(int i=start;i<=end;i+=step){
System.out.println("Test");
}
开始<末
多久的运行循环,分别是什么数学公式?
基本问题:(for)循环迭代多少次?
for(int i=start;i<=end;i+=step){
System.out.println("Test");
}
开始<末
多久的运行循环,分别是什么数学公式?
你for
循环是以下代码的简写:
int i = start;
if (i <= end) {
/* loop body */
i += step;
}
要回答你的问题,它将运行ceiling((end - start + 1)/step)
倍。通过纸上的逻辑来看看你是否得出了相同的结论。
start = 0,end = 10,step = 3。迭代4次,但(10-0)/3=3.33 – user 2014-09-04 01:10:05
@user将'ceiling'函数应用于结果。 – 2014-09-04 01:40:55
我们需要知道开始,结束和步骤的值是什么。
if:
start = 0;
end = 10;
step = 1;
它将循环11次,每次加1到i的先前值,直到它< = 10(0,1,2,3,4,5,6,7,8,9 ,10,11)
if:
start = 0;
end = 10;
step = 2;
它将循环6次,每次加入2至i的先前值,直到它< = 10(0,2,4,6,8,10)
if:
start = 10;
end = 100;
step = 10;
它会循环10次,每次将10加到先前的值直到它是< = 100.(10,20,30,40,50,60,70,80,90,100)
等等。
这就是要点。我需要知道(数学)答案的变量,而不是数值。 – user 2014-09-04 01:23:29
另外,我们可以用'start','end'和'step'来给出一个数学函数。 – 2014-09-04 01:40:15
诡计问题?是'开始<结束'?你在身体里搀杂着“我”吗? – Thilo 2014-09-04 01:00:12
start
user
2014-09-04 01:03:08
无。它由编译器进行了优化。 – 2014-09-04 01:03:33