-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update RedPacket.java #119
Open
shaaoteiman
wants to merge
2
commits into
crossoverJie:master
Choose a base branch
from
shaaoteiman:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,16 +40,16 @@ public class RedPacket { | |
* 最大的红包是平均值的 TIMES 倍,防止某一次分配红包较大 | ||
*/ | ||
private static final double TIMES = 2.1F; | ||
|
||
//变量定义 | ||
private static int STATUS = 1; | ||
private int recursiveCount = 0; | ||
|
||
public List<Integer> splitRedPacket(int money, int count) { | ||
public List<Integer> splitRedPacket(int money, int count) throws Exception { | ||
List<Integer> moneys = new LinkedList<>(); | ||
|
||
//金额检查,如果最大红包 * 个数 < 总金额;则需要调大最小红包 MAX_MONEY | ||
if (MAX_MONEY * count <= money) { | ||
System.err.println("请调大最小红包金额 MAX_MONEY=[" + MAX_MONEY + "]"); | ||
return moneys ; | ||
//金额检查,如果最大红包 * 个数 < 总金额或最小红包 * 个数 > 总金额;则需要调整红包总金额 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里没必要抛异常了,像以前直接打印也没问题。不然所有的单测都需要改。 |
||
if (MAX_MONEY * count < money || MIN_MONEY * count > money) { | ||
throw new Exception(); | ||
} | ||
|
||
|
||
|
@@ -82,12 +82,21 @@ private int randomRedPacket(int totalMoney, int minMoney, int maxMoney, int coun | |
maxMoney = maxMoney > totalMoney ? totalMoney : maxMoney; | ||
|
||
//在 minMoney到maxMoney 生成一个随机红包 | ||
int redPacket = (int) (Math.random() * (maxMoney - minMoney) + minMoney); | ||
int redPacket = 0; | ||
double temp = (Math.random() * (maxMoney - minMoney) + minMoney); | ||
if(STATUS == LESS){ | ||
redPacket = (int) Math.floor(temp); | ||
}else if(STATUS == MORE){ | ||
redPacket = (int) Math.ceil(temp); | ||
}else{ | ||
redPacket = (int) temp; | ||
} | ||
|
||
int lastMoney = totalMoney - redPacket; | ||
|
||
int status = checkMoney(lastMoney, count - 1); | ||
|
||
STATUS = status; | ||
|
||
//正常金额 | ||
if (OK == status) { | ||
return redPacket; | ||
|
@@ -132,14 +141,19 @@ private int checkMoney(int lastMoney, int count) { | |
|
||
public static void main(String[] args) { | ||
RedPacket redPacket = new RedPacket(); | ||
List<Integer> redPackets = redPacket.splitRedPacket(20000, 100); | ||
System.out.println(redPackets); | ||
|
||
int sum = 0; | ||
for (Integer red : redPackets) { | ||
sum += red; | ||
} | ||
System.out.println(sum); | ||
List<Integer> redPackets = null; | ||
try { | ||
redPackets = redPacket.splitRedPacket(20000, 100); | ||
System.out.println(redPackets); | ||
|
||
int sum = 0; | ||
for (Integer red : redPackets) { | ||
sum += red; | ||
} | ||
System.out.println(sum); | ||
} catch (Exception e) { | ||
System.err.println("请调整红包总金额 !"); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请将注释换为
Javadoc
的形式。