如何从 int 数组向下舍入到最接近的整数?

发布于 2024-12-22 12:14:00 字数 196 浏览 0 评论 0原文

我正在用 Java 设计一个捐赠插件,用户可以从中捐赠自定义金额。通过捐赠,您可以从某些包裹中获得好处。

我想做的是将整数n向下舍入到最接近的捐赠包。

例如,可能有 3 个捐赠包,全部表示为整数。捐款包有 5 美元、10 美元和 20 美元。如果用户捐赠 13 美元,我希望插件将其四舍五入为 10 美元,因为这是最近的捐赠包。

I'm designing a donation plugin in Java, from which a user can donate a custom amount. From donating, there are benefits you receive from certain packages.

What I wish to do, is round down an integer n, to the nearest donation package.

For example, there might be 3 donation packages, all represented as integers. There are the $5, the $10, and the $20 donation packages. If a user donates $13, I want the plugin to round that down to $10, as that is the nearest donation package.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏别ゝ 2024-12-29 12:14:00

嗯,我今天有一些空闲时间。 (: 关心捐赠数组的顺序是递增的。否则只需先对其进行排序

public class Donation {
    private static int[] donArray = {10, 5, 30, 20};
    static{
        Arrays.sort(donArray);
    }

    public static void main(String[] args){
        int paid = 13;
        System.out.println("Applied Donation: " + applyDonation(paid));
    }

    private static int applyDonation(int paid) {
        int applied = 0;
        for(int range: donArray){
            if(range <= paid)
                applied = range;
            else
                break;
        }
        return applied;
    }
}

甚至更简单:

    TreeSet<Integer> donSet = new TreeSet<Integer>(Arrays.asList(new Integer[]{10, 5, 30, 20}));
    int paid = 13;
    System.out.println("Applied Donation: " + donSet.floor(paid));

Well, I have some free time today. (: Care for the donation array is increasing order.. else just sort it first

public class Donation {
    private static int[] donArray = {10, 5, 30, 20};
    static{
        Arrays.sort(donArray);
    }

    public static void main(String[] args){
        int paid = 13;
        System.out.println("Applied Donation: " + applyDonation(paid));
    }

    private static int applyDonation(int paid) {
        int applied = 0;
        for(int range: donArray){
            if(range <= paid)
                applied = range;
            else
                break;
        }
        return applied;
    }
}

or even simpler this:

    TreeSet<Integer> donSet = new TreeSet<Integer>(Arrays.asList(new Integer[]{10, 5, 30, 20}));
    int paid = 13;
    System.out.println("Applied Donation: " + donSet.floor(paid));
云淡月浅 2024-12-29 12:14:00

将所有值放入 TreeSet 中,然后使用

myTreeSet.headSet( donatedValue ).last();

Put all the values in a TreeSet<Integer>, then use

myTreeSet.headSet( donatedValue ).last();
友谊不毕业 2024-12-29 12:14:00

我不知道为什么不是这个?

if(donation >10 && <=15) {           //say you want to round 15 or less to 10
  donation=10;
 }

I'm not sure why not this?

if(donation >10 && <=15) {           //say you want to round 15 or less to 10
  donation=10;
 }
毁梦 2024-12-29 12:14:00

尝试将其除以 10,四舍五入到最接近的整数,然后再次乘以 10。

例如。

13/10=1.3 -> 1.3~1 -> 1*10 = 10$

16/10=1.6 -> 1.6~2 -> 2*10=20$

当然,这仅适用于 10 枚的捐赠包。

Try dividing it by 10, rounding it to the nearest integer and then multiply it again by 10.

eg.

13/10=1.3 -> 1.3~1 -> 1*10 = 10$

or

16/10=1.6 -> 1.6~2 -> 2*10=20$

Of course this will only work in donation packages of 10s.

披肩女神 2024-12-29 12:14:00

您可以对捐赠数组使用二分搜索,找到新捐赠值所在的索引,然后使用 if-else 轻松地从两个值中做出决定。
编辑:您需要事先对数组进行排序。

You can use binary search on donations array and find the indexes between which your new donation value lies and then decide from the two values easily using if-else.
Edit: you need to have array sorted before hand for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文