当前位置:网站首页>Overlay number
Overlay number
2022-07-20 08:08:00 【zoyation】
/**
* Cover numbers
*
* Given an integer interval [a,b] And integer interval [x,y], You can use it any number of times a,b Add integers between , How many can be made up [x,y] An integer in an interval ? Input
* a,b,x,y, among 1<= a < b <= 1000000000, 1 <= x < y <= 1000000000. Output :
* use [a,b] Add integers in any number of times , How many can you get [x,y] The whole number inside . for example a = 8, b = 10, x = 3 , y = 20 We can get
* [3..20] Integer between 8, 9, 10, 16 ( 8 + 8), 17(8 + 9), 18(9 + 9), 19(9 + 10), 20(10 +
* 10), So output 8. ask :2+3=5 1+4=5 Is this a 1 One is still 2 individual ? answer : count 1 Time Ask how many different numbers you can cover [x,y] If it is completely covered Namely y - x +
* 1.
*
* @author zoyation
*
*/
public class OverWriteNumber {
public static int howmany(int a, int b, int x, int y) {
/*
* Scanner sc = new Scanner(System.in); Get input abcd Input ,1<= a < b <=
* 1000000000,1 <= x < y <= 1000000000 while (a <= 0 || a >= 999999999
* || b <= a || b >= 1000000000 || x <= 0 || x >= 999999999 || y <= x ||
* y >= 1000000000) { if (a <= 0 || a >= 999999999) {
* System.out.print(" Please enter a Value ([1,999999999]):"); a = sc.nextInt(); if (a
* <= 0 || a >= 999999999) {
* System.out.println("a The value of is not in the interval [1,999999999] Inside , Please re-enter !"); } } else if (b <=
* a || b >= 1000000000) { System.out.print(" Please enter b Value ([a+1=" + (a + 1) +
* ",999999999]):"); b = sc.nextInt();
*
* if (b <= a || b >= 1000000000) { System.out.println("b The value of is not in the interval [a+1=" +
* (a + 1)+ ",999999999] Inside , Please re-enter !"); } } else if (x <= 0 || x >=
* 999999999) { System.out.print(" Please enter x Value ([1,999999999]):"); x =
* sc.nextInt();
*
* if (x <= 0 || x >= 999999999) {
* System.out.println("x The value of is not in the interval [1,999999999] Inside , Please re-enter !"); } } else if (y <=
* x || y >= 1000000000) { System.out.print(" Please enter y Value ([x+1=" + (x + 1) +
* ",999999999]):"); y = sc.nextInt();
*
* if (y <= x || y >= 1000000000) { System.out.println("y The value of is not in the interval [x+1=" +
* (x + 1)+ ",999999999] Inside , Please re-enter !"); } } } System.out.println("a=" + a +
* ",b=" + b + ",x=" + x + ",y=" + y);
*/
/* count Record how many different numbers can be covered */
int count = 0;
if (a > y) {
count = 0;
} else if (a == y) {
count = 1;
} else {
int i = a >= x ? a : x;// Record the beginning of traversal [x,y] value
if (b >= y) {
/* b Than y Big , Direct calculation */
count = y - i + 1;
} else {
int k = 0;/* The record starts in the interval [x,y] Calculate the minimum number of statistics through circulation */
if (b < x) { /* a<b<x */
count = 0;
k = x;
} else {
count = b - i + 1;
k = b + 1;
}
/* Next, traverse the statistical coverage figures */
int t1 = a;
int t2 = b;
do {
int m1 = k % t1;
if (m1 >= a || m1 == 0) {
/* Current statistics and t1 Seeking remainder , If 0 Or not a Small , In the interval [a,t1) Inside ,k=t1*n+m1(n=k/t1)*/
count++;
// System.out.println("m1="+m1+",t1="+t1+",k="+k);
t1 = a;
t2 = b;
k++;
} else {
int m2 = k % t2;
if (m2 >= a || m2 == 0) {
/* Current statistics and t1 Seeking remainder , If 0 Or not a Small , In the interval [a,t2) Inside ,k=t2*n+m2 (n=k/t2)*/
count++;
// System.out.println("m2="+m2+",t2="+t2+",k="+k);
t1 = a;
t2 = b;
k++;
} else {
t1++;
t2--;
/* t1 Greater than t2 when ,[a,b] The interval values have been taken , Restart the verification of the next number */
if (t1 > t2) {
t1 = a;
t2 = b;
k++;
}
}
}
} while (k <= y);
}
}
return count;
}
public static void main(String args[]) {
System.out.println(howmany(51, 52, 50, 999999999));// 57315234
}
}
边栏推荐
- Nodejs学习
- 序列化概念学习
- Rllib学习[2] --env定义 + env rollout
- Option classes shared by MFC and ATL
- CyclicBarrier
- CacheManager - written in C # Net
- An open source web drawing board is really convenient
- Mongodb亿级别数据操作
- 使用wireshark常用的过滤命令
- Next time, the interviewer will ask about the design of high concurrency system and directly dump this article to him
猜你喜欢
URL地址映射配置
The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"
【玩物立志-scratch少儿编程】骑上小摩托(动态背景+摄像头控制操作)
超超超超写实的数字人!让你24小时不停播
Kuzaobao: summary of Web3 encryption industry news on July 18
What are the main aspects of interface testing? What skills are needed? How to learn?
PC网站实现微信扫码登录功能(一)
firewall 端口转发
基于.NET动态编译技术实现任意代码执行
面试题总结(4) Tcp / Ip 四层模型,三次握手四次挥手,多一次少一次可以不,NIO的实现原理
随机推荐
小程序页面生成链接通过短信发送
idea svn主干合并分支版本Missing ranges异常Error:svn: E195016
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
Understand explicit and implicit in C #
C#中的Explicit和Implicit了解一下吧
C # use objects comparer to compare objects
JS 99 multiplication table
Use Wireshark's common filtering commands
PC网站实现微信扫码登录功能(二)
SQL SERVER 发送邮件失败 提示必须制定收件人
局域网访问项目注意事项
China Astragalus industry market forecast and investment strategy report (2022 Edition)
No overlapping interval [greedy practice]
Thymeleaf 视图集成
WinForm layout and control adaptive resolution and prevention of dislocation
覆盖数字
线性结构理解
CyclicBarrier
Redis地理算法GEO解析和应用
Jetpack compose比xml优秀的地方