C / C ++ Puzzle: to print values ​​from 1..15 15..1 with one loop for a loop

My colleague was provided to me to print the values 1 2 3 4 .... 15 15 ..... 4 3 2 1 with only one cycle, without functions, without goto statements and without using any conditional statements or triple statements .

So, I used typecasting to solve it, but this is not an exact solution, since 15 is not printed twice.

 int main() { int i, j; for(i = 1, j = 0;j < 29;j++, i += int(j/15)*-2 + 1) cout<<i<<endl; } 

Output : 1 2 3 4 ... 15 14 13 .... 2 1

Any alternative solutions?

+8
c ++ c puzzle
source share
11 answers

You can use a loop from 1 to 30, and then use the fact that (i / 16) will be β€œ0” for your upstream part and β€œ1” for your downstream part.

 for (int i = 1; i < 31; i++) { int number = (1-i/16) * i + (i/16) * (31 - i); printf("%d ", number); } 
+32
source share
 for (int i=0; i<1; i++) { std::cout << "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1" } 
+26
source share

How about this:

 std::string first; std::string second; for ( int i = 1 ; i <= 15 ; i++ ) { std::ostringstream s; s << i; first += s.str(); second = s.str() + second; } std::cout << first << second; 
+8
source share

Alternative:

 static int bla[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; for (int i = 0; i < 30; i++) { printf("%d\n", bla[i]); } 

Good, it is faster in execution compared to all ...

+5
source share

XOR bit # 4 (ie j & 0x10 ) with bits 3: 0. You will need to find a way to β€œrepeat” this bit at 4 positions.

+3
source share
 for (int i=1;i<31;++i) { cout<<(((i<<27>>31|i)&(~i<<27>>31|~i))&15)<<" "; } 
+3
source share
 #include <iostream> int main() { for(int i = 1; i < 31; i++) std::cout << ((i/16)-1)*-i+(i/16)*(i^0x1F) << " "; std::cout << std::endl; } 
+2
source share

I saw many complex answers, but no one used symmetry as is.

 std::string head = "1"; std::string tail = "1"; for (unsigned i = 2; i != 16; ++i) { std::string const elem = boost::lexical_cast<std::string>(i); head = head + " " + elem; tail = elem + " " + tail; } std::cout << head << " " << tail << "\n"; 

In ideone action (minus lexical_cast ):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

It works simply for any size of the upper limit (how much memory your computer has).

+2
source share
 const int N = 15; for(int i = 1; i <= 2 * N; ++i) printf("%d ", i + (i > N) * (1 + 2 * (N - i))); 
+2
source share
 for (int i = 1; i < 30; i++) printf("%d\n", (-((i & 16) >> 4) + 1) * i + ((i & 16) >> 4) * (14 - (i & 15))); 
+1
source share
 int main() { for(int i = 15, j = 30, k = 15; i <= 30; i++, j--, k -= 2) { cout << (j - i) * (k % 2) << endl << (j - i - 1) * (k % 2) << endl; } return 0; } 

This is what I came up with. There is another way, i.e. 15 β†’ 0 β†’ 15. Just more food for thought. Uses mod and k to develop if a negative number. I - I meet in the middle. This is by no means perfect, and there are other better answers.

+1
source share

All Articles