从网上收集来的一些面试题和解题思路,加以整理,供参考。
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();
}