英文:
The output is not getting convert into into long data type
问题
问题来自:https://www.hackerrank.com/challenges/mini-max-sum/problem
问题描述:给定五个正整数,找出通过对这五个整数中的任意四个进行求和可以得出的最小值和最大值。然后将相应的最小值和最大值打印为一行,两个以空格分隔的长整型数。
问题:对于一些测试案例,我得到了负数的输出,我理解这是因为在 "int" 大小中存在存储限制,这就是我得到负值的原因。我不明白我在哪里犯了错误。
我的解决方案:
static void miniMaxSum(int[] arr) {
int n = arr.length;
long[] ar = new long[n];
ar[0] = arr[1] + arr[2] + arr[3] + arr[4];
ar[1] = arr[0] + arr[2] + arr[3] + arr[4];
ar[2] = arr[0] + arr[1] + arr[3] + arr[4];
ar[3] = arr[0] + arr[1] + arr[2] + arr[4];
ar[4] = arr[0] + arr[1] + arr[2] + arr[3];
long[] a = new long[n];
for(int i=0; i<n; i++){
a[i]=ar[i];
}
Arrays.sort(a);
long max =a[4];
long min =a[0];
System.out.print(min+" "+max);
}
HackerRank 提供的预写代码:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the miniMaxSum function below.
static void miniMaxSum(int[] arr) {
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = new int[5];
String[] arrItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < 5; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
miniMaxSum(arr);
scanner.close();
}
}
英文:
The problem is from : https://www.hackerrank.com/challenges/mini-max-sum/problem
Problem Description : Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Issue : To some of the test cases I am getting the output -ve, which I understood its because of there is limit to the storing in "int" size, that's why I am getting the -ve values.
I don't understand where I am making the mistake.
My solution :
static void miniMaxSum(int[] arr) {
int n = arr.length;
long[] ar = new long[n];
ar[0] = arr[1] + arr[2] + arr[3] + arr[4];
ar[1] = arr[0] + arr[2] + arr[3] + arr[4];
ar[2] = arr[0] + arr[1] + arr[3] + arr[4];
ar[3] = arr[0] + arr[1] + arr[2] + arr[4];
ar[4] = arr[0] + arr[1] + arr[2] + arr[3];
long[] a = new long[n];
for(int i=0; i<n; i++){
a[i]=ar[i];
}
Arrays.sort(a);
long max =a[4];
long min =a[0];
System.out.print(min+" "+max);
}
Pre written code provided by the HackerRank :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the miniMaxSum function below.
static void miniMaxSum(int[] arr) {
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = new int[5];
String[] arrItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < 5; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
miniMaxSum(arr);
scanner.close();
}
}
答案1
得分: 1
首先,在解决问题时,您应编写通用代码,以便它可以执行问题的各种测试案例。例如,如果数组大小更大会怎么样?
我还注意到,在某些答案中,他们首先对数组进行了排序,这会使代码变慢。在这种情况下,您根本不需要排序。
以下是解决方案,以 O(n) 的时间复杂度解决了该问题并且更快:
static void minMaxSum(int[] arr) {
int min = arr[0];
int max = arr[0];
long totalSum = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
} else if (max < arr[i]) {
max = arr[i];
}
totalSum += arr[i];
}
long minSum = totalSum - max;
long maxSum = totalSum - min;
System.out.println(minSum + " " + maxSum);
}
现在回到您最初的问题,为什么 int 数据没有转换为 long。
这是因为在诸如 C、C++、Java 或 C# 等语言中,当你将多个整数相加时,计算的结果始终是一个整数,因为所有操作数都是整数。由于结果是整数,它计算了一个负整数值,然后将其分配给 long。因此,long 没有接收到原始值。
要将加法解释为 long,您应在四个操作数中的一个上执行显式的类型转换。
可以看到,int + int => int
但是,long + int => long
因此,您应编写以下代码(将第一个操作数转换为 long):
ar[0] = (long)arr[1] + arr[2] + arr[3] + arr[4]
这将得到正确的结果。
英文:
First of all you should write a generic code while solving a problem so that it can perform all kinds of test cases for the problem. Eg. What if array size is more?
And I also saw, in some answers to your question, they have first sorted the array, making the code slow. You never need sorting in this case.
Here is the solution, which solves the problem in O(n) and is faster
static void minMaxSum(int[] arr) {
int min = arr[0];
int max = arr[0];
long totalSum = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
} else if (max < arr[i]) {
max = arr[i];
}
totalSum += arr[i];
}
long minSum = totalSum - max;
long maxSum = totalSum - min;
System.out.println(minSum + " " + maxSum);
}
Now coming to your original question that why the int data is not converting into long.
This is happening because when you add multiple integers in languages like C, C++, Java or C#, the result calculated will always be an integer, since all the operands were integers. And since the result is an integer, it calculated a -ve integer value, then assigned it long. So long did not receive the original value.
To interpret the addition as long, you should perform explicit type casting on one of four operands.
See, int + int => int
But, long + int => long
So you should write, (casting first operand to long)
ar[0] = (long)arr[1] + arr[2] + arr[3] + arr[4]
And this will give correct result.
答案2
得分: 0
总是以通用的方式编写代码,不要硬编码所有内容(固定的5个元素)。
使用以下解决方案,它将通过您的所有测试用例:
static void miniMaxSum(int[] arr) {
int n = arr.length;
Arrays.sort(arr);
long min = 0;
long max = 0;
for (int i = 0; i <= n - 2; i++) {
min += arr[i];
}
for (int i = 1; i <= n - 1; i++) {
max += arr[i];
}
System.out.println(min + " " + max);
}
英文:
Always write code in generic way, You hard-coded everything(fixed 5 elements).
Use the below solution, It will pass all your test cases:
static void miniMaxSum(int[] arr) {
int n =arr.length;
Arrays.sort(arr);
long min = 0;
long max = 0;
for(int i = 0; i <= n - 2; i++){
min += arr[i];
}
for (int i = 1; i <= n - 1; i++){
max += arr[i];
}
System.out.println(min + " " + max);
}
答案3
得分: 0
在输出方面没有问题,它已经是long
类型。然而,你的逻辑毫无意义,你创建了5个数组ar
,每个数组的总和都不同。
for
循环将ar[4]
存储在a[i]
中。排序仅针对单个元素执行,而max
和min
被赋予了任意值。
你只需要将数组按升序排序,找到前4个元素和最后4个元素的和并显示出来。
static void miniMaxSum(int[] arr)
{
long sum1 = 0, sum2 = 0;
Arrays.sort(arr); //升序排序
for (int i = 0; i < 4; i++)
sum1 += arr[i]; //前4个元素的和
for (int j = arr.length - 1; j > 0; j--)
sum2 += arr[j]; //后4个元素的和
System.out.println(sum1 + " " + sum2);
}
英文:
There's no problem in output, it is already in long
. However your logic makes no sense you've created 5 arrays ar
each having different sum.
The for
loop stores ar[4]
in a[i]
. Sorting is done on a single element and max
and min
have arbitrary values assigned.
You just need to sort the array in ascending order, find the sum of first and last 4 elements and display them.
static void miniMaxSum(int[] arr)
{
long sum1 = 0, sum2 = 0;
Arrays.sort(arr); //Ascending Sort
for (int i = 0; i < 4; i++)
sum1 += arr[i]; //First 4 sum
for (int j = arr.length - 1; j > 0; j--)
sum2 += arr[j]; //Last 4 sum
System.out.println(sum1 + " " + sum2);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论