博客
关于我
算法笔记_192:历届试题 买不到的数目(Java)
阅读量:443 次
发布时间:2019-03-06

本文共 1162 字,大约阅读时间需要 3 分钟。

目录

 


1 问题描述

问题描述

小明开了一家糖果店。他别出心裁:把水果糖包成4颗一包和7颗一包的两种。糖果不能拆包卖。

小朋友来买糖的时候,他就用这两种包装来组合。当然有些糖果数目是无法组合出来的,比如要买 10 颗糖。

你可以用计算机测试一下,在这种包装情况下,最大不能买到的数量是17。大于17的任何数字都可以用4和7组合出来。

本题的要求就是在已知两个包装的数量时,求最大不能组合出的数字。

输入格式

两个正整数,表示每种包装中糖的颗数(都不多于1000)

输出格式

一个正整数,表示最大不能买到的糖数

样例输入1
4 7
样例输出1
17
样例输入2
3 5
样例输出2
7

 

 

 


2 解决方案

 

具体代码如下:

 

import java.util.Scanner;public class Main {    public static int n, m;        public void getResult() {        int result = n * m;        for(;result >= 1;result--) {            boolean judge = false;            for(int i = 0;i <= result / n;i++) {                for(int j = 0;j <= result / m;j++) {                    if(n * i + m * j == result) {                        judge = true;                        break;                    }                }                if(judge == true)                    break;            }            if(judge == false) {                System.out.println(result);                return;            }        }    }        public static void main(String[] args) {        Main test = new Main();        Scanner in = new Scanner(System.in);        n = in.nextInt();        m = in.nextInt();        test.getResult();    }}

 

转载地址:http://owtyz.baihongyu.com/

你可能感兴趣的文章
Nginx
查看>>
nginx + etcd 动态负载均衡实践(一)—— 组件介绍
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
nginx - thinkphp 如何实现url的rewrite
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx 301跳转
查看>>
nginx 403 forbidden
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>