Java for every 6 remove 1 [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm making a system.
What I want is, for every 6 items you have to buy 5 (so when the price is 5 each item, 6 items is not 30 but 25, same with 12, 18, 24 etc...)
How would I do that?
I thought it would be something like this:
if (amount % 6 == 0) {
}</code>
But that would get it one time if I'm correct.

The modulus operator won't work in this situation.
So for an efficient solution.
int numberOfItems = 17; //however many there are
int discount = numberOfItems / 6;
double priceToPay = price * (numOfItems - discount);
By having the discount as an int you won't get any rounding or decimal part after the division.

Using modulus will only give you the discount if you have 6, 12, etc. items. What about if you have 7 items? You won't get any discount (it is not divisible by 6)! So, it would be something like this:
int numOfItems = 6; //this will be different every time
//copy numOfItems because it will be modified
int temp = numOfItems;
double price = 5;
int discount = 0;
//While there are at least 6 items
while (temp >= 6) {
discount++; //discount one item
temp -= 6; //take away 6
}
//you have to pay for the number of items, but not the discounted items
double amountToPay = price * (numOfItems - discount);
This way, every time you take away 6, you don't have to pay for 1 item.

Related

Java formula to compute discount dynamically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 days ago.
Improve this question
I need to implement an operation in my java program.
So I have the item, the price of it is 6,00€, but if the user get three of these items I can get a discount of 3,00€. So
int quantity = 3;
double discount = 3.0;
int quantityItem = 3;
double priceItem = 6.0;
double totalPrice = priceItem * quantityItem;
if(quantityItem == quantity){
totalePrice = totalePrice - discount;
}
The previous code is ok. But if I get 5 items the correct totalePrice should be:
priceItem (6€) * quantity (5) = 30€ - 3.0€ (discount) = 27€.
If I get 6 items the correct totalPrice should be:
priceItem (6€) * quantity (6) = 36€ - 6.0€ (discount) = 30€.
How can I write the code to make this dynamically ?
To identify the number of "groups of 3" of something bought, you need to divide the number bought by 3. Specifically, you want to use integer division, which basically means "divide by 3, then ignore the decimal part". So
1 / 3 = 0
2 / 3 = 0
3 / 3 = 1
4 / 3 = 1
5 / 3 = 1
6 / 3 = 2
...
In Java, division between two integers performs integer division by default, so you can calculate the number of "groups of 3" like so.
int discountQuantity = quantity / quantityItem;
Then you apply the discount that many times.
double totalPrice = priceItem * quantityItem - discountQuantity * discount;

Find Maximum GCD sum without repetition in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have an array of even number of elements, I have to select n/2( n=array size), pairs and calculate their GCD such that the sum of their GCD is max, given once we use those elements from array, we cannot use them again.
Example1: `
Input : 8 24 12 16
Output : 20
Explanation: We select two pairs (8,16) and (12,24) as sum of their GCD is maximum.
If we choose some other pairs, say (8,12) and (24,16), sum of their GCD will be 4+4 =8.
Example 2:
Input : 12 10 36 25 36 16
Output: 45
Explanation: We select the following 3 pairs : (36,36), (10,25) and (12,16) as the sum of their GCD is
36+5+4 = 45.
Our Approach:
for i in range(0,n):
max = 0;
for j in range(i+1,n):
temp = gcd(a[i], a[j]) // standard func to find GCD
if(max<temp):
store i and j
store max gcd every time and finally make a[i] and a[j] =0 to mark the visited elements
Edited
Constraint: max number of elements = 20, a[i]< 10^9.
Can you suggest an algorithm to optimally satisfy the above testcases in the least time complexity?
Because my approach is failing on multiple testcases.
This is a comment but I am not allowed to post comment yet.
It is not good to solve this problem by looking for the largest gcd.
Take [8,9,24,36], the largest gcd is gcd(24,36) = 12, that will get you gcd(24,36) + gcd(8,9) = 12+1 =13.
However, the largest sum is given by gcd(8,24) + gcd(9,36) = 8+9 = 17.

Do while loop nested in for loop wont iterate [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to enter N which is a number of numbers for which I need to see if they can be divided by 3. Afterwards I need to display what % of numbers from the N I can divide. Numbers need to go from 15 to 62 and they need to loop until I enter the right value each time, but they don't. Instead, they just repeat the for loop regardless of my input. Here is the code:
System.out.println("Enter N number of numbers");
int N = TextIO.getlnInt();
int number;
int counterOfDivisible = 0;
for(int i = 0; i < N ; i++) {
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number<15 && number>62);
if(number%3==0)
counterOfDivisible++;
}
System.out.println("% of numbers from the N that can be divided by 3 is " + (counterOfDivisible*100.0)/N + "%");
number can never be <15 and >62 at the same time. Think about your condition.
Thank you all for the quick reply, first time here. Quite silly mistake on my behalf.
This seems to do the trick i was aiming for.
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number < 15 || number > 62);

changing values inside the array in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Good evening
i'm trying to solve a question which is :
You are given a int[] marks containing the grades you have received so far in a class. Each
grade is between 0 and 10, inclusive. Assuming that you will receive a 10 on all future
assignments, determine the minimum number of future assignments that are needed for you to
receive a final grade of 10. You will receive a final grade of 10 if your average grade is 9.5 or
higher.
Definition Class: AimToTen Method: need Parameters:
int[] Returns: int Method signature: int need(int[]
marks) (be sure your method is public) Examples 1)
{9, 10, 10, 9} Returns: 0 Your average is already 9.5, so no
future assignments are needed. 2) {8, 9} Returns:
4 In this case you need 4 more assignments. With each
completed assignment, your average could increase to 9, 9.25, 9.4
and 9.5, respectively
My attempt to solve is :
public int need(int[] marks) {
int i=0, sum = 0, avg = 0, k = 0, counter = 0, ToCompleteMarks[] = null;
for (i; i < marks.length; i++) {
sum = sum + marks[i];
ToCompleteMarks[i] = marks[i] + ToCompleteMarks[i];// To copy the array so when i add 10 later to the program the original array does not change > good ?
}
avg = sum / marks.length;
while (avg < 9.5)
ToCompleteMarks[i]; //I need to add the number 10 to the array then get back to calculate the avg . But no ideas how to do that ! .
counter++;
return counter;
}
if you could help me with that I would be really greatful
thanks
We can do the below mentioned steps:
1. Get the difference between the avg calculated and the desired average(9.5)
LEts say that the calculated average is 8.
Difference would be 9.5-8 = 1.5
Hence we can take the upper limit(ceiling value) of the difference using Math.ceil(difference). Here it would be 2. Thus we need to add two assignments to the array.

How to generate people(number) - normal probability distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My job is to generate and urban residents using normal probability distribution.
Number of cities, 3000, Population 15 000 000
(in JAVA)
int people = 15000000;
int[] arrayofcity = new int[3000]
for (int i = 0; i < arrayofcity.length; i++) {
//how to generate people to the towns of total
//nextGaussian()??
}
Thank you for your help
Try something like this.. Not very efficient as the distributions are averaged/cancelled for every 2 values.
int people = 15000000;
int[] arrayofcity = new int[3000];
int sharedSpread=people/3000; // sharedSpread= avg population
for (int i = 0; i < arrayofcity.length; i++) {
if(i%2!=0)
arrayOfCity[i] = sharedSpread + (sharedSpread-arrayOfCity[i-1]);
else
{
Random r = new Random();
int val = Random.nextInt();
arrayOfCity[i] = val%sharedSpread ;
}
}
PS: This is not the exact code, but this can show how to go about the problem.. You can change the frequency of distribution . Instead of 2 here, we can change it to 4 to get better random distributions...
A rough sketch of a method:
Use Random#nextGaussian() to create a value X between 0-1 for each city.
Calculate the sum of all these X values. Call it S.
For each city, take its value X and divide by S. Multiply this result by the total population (15 000 000). This is how many people live in this city.
Note that this would be tricky to implement in practice due to rounding. As such, the total population may be slightly above/below the desired 15 000 000, so some method will be needed to add/remove the extra people.
One (non efficient, but beautiful mathematically IMO) way to do it is:
For each person (repeat 15M times), choose with uniform distribution
his city.
It will give you a total sum of 15M people in all cities, and according to central limit theorem - you will get close to normal distributionin in each city, because each city is basically a sum of Bernoulli trials with p=1/3000, over 15M trials.

Categories