请求优化代码
查看原帖
请求优化代码
1378709
Yue_Hao楼主2025/1/28 19:42

这个代码时间有73ms,请问还能再优化吗?

#include <stdio.h>
#include <math.h>
#define ll unsigned long long
inline ll read(){
	ll x = 0, f = 1;
	char ch;
	ch = getchar();
	while(ch <= '0' || ch > '9'){
		if(ch == '-'){
			f = -1;
			ch = getchar();
		}
	}
	while(ch >= '0' && ch <= '9'){
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	return (x * f);
}
ll max(ll a, ll b){
	if(a > b) return a;
	else return b;
}
int main(){
	bool f = false;
	ll w, h, ans = 2;
	w = read(), h = read();
	ll s = w * h;
	if(w < 2 || h < 2){
		printf("0");
		return 0;
	}
	for( ;ans <= w && ans <= h; ans++){
		double op1 = w * 1.0 / ans, op2 = h * 1.0 / ans;
		if( (ceil(op1) == w / ans) && (ceil(op2) == h / ans) ){
			f = true;
			break;
		}
	}
	if( f ){
		ll answ = (w / ans) * (h / ans);
		printf("%lld", answ);
	}else{
		printf("0");
	}
	return 0;
}
2025/1/28 19:42
加载中...