I - Increase and Copy(数学+贪心+公式)

链接: link.
写这一道题的时候我知道是用公式,但是忽略了高中学的一个数学函数(对勾函数) 给定三个数n,x,y,要求 xy>=n 这时当x=y的时候即x=sqrt(n)时,有几个细节
① 若x
xn 操作数ans=2x-2;
②若x
x<n 为了使前面两个数之积大于n, 引入变量t,使t=n/x(或者是n/x+1),这里是当n%x
0 ,t=n/x,否则t=n/x+1才能满足条件 操作数ans=x+t-2;
第三种情况x*x>n不予考虑,因为与第二种情况原理相同
代码

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t,n,x,tep,ans;
	cin>>t;
	while(t--){
		cin>>n;
		x=sqrt(n);
		if(x*x==n) ans=2*x-2;
		else{
			if(n%x==0) tep=n/x;
			else tep=n/x+1;
			ans=x+tep-2;
		}
		cout<<ans<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
use java language ,In this project you need to write a book lending system for a Library. The system has different roles for registered users. There are two types of user roles: borrower and lender. Write an IUser interface for library users, with the following UML specification: +----------------------------------+ | <<interface>> | | IUser | +----------------------------------+ | + getName(): String | | + getBook(): int | | + moreBook(int number): void | +----------------------------------+ and a User class that implements IUser and has the following UML specification: +-----------------------------------+ | User | +-----------------------------------+ | - name: String | | - book: int | +-----------------------------------+ | + User(String name, int book) | | + getName(): String | | + getBook(): int | | # setBook(int book): void | | + moreBook(int number): void | | + testUser(): void | +-----------------------------------+ The name instance variable indicates the user name. The book instance variable indicates the number of books borrowed by the user. The setBook method changes the number of books borrowed by the user. The setBook method is protected, not public. This means that only subclasses of the User class can use the setBook method. All the other classes in the system cannot use the setBook method, so they cannot change the number of books borrowed by a user. The purpose of the moreBook method is to increase the number of books borrowed or lent by the user (depending on what kind of user it is) by the number given as argument to the method. The moreBook method of the User class is abstract, since we do not know what kind of role the user is (a borrower borrows books from other users and a lender lend books to other users). Also add to your program a Test class to test your User class. public class Test { public static void main(String[] args) { User.testUser(); } }
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值