#7925: 此題的最快速解法為何


ZJOJSFUS (ZJOJSFUS)


個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快

#9211: Re:此題的最快速解法為何


linhunghsiang (呆瓜)


#include <iostream>

#include <cstdlib>

#include <ctime>

#include <string>

 

using namespace std;

 

int main(){

int i=0;

cin>>i;

 

             while(i>=1){

             i-=1;

             long long int x,y;

             int z;

             cin>>z;

             cin>>x;

             cin>>y;

             

                         if(z==1){

                                 cout<<x+y<<"\n";

                                 }

                         else if(z==2){

                                 cout<<x-y<<"\n";

                                 }

                         else if(z==3){

                                 cout<<x*y<<"\n";

                                 }

                         else if(z==4){

                                 cout<<x/y<<"\n";

                                 }

                         }

 

return 0;

 

}

 
 
44ms ^o^ 
#9212: Re:此題的最快速解法為何


linhunghsiang (呆瓜)


個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <string>

 

using namespace std;

 

int main(){

int i=0;

cin>>i;

 

             while(i>=1){

             i-=1;

             long long int x,y;

             int z;

             cin>>z;

             cin>>x;

             cin>>y;

             

                         if(z==1){

                                 cout<<x+y<<"\n";

                                 }

                         else if(z==2){

                                 cout<<x-y<<"\n";

                                 }

                         else if(z==3){

                                 cout<<x*y<<"\n";

                                 }

                         else if(z==4){

                                 cout<<x/y<<"\n";

                                 }

                         }

 

return 0;

 

}

44ms ^o^ 



#9213: Re:此題的最快速解法為何


linhunghsiang (呆瓜)


個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快


我40ms ...

 

用if , long long int ,cin ,cout 

#9214: Re:此題的最快速解法為何


linhunghsiang (呆瓜)


個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快


我40ms ...

 

用if , long long int ,cin ,cout 

:p

 

#9235: Re:此題的最快速解法為何


hugo8642612 (Hugowwwwwww)


可以用scanf和printf取代cin和cout

cin >> a;

scanf("%lld",&a);

 

cout<<a;

printf("%lld",a);

%lld 用在long long int 

%d 用在int  

 

#9236: Re:此題的最快速解法為何


hugo8642612 (Hugowwwwwww)


可以用scanf和printf取代cin和cout

cin >> a;

scanf("%lld",&a);

 

cout<<a;

printf("%lld",a);

%lld 用在long long int 

%d 用在int  

 

#9237: Re:此題的最快速解法為何


hugo8642612 (Hugowwwwwww)


可以用scanf和printf取代cin和cout

cin >> a;

scanf("%lld",&a);

 

cout<<a;

printf("%lld",a);

%lld 用在long long int 

%d 用在int  

 

#9238: Re:此題的最快速解法為何


hugo8642612 (Hugowwwwwww)


可以用scanf和printf取代cin和cout

cin >> a;

scanf("%lld",&a);

 

cout<<a;

printf("%lld",a);

%lld 用在long long int 

%d 用在int  

 


#9239: Re:此題的最快速解法為何


hugo8642612 (Hugowwwwwww)


阿...抱歉  電腦沒反應按了好幾下...



#9636: Re:此題的最快速解法為何


phantom (shadow)


 

個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快


#include <iostream>
#include <cmath>
using namespace std;
int main(){
long long int x,i,a,b,c;
while(cin>>x)
{
for(i=0;i<x;i++)
{
cin>>a>>b>>c;
if(a==1)
cout<<b+c<<endl;
else if(a==2)
cout<<b-c<<endl;
else if(a==3)
cout<<b*c<<endl;
else
cout<<b/c<<endl;
}
}
return 0;
}

 

 

36ms    微笑 


#9760: Re:此題的最快速解法為何


yinming963 (小傑)


個人用c++, 44ms AC

cin, for迴圈跑 switch

想請問此題如何寫得更快

 


C++,32m/s AC

 

我想說我很慢的說= = 

#9761: Re:此題的最快速解法為何


CSE310634 (123gogo)


可以用scanf和printf取代cin和cout

cin >> a;

scanf("%lld",&a);

 

cout<

printf("%lld",a);

%lld 用在long long int 

%d 用在int  

 


 

可以再用一個變數 去裝 b c的結果 在顯示那個變數 會比較快

 

28ms 

#10315: Re:此題的最快速解法為何


tyc40406 (Master)


#include <iostream>

#include <string>

 

using namespace std;

 

 

 

int main()

{

long long int number,b,c;

cin>>number;

int a[number];

 

for(int i = 0 ; i<number ; i++)

{

cin>>a[i]>>b>>c;

switch(a[i])

{

case 1:

cout<<b+c<<endl;

break;

case 2:

cout<<b-c<<endl;

break;

case 3:

cout<<b*c<<endl;

break;

case 4:

cout<<b/c<<endl;

break;

}

}

return 0;

}

C++32ms 請參考

 
#10876: Re:此題的最快速解法為何


noodlet (noodlet)


個人用C++ 28ms AC
沒有使用 if-else switch
反而用了函式指標

 

#include <iostream>

#include <cstdlib>

#include <math.h>

 

 

long op_add(long b,long c)

{

    return b+c;    

}

long op_sub(long b,long c)

{

    return b-c;    

}

long op_mult(long b,long c)

{

    return b*c;    

}

long op_div(long b,long c)

{

    return b/c;    

}

 

long (*Operator[4])(long,long) = {op_add,op_sub,op_mult,op_div};

int main()

    long N;

    std::cin >> N;

    

    long b;

    long c;

    long opIndex; // 運算符號引索 

 

    while(N>0)

    {

        N--;

        std::cin >> opIndex >> b >> c;

        std::cout << Operator[opIndex-1](b,c) << std::endl;

        

    }

};