找出输入中有多少对
Find how many equal pairs there is in input
输入是一系列以 0 结尾的正整数。我的任务是打印出这个系列包含多少对相等的数字。
例如:
输入:2 34 34 4 6 2 27 27 8 8 8 5 0
对:4(34 & 34、27 & 27、8 & 8、8 & 8)
到目前为止我已经想到了这个,但我想我真的迷路了。
import java.util.Scanner;
public class P3_5 {
public static void main(String[]args) {
int k1 = 0;
int k2 = 0;
int pair = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Input series of positive integers: ");
while (scan.nextInt() != 0) {
k1 = scan.nextInt();
k2 = scan.nextInt();
if (k1 == k2) {
pair++;
}
}
System.out.print(pair);
}
}
您应该只在循环中请求一个新的整数。在循环外请求初始 int,然后在循环内请求一个。在循环中将新的 k2
与 k1
进行比较并可能增加 pair
。然后设置k1 = k2
,继续下一次循环迭代。
您可以查看 HashMaps 以记住您已经看过的数字以避免多次计算它们
你不应该一次要求两个输入,因为一个数字可以与其他两个数字形成一对,就像 maloomeister 在评论中已经说过的那样。
根据你的问题,我假设一对是两个连续相等的数字。此外,一个数字可以与其他两个数字形成一对(在两个以上相等的连续数字的情况下),在您的示例中为 8
。
您必须以某种方式记住前一个数字,以便在每次迭代中将其与当前数字进行比较。由于某个数可以成对出现,可以推导出k个连续数,对数为k − 1。所以我们需要记录当前连续相等数字序列的长度,如果它重置为零(即当出现另一个数字时),那么我们将它添加到总对数中:
// The total number of pairs found
int pairs = 0;
// The current number of consecutive equal numbers (always >= 1)
int sequence = 1;
// Record the previous number encountered. Since the first element of an
// array cannot be compared with the previous one (since there is none!),
// we will need to start at the second element.
int last = ints[0];
// It is assumed that the length of the array is at least one. You should,
// however, add code to check if the length is not zero.
for (int i = 1; i < ints.length; i++) {
// Check if the current number matches the previous one. If true, then
// our sequence of equal values increments
if (ints[i] == last) {
sequence++;
}
else {
// If the current element is not equal to the previous one, then
// our sequence of equal numbers has ended. In that case, add the
// length of the sequence minus one to the total number of pairs.
pairs += (sequence - 1);
// ...and reset the sequence length
sequence = 1;
}
// Update the previously seen number
last = ints[i];
}
我这里用的是数组,但是不用ints[]
,你也可以要求用户输入一个int。
因为您显然只需要计算连续的对数。这意味着 8,8,8 产生 [8,8] 和 [8,8],因为第一个和第三个 8 不是连续的。你可以用一个简单的循环来完成。
int k = 0;
int count = 0;
int previousK = 0;
String s = "2 34 34 4 6 2 27 27 8 8 8 5 0";
Scanner input = new Scanner(s);
String pairs = "";
while ((k = input.nextInt()) != 0) {
// if current number equals previous, a pair has been found.
if (k == previousK) {
// update output string and count
pairs += k + " & " + k + ", ";
count++;
}
// update previous value with this value
previousK = k;
}
// get rid of lingering ","
int len = pairs.length()-2;
System.out.println("Pairs: = " + count + " " + pairs.substring(0,len)+")");
版画
Pairs: 4 (34 & 34, 27 & 27, 8 & 8, 8 & 8)
输入是一系列以 0 结尾的正整数。我的任务是打印出这个系列包含多少对相等的数字。
例如:
输入:2 34 34 4 6 2 27 27 8 8 8 5 0
对:4(34 & 34、27 & 27、8 & 8、8 & 8)
到目前为止我已经想到了这个,但我想我真的迷路了。
import java.util.Scanner;
public class P3_5 {
public static void main(String[]args) {
int k1 = 0;
int k2 = 0;
int pair = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Input series of positive integers: ");
while (scan.nextInt() != 0) {
k1 = scan.nextInt();
k2 = scan.nextInt();
if (k1 == k2) {
pair++;
}
}
System.out.print(pair);
}
}
您应该只在循环中请求一个新的整数。在循环外请求初始 int,然后在循环内请求一个。在循环中将新的 k2
与 k1
进行比较并可能增加 pair
。然后设置k1 = k2
,继续下一次循环迭代。
您可以查看 HashMaps 以记住您已经看过的数字以避免多次计算它们
你不应该一次要求两个输入,因为一个数字可以与其他两个数字形成一对,就像 maloomeister 在评论中已经说过的那样。
根据你的问题,我假设一对是两个连续相等的数字。此外,一个数字可以与其他两个数字形成一对(在两个以上相等的连续数字的情况下),在您的示例中为 8
。
您必须以某种方式记住前一个数字,以便在每次迭代中将其与当前数字进行比较。由于某个数可以成对出现,可以推导出k个连续数,对数为k − 1。所以我们需要记录当前连续相等数字序列的长度,如果它重置为零(即当出现另一个数字时),那么我们将它添加到总对数中:
// The total number of pairs found
int pairs = 0;
// The current number of consecutive equal numbers (always >= 1)
int sequence = 1;
// Record the previous number encountered. Since the first element of an
// array cannot be compared with the previous one (since there is none!),
// we will need to start at the second element.
int last = ints[0];
// It is assumed that the length of the array is at least one. You should,
// however, add code to check if the length is not zero.
for (int i = 1; i < ints.length; i++) {
// Check if the current number matches the previous one. If true, then
// our sequence of equal values increments
if (ints[i] == last) {
sequence++;
}
else {
// If the current element is not equal to the previous one, then
// our sequence of equal numbers has ended. In that case, add the
// length of the sequence minus one to the total number of pairs.
pairs += (sequence - 1);
// ...and reset the sequence length
sequence = 1;
}
// Update the previously seen number
last = ints[i];
}
我这里用的是数组,但是不用ints[]
,你也可以要求用户输入一个int。
因为您显然只需要计算连续的对数。这意味着 8,8,8 产生 [8,8] 和 [8,8],因为第一个和第三个 8 不是连续的。你可以用一个简单的循环来完成。
int k = 0;
int count = 0;
int previousK = 0;
String s = "2 34 34 4 6 2 27 27 8 8 8 5 0";
Scanner input = new Scanner(s);
String pairs = "";
while ((k = input.nextInt()) != 0) {
// if current number equals previous, a pair has been found.
if (k == previousK) {
// update output string and count
pairs += k + " & " + k + ", ";
count++;
}
// update previous value with this value
previousK = k;
}
// get rid of lingering ","
int len = pairs.length()-2;
System.out.println("Pairs: = " + count + " " + pairs.substring(0,len)+")");
版画
Pairs: 4 (34 & 34, 27 & 27, 8 & 8, 8 & 8)