如何通过循环实现愿望输出
how to implement desire output by loop
如何仅使用循环产生以下输出
iteration 1 = 54321
iteration 2 = 43215
iteration 3 = 32145
iteration 4 = 21345
iteration 5 = 12345
我能理解的语言有C++、JavaScript、Php、PLSQL(最好是PLSQL)
在C++11中:由于你没有解释逻辑,我做了最简单的^^:
int main () {
const int numbers[5] = {54321, 43215, 32145, 21345, 12345};
for (auto number : numbers) {
std::cout << number << std::endl;
}
return 0;
}
您给出的结果似乎是选择排序的结果。您必须实现以下算法:
For i = N downto 2
Let b = index of the biggest element from 1 to i
if b != i
Exchange item b with item i
C++
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i=9; i>4; i--){
for(j=5; j>=1; j--){
cout<<( ( (i+j)%5 ) +1 );
}
cout<<endl;
}
}
JavaScript
<script type="text/javascript">
var i, j;
for(i=9; i>4; i--){
for(j=5; j>=1; j--){
document.write( ( (i+j)%5 ) +1 );
}
document.write("<br />");
}
</script>
PL SQL块
declare
v_string varchar2(5) := '54321';
begin
for i in 1 .. length(v_string) loop
dbms_output.put_line('iteration ' || i || ' = ' || v_string);
v_string := substr('54321', 2) || substr('54321', 1, 1) ;
end loop;
end;
/
似乎没有人提到标准C++算法std::rotate
const size_t N = 5;
int a[N] = { 5, 4, 3, 2, 1 };
for ( size_t i = N; i != 0; i-- )
{
for ( int x : a ) std::cout << x;
std::cout << std::endl;
std::rotate( a, a + 1, a + i );
}
Please check this code. I think it will solve your query regarding iterations you require.Thanks
DECLARE
v_var VARCHAR2(100);
v_num NUMBER;
v_chnge VARCHAR2(100);
v_finl_chnge VARCHAR2(100);
v_frst_str VARCHAR2(100);
BEGIN
v_var:=trim('&enter');
v_num:=LENGTH(v_var);
dbms_output.put_line(LENGTH(v_var));
FOR i IN 1..v_num
LOOP
v_frst_str :=SUBSTR(v_var,-v_num,i);
v_chnge :=SUBSTR(v_var,i,v_num);
v_finl_chnge:=(v_chnge||v_frst_str);
dbms_output.put_line(SUBSTR(v_finl_chnge,-v_num,v_num));
END LOOP;
END;
------------------------
OUTPUT
------------------------------
anonymous block completed
5
34562
45623
56234
62345
23456
Clojure
由于这似乎是家庭作业,我给你一个你可以交的答案真的不合适——所以我会用一种稍微不同的语言给你答案,你可以直接翻译:
(let [i [1 2 3 4 5]
a [54321 43215 32145 21345 12345]]
(doall (map #(println "Iteration " %1 " = " %2) i a)))
分享并享受。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何通过循环实现愿望输出