English 中文(简体)
C.C.++ Puzzle: 印刷数值从1.15.1到1,1,1,1,1,1,1,1,1,1,1,1,1,3,0
原标题:C/C++ Puzzle: To print values from 1..15 15..1 with a single for loop [closed]
  • 时间:2012-04-11 11:44:07
  •  标签:
  • c++
  • c
  • puzzle

我的同事给出了以下文字值:1 2 3 4 . 15 ....... 4 2 1, 仅限no functionno goto statements, 不得使用任何有条件声明或私人操作者<>。

因此,我为解决这一问题而进行播种,但这不是一个准确的解决办法,因为15份不是两次印刷。

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

任何替代解决办法?

最佳回答

您可以从1到30岁,然后使用以下事实(i/16)将“0”用于您的赞助部分,而“1”则用于您的赞助部分。

for (int i = 1; i < 31; i++)
{
    int number = (1-i/16) * i + (i/16) * (31 - i);
    printf("%d ", number);
}
问题回答
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"
}

如何做到这一点:

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;

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
", bla[i]);
}

情况良好,执行速度快于所有......

XOR bit #4 (i.e. j & 0x10) with bits 3:0. 你们需要找到一种办法,把这比喻为4个职位。

for (int i=1;i<31;++i)
{
  cout<<(((i<<27>>31|i)&(~i<<27>>31|~i))&15)<<" ";
}
#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;
}
const int N = 15;
for(int i = 1; i <= 2 * N; ++i)
    printf("%d ", i + (i > N) * (1 + 2 * (N - i)));

我看到许多复制<>/em>答案,但没有人利用作为

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 << "
";

ideone (minus lexical_cast):

A. 导 言

它只是为了达到最高层(就你的电脑来说是足够的记忆)而工作。

for (int i = 1; i < 30; i++)
    printf("%d
", (-((i & 16) >> 4) + 1) * i + ((i & 16) >> 4) * (14 - (i & 15)));
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;
}

这是我提出的。 换言之,即:15 ->0 ->15。 更多的思考食物。 如果是负数,则使用 mo和 k。 I - J在中间举行会议。 它绝不是完美的,还有其他一些更好的答案。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?