英文:
Find how many equal pairs there is in input
问题
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("输入一系列正整数: ");
while ((k1 = scan.nextInt()) != 0) {
k2 = scan.nextInt();
if (k1 == k2) {
pair++;
}
}
System.out.print(pair);
}
}
英文:
Input is a series of positive integers with a 0 at the end. My task is to print out how many pairs of equal numbers this series contains.
For example:
Input: 2 34 34 4 6 2 27 27 8 8 8 5 0
Pairs: 4 (34 & 34, 27 & 27, 8 & 8, 8 & 8)
I have come up with this so far but I think I'm really lost.
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);
}
}
答案1
得分: 0
你应该在循环内部仅请求一个新的整数。在循环外部请求初始整数,然后在循环内部再请求一个。在循环内部获得一个新的整数赋值给 k2
后,将其与 k1
进行比较,也许增加 pair
。然后将 k1
设为 k2
,继续进行下一次循环迭代。
英文:
You should only ask for one new int within your loop. Ask for the initial int outside the loop and then ask for one within the loop. After you get a new one into k2
inside the loop compare it against k1
and maybe increment pair
. Then set k1 = k2
and continue with the next loop iteration.
答案2
得分: 0
你可以查看HashMaps,以便记住您已经看过的数字,以避免多次计数。
英文:
You may have a look onto HashMaps in order to remember which numbers you have already seen to avoid to count them multiple times
答案3
得分: 0
你不应该同时要求两个输入,因为一个数字可以与另外两个数字组成一对,就像评论中maloomeister已经提到的那样。
从你的问题中,我理解一对是指两个连续相等的数字。另外,一个数字可以与另外两个数字组成一对(在连续的相等数字超过两个的情况下),例如你的例子中的 8
。
你需要以某种方式记住先前的数字,以便在每次迭代中将其与当前数字进行比较。由于某个数字可以出现在两对中,可以推断出对于 k 个连续的数字,成对的数量是 k - 1。因此我们需要记录当前连续相等数字序列的长度,如果它归零(即遇到另一个数字时),则将其添加到总的成对数量中:
// 找到的总成对数量
int pairs = 0;
// 当前连续相等数字的数量(始终 >= 1)
int sequence = 1;
// 记录先前遇到的数字。由于数组的第一个元素无法与前一个元素进行比较
// (因为前面没有元素!),我们需要从第二个元素开始。
int last = ints[0];
// 假设数组的长度至少为1。不过,你应该添加代码来检查长度是否不为零。
for (int i = 1; i < ints.length; i++) {
// 检查当前数字是否与先前的数字匹配。如果是,那么我们的连续值序列递增
if (ints[i] == last) {
sequence++;
}
else {
// 如果当前元素与先前元素不相等,那么我们的连续相等数字序列已结束。
// 在这种情况下,将序列长度减一的值添加到总的成对数量中。
pairs += (sequence - 1);
// ...并重置序列长度
sequence = 1;
}
// 更新先前看到的数字
last = ints[i];
}
我在这里使用了一个数组,但是除了使用 ints[]
,你也可以要求用户输入一个整数。
英文:
You should not ask for two inputs at once, since a number can form a pair with two other numbers, like maloomeister already said in the comments.
From your question I assume a pair is two consecutive equal numbers. Also, a number can form a pair with two other numbers (in the case of more than two equal consecutive numbers), 8
in your example.
You have to somehow remember the previous number, in order to compare it with the current one, in each iteration. Since a certain number can occur in two pairs, it can be deduced that for k consecutive numbers, the number of pairs is k − 1. So we need to record the current length of the sequence of consecutive equal numbers, and if it resets to zero (i.e. when an other number occurs), then we add it to the total number of pairs:
// 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];
}
I used an array here, but instead of using ints[]
, you can also ask the user to input an int.
答案4
得分: 0
因为你显然只需要计算连续的数对。这意味着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 (k == previousK) {
// 更新输出字符串和计数
pairs += k + " & " + k + ", ";
count++;
}
// 使用此值更新前一个值
previousK = k;
}
// 去除末尾的逗号和空格
int len = pairs.length()-2;
System.out.println("Pairs: = " + count + " " + pairs.substring(0,len)+")");
输出结果为
Pairs: 4 (34 & 34, 27 & 27, 8 & 8, 8 & 8)
英文:
Since you apparently only need to count consecutive pairs. This means that 8,8,8 yields [8,8] and [8,8] since the first and third 8's are not consecutive. You can do it with a simple loop.
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)+")");
Prints
Pairs: 4 (34 & 34, 27 & 27, 8 & 8, 8 & 8)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论