- 1
Infinite sequence
Write the first numbers of the infinite sequence 1 2 3 4 5 4 3 2 1 2 3 4 5 4... Examples: Input: 1 Output: 1 Input: 5 Output: 1 2 3 4 5 Input: 7 Output: 1 2 3 4 5 4 3 Please I need help with this
2 ответов
0
TheScan
You can use loops to do this.
#include <iostream>
using namespace std;
int main() {
int j=1,i=1,k=0;
int n;
cin>>n;
while(k<n){
if(i<=5){
cout<<i<<' ';
k++;
i++;
}
else
{cout<<5-j<<' ';
j++;
k++;
if(j==4){
i=1;
j=1;
}
}
}
return 0;
}
0
Thank you so much :)