打印所有订购的3元组 [英] Print all ordered 3 tuples

查看:137
本文介绍了打印所有订购的3元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有人可以帮助我来个conculiton这个问题...

I was wondering if someone could help me come to a conculiton with this problem...

打印所有订购3元组(I,J,K)i的0 ..(N-1)中,J 0 ..(N-1)中,k在0 ..(N-1),在。字典序,每行一个元组3字典顺序(想想在字典单词),命令序列:在这两个序列的第一个不平等元素确定的顺序,例如:(0,1,5,3)≤(0 ,1,6,0)。

"Print all ordered 3 tuples (i,j,k) i in 0..(n-1), j in 0 .. (n-1), k in 0..(n-1), in lexicographical order, one 3 tuple per line. Lexicographical order (think about words in a dictionary), orders sequences: the first UNEQUAL element in the two sequences determines the order, e.g.: (0,1,5,3) < (0,1,6,0).

格式:开括号数数逗号逗号数近括号。使用附带的打印print3Tuple()方法,让大家在相同的格式打印。 (想一想:你会怎么必须改变你的code打印4元组,5元组,k元组?)
例如:对于n = 2,你的输出应为

Format: open-paren number comma number comma number close-paren. Use the print3Tuple() method provided to print, so that we all print in the same format. (Think about this: how would you have to change your code to print 4-tuples, 5-tuples, k-tuples?) Example: for n=2, your output should be"

所以我有两种方法,我卡在我的环...

so i have two methods and i'm stuck on my loops...

public void print3Tuple(int a, int b, int c) {
    System.out.println("(" +a+ "," +b+ "," +c+ ")" );
}

和则一:我在工作

public void print3Tuples(int n) {
    // Replace this body with your solution
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = j; k < n; k++) {
                print3Tuple(i, j, k);
            }
        }
    }

这本打印
(0,0,0)
(0,0,1)
(0,1,1)
(1,1,1)

which prints this (0,0,0) (0,0,1) (0,1,1) (1,1,1)

和我需要这个。

(0,0,0)
(0,0,1)
(0,1,0)
(0,1,1)
(1,0,0)
(1,0,1)
(1,1,0)
(1,1,1)

(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1)

我是卡住任何帮助将是AP preciated。

i'm stuck any help would be appreciated.

推荐答案

更改循环变量的初始值Ĵ K 0

Change the initial value of the loop variable j and k to 0:

public static void print3Tuples(int n) {
    // Replace this body with your solution
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                print3Tuple(i, j, k);
            }
        }
    }
}

这篇关于打印所有订购的3元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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