田忌赛马的java代码 田忌赛马acm
Java代码执行效率问题
效率是一样的,虽然语句不一样,但是编译成class文件后,都是相同的JVM指令。
成都创新互联专注于企业全网整合营销推广、网站重做改版、松江网站定制设计、自适应品牌网站建设、HTML5建站、商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为松江等各大城市提供网站开发制作服务。
新建一个Java文件Test2.java,然后把它编译成Test2.class
public class Test2 {
public static boolean stringIsNull(String str) {
if (str == null)
return true;
return false;
}
public static boolean stringIsNull2(String str) {
if (str == null)
return true;
else
return false;
}
public static boolean stringIsNull3(String str) {
return str == null;
}
}
然后你可以通过JDK自带的javap命令查看JVM指令,三个方法的JVM指令是一样的
JAVA安装目录/bin/javap -c Test2
Compiled from "Test2.java"
public class Test2 extends java.lang.Object{
public Test2();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."init":()V
4: return
public static boolean stringIsNull(java.lang.String);
Code:
0: aload_0
1: ifnonnull 6
4: iconst_1
5: ireturn
6: iconst_0
7: ireturn
public static boolean stringIsNull2(java.lang.String);
Code:
0: aload_0
1: ifnonnull 6
4: iconst_1
5: ireturn
6: iconst_0
7: ireturn
public static boolean stringIsNull3(java.lang.String);
Code:
0: aload_0
1: ifnonnull 8
4: iconst_1
5: goto 9
8: iconst_0
9: ireturn
}
田忌赛马C++程序源代码 不用class的
#include iostream
#include string
#include vector
#include algorithm
#include time.h
const std::string GetGongziHorse(std::vectorstd::string horse)
{
int size = horse.size();
int choose = rand()%size;
std::string horseChoosed = horse[choose];
std::vectorstd::string::iterator it
= find(horse.begin(), horse.end(), horseChoosed);
horse.erase(it);
return horseChoosed;
}
const std::string GetTianjiHorse(const std::string horseChoosed)
{
if (strcmp(horseChoosed.c_str(), "千里马") == 0)
return "劣马";
else if (strcmp(horseChoosed.c_str(), "好马") == 0)
return "千里马";
else
return "好马";
}
int main(void)
{
srand(time(NULL));
std::string strHorse[] = {"千里马", "好马", "劣马"};
std::vectorstd::string horseGongzi;
horseGongzi.insert(horseGongzi.end(), strHorse, strHorse + 3);
int i = 1;
while(horseGongzi.size())
{
std::string horseChoosed = GetGongziHorse(horseGongzi);
std::cout "第" i "场" std::endl;
std::cout "虚空公子派出了:" horseChoosed std::endl;
std::cout "田忌派出了:"
GetTianjiHorse(horseChoosed) std::endl;
i++;
std::cout std::endl;
}
std::cout "田忌无耻的赢了" std::endl;
getchar();
return 0;
}
用c语言写出田忌赛马的流程图和程序
根据流程图,我写了一个完整的程序给你参考。
源代码如下(vc++6.0下编译通过):
#include stdio.h
int main(void)
{
int x=0,max=0,min=0,i=0,s=0;
printf("please input a number(x): \n");
scanf("%d", x);
max = x;
min = x;
当前文章:田忌赛马的java代码 田忌赛马acm
转载注明:http://ybzwz.com/article/dojeeog.html