需要这个小程序的最快算法 [英] Need the fastest algorithm for this small program

查看:125
本文介绍了需要这个小程序的最快算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我需要这个问题的最快算法代码:

打印数字从1到75但如果数字是5的倍数打印Foo,如果数字是7的打印数量的多个Bar如果数字是5和7的倍数打印FooBar



我想用if if else检查数字是否可分由5,7和两者组成。

 如果(n%5 ==  0 
系统。 out .print( Foo
else if (n%7 == 0
系统。 out .print( Bar
else if (n%5 == 0&& n%7 == 0
系统。 out .print( FooBar
else
系统。 out .print(n)



所以如果你有更好的效率和更快的代码请发布。



谢谢

Sabin

解决方案

只需将n%5 == 0 && n%7 == 0的检查放到第一个if的顶部。这将解决您的问题。



问候,



Manfred


< blockquote>感谢Sandeep快速回复。实际上我是新来的C#这里是我的完整代码:

  static   void  Main( string  [] args)
{
int n = 1 ;
while (n < = 75
{
if (n% 5 == 0
System.Console.WriteLine( Foo );
else if (n%7 == 0
System.Console.WriteLine( Bar);
else if (n%5 == 0 && n% 7 == 0
系统。 Console.WriteLine( FooBar);
else
System.Console.WriteLine(n);
n ++;
}



它正确打印可被5和7整除的数字,但打印Foo的数字可以整除我的5和7.SO你能分辨吗?我对代码有什么问题。因为我是C#的新手,这是这个程序最快的算法。


更小更快:



  void  Solution3()
{
for int n = 1 ; n < = 75 ; n ++)
{
if (n% 35 == 0
Console.WriteLine( FooBar);
else if (n% 5 == 0
Console.WriteLine( );
else if (n% 7 == 0
Console.WriteLine( );
else
Console.WriteLine(n);
}
}





问候!



Axel Kemper


Actually I need the fastest algorithm code for this problem:
Print numbers from 1 to 75 but if numbers are multiple of 5 print "Foo", if numbers are multiple of 7 print "Bar" and if numbers are multiple of both 5 and 7 print "FooBar"

I am thinking to use if else for checking the number is divisible by 5,7 and by both.

if(n%5==0)
System.out.print("Foo")
else if(n%7==0)
System.out.print("Bar")
else if(n%5==0&&n%7==0)
System.out.print("FooBar")
else
System.out.print(n)


So please post if you have any better code which is more efficiency and fastest than this.

Thanks
Sabin

解决方案

Just put the check for both n%5 == 0 && n%7 == 0 to the top into the first if. That will solve your problem.

Regards,

Manfred


Thanks Sandeep for quick respond.Actually i am new at the C# here is my full code:

static void Main(string[] args)
        {
            int n = 1;
            while (n <= 75)
            {
                if (n % 5 == 0)
                    System.Console.WriteLine("Foo");
                else if(n%7==0)
                    System.Console.WriteLine("Bar");
                else if (n%5==0 && n % 7 == 0)
                    System.Console.WriteLine("FooBar");
                else
                    System.Console.WriteLine(n);
                n++;
            }


It print correctly for number divisible by 5 and 7 but it print "Foo" for number is divisible my both 5 and 7.SO can you tell me what is wrong with code.As i am new to C# and is this is the fastest algorithm for this program.


A bit smaller and faster:

void Solution3()
        {
            for (int n = 1; n <= 75; n++)
            {
                if (n % 35 == 0)
                    Console.WriteLine("FooBar");
                else if (n % 5 == 0)
                    Console.WriteLine("Foo");
                else if (n % 7 == 0)
                    Console.WriteLine("Bar");
                else
                    Console.WriteLine(n);
            }
        }



Greetings!

Axel Kemper


这篇关于需要这个小程序的最快算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆