回答另一个Stack Overflow问题(这个)我偶然发现了一个有趣的子问题.排序6个整数数组的最快方法是什么?
由于问题是非常低的水平:
&&或||).真的这个问题是一种高尔夫,其目标不是最小化源长度而是执行时间.我把它叫做"Zening"代码在本书的标题中的代码优化禅由迈克尔·亚伯拉什及其续集.
至于为什么它很有趣,有几个层次:
这是我的参考(天真的,未优化的)实现和我的测试集.
#include <stdio.h>
static __inline__ int sort6(int * d){
char j, i, imin;
int tmp;
for (j = 0 ; j < 5 ; j++){
imin = j;
for (i = j + 1; i < 6 ; i++){
if (d[i] < d[imin]){
imin = i;
}
}
tmp = d[j];
d[j] = d[imin];
d[imin] = …Run Code Online (Sandbox Code Playgroud) 我只会从用户那里获取3个值,因此只有3个可能的答案.您还可以假设用户不会输入0三次,并且如果他们三次输入相同的值则无关紧要.我可以为这些场景创建我自己的东西(我已经拥有).
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Triangles
{
public static void main (String[]args) throws IOException
{
String a = "You have an";
String b = "triangle";
double l;
Scanner lengthinput = new Scanner(System.in);
double h;
Scanner heightinput = new Scanner(System.in);
double w;
Scanner widthinput = new Scanner(System.in);
System.out.println("Enter the length.");
l = lengthinput.nextDouble();
System.out.println("Enter the height.");
h = heightinput.nextDouble();
System.out.println("Enter the width.");
w = widthinput.nextDouble();
double max = l;
if (w > max) max = w;
if …Run Code Online (Sandbox Code Playgroud)