从网上收集来的一些面试题和解题思路,加以整理,供参考。

1.(比较无聊的循环)Change/add only one character and print '*' exactly 20 times.
(there are atleast 3 solutions to this problem :-)
int main()
{
 int i, n = 20;
 for (i = 0; i < n; i--)
  printf("*");
 return 0;
}

解1:
int main()
{
 int i, n = 20;
 for (i = 0; -i < n; i--)
  printf("*");
 return 0;
}

解2:
int main()
{
 int i, n = 20;
 for (i = 0; i < n;n--)
  printf("*");
 return 0;
}

解3:
int main()
{
 int i, n = 20;
 for (i = 0; i + n; i--)
  printf("*");
 return 0;
}

 

2. display below like:
        1
       212
      32123
     4321234
    543212345

解:(双重循环)
for (int i = 1; i <= n; i++)
{
    for (int j = 2 * n - 1; j >= 1; j--)
    {
        if (Math.Abs(j - n) >= i)
              Console.Write(' ');
        else
              Console.Write(Math.Abs(j - n) + 1);
    }
    Console.WriteLine();
}

相关文章:

  • 2021-07-20
  • 2021-10-09
  • 2021-06-04
  • 2022-01-08
  • 2021-09-16
  • 2022-01-17
  • 2021-10-19
  • 2021-04-09
猜你喜欢
  • 2022-12-23
  • 2021-08-01
  • 2021-10-10
  • 2021-07-15
  • 2021-08-07
  • 2021-04-22
  • 2022-12-23
相关资源
相似解决方案