面试题解(2):loop相关
從網(wǎng)上收集來的一些面試題和解題思路,加以整理,供參考。
1.(比較無聊的循環(huán))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
解:(雙重循環(huán))
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();
}
總結(jié)
以上是生活随笔為你收集整理的面试题解(2):loop相关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试题解(3):SQL
- 下一篇: Flutter:尝试撸一个具有惯性跟阻力