如何通过循环实现愿望输出

how to implement desire output by loop

本文关键字:愿望 输出 实现 循环 何通过      更新时间:2023-10-16

如何仅使用循环产生以下输出

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)))

分享并享受。