今天突然看到一题 输出 Hello Word 很多牛人的算法 在这里存一份 以供以后闲来翻看...

  题目:

完成如下代码,使其输出“Hello World!”

1 if() printf("Hello"); else printf(" World!");

我很快想到了答案。然后同学告诉我这个题不止一个答案。

如果换成C#,也可以这样问:完成如下代码,使其输出“Hello World!”

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 namespace HelloWorld
6 {
7 class Program
8 {
9 staticvoid Main(string[] args)
10 {
11 if (/* 补充这里 */)
12 Console.Write("Hello");
13 else
14 Console.Write(" World!");
15 }
16 }
17 }

    一下是楼主总结的答案:   首先总结一下可行的做法
1、在条件中输出“Hello”,并返回false,典型的答案是  #6 的 wuyazhe#20 的 Bourbon1795 给出的
2、在条件中输出“Hello World!”,并终止程序,典型的答案是  #225 的 Bourbon1795 给出的
3、递归调用Main方法,典型的答案是  #25 的 bloodish#42 的 Bourbon1795 给出的
4、代码注入,典型的答案是  #27 的 baihacker 给出的;这个方法有犯规的嫌疑,不过依然可行
5、“并行”执行,在条件中实现类似linux中的fork()函数,典型的答案是下面的第三个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
if (/**/)
Console.Write("Hello");
else
Console.Write(" World!");
}
}
}

/*
* 1 wuyazhe #6
* new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()
*
* 2
* args.Length != 0 || System.Reflection.Assembly.GetEntryAssembly().EntryPoint.Invoke(null, new object[] { new string[] { "1" } }) != null
*
* 3
* args.Length != 0 || new Func<bool>(() =>
* {
* System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName, "1");
* startinfo.RedirectStandardOutput = true;
* startinfo.UseShellExecute = false;
* System.Diagnostics.Process proc = System.Diagnostics.Process.Start(startinfo);
* Console.Write(proc.StandardOutput.ReadToEnd());
* return false;
* }).Invoke()
*
* 4 bloodish #16
* (args = new string[1] { "Hello" }).Length > 0 && args.Any(s => { Console.Write(s); return false; })
*
* 5 Bourbon1795 #20
* Console.Write("Hello") is object
*
* 6 wuyazhe #22
* args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke()
*
* 7 bloodish #25
* args.Length == 0 ? (Main(args = new string[] { string.Empty }) is object) : true
*
* 8
* args.Length != 0 || Main(args = new string[] { string.Empty }) is object
*
* 9 baihacker #27 (这个很发散)
* true)Console.Write("Hello World!");else if (false
*
* 10 ZengHD #38
* Convert.ToBoolean(Type.GetType("System.Console").GetMethod("Write", new Type[] { System.Type.GetType("System.String") }).Invoke(null, new string[] { "Hello" }))
*
* 11 Bourbon1795 #42
* (args == null || Main(null) is object)
*
* 12 Bourbon1795 #222
* (!((new System.Threading.Thread(() => Console.Write("Hello"))).Start() is object) && System.Threading.Thread.Sleep(1000) is object)
*
* 13 Bourbon1795 #225
* Console.Write("Hello World!") is object || Environment.Exit(0) is object
*
* 14 drosh #513
* args.Length == 1 || typeof(Program).GetMethod("Main", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).Invoke(null, new object[] { new string[1] }) != null
*
*/


题目原地址: http://topic.csdn.net/u/20101009/20/1c312685-201b-447d-97bd-da71a8a45664.html

转载于:https://www.cnblogs.com/Relict/articles/2252364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值