c - long long int 在 main() 内部声明

标签 c variables int

Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.

What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.

Input

The input contains three positive integer numbers in the first line: n, m and a (1 ≤  n, m, a ≤ 10^9).

input given: 6 6 4

Output

Write the needed number of flagstones.

#include<stdio.h>
#include<math.h>
int main(){
    long long m, n, a;
    scanf("%lld %lld %lld", &m, &n, &a);
    long long req = ceil(m / a) * ceil(n / a);
    printf("%lld", req);    
}

在第一个测试用例中它应该给我 4 但它给了我 1。我看到一个解决方案,人们使用 int m, n, a 作为全局声明的变量,在 main() 之上,在这种情况下,如果 10^18 在中,他们如何存储值4 字节全局声明变量(假设最后一个测试用例非常大)。或者我们在全局声明变量时是否有不同的规则?其中一些作为 main(m, n, a) 传递而没有在任何地方声明。

最佳答案

对于“(1 ≤ n, m, a ≤ 10^9)”,不需要double 数学或函数。

因为 m/a整数除法 - 它会截断商的小数部分,使 ceil(m/a) 无关紧要- 简单地寻找余数,看看是否应该“四舍五入”。

// long long req = ceil(m / a) * ceil(n / a);
long long m_over_a = m/a + (m%a != 0);
long long n_over_a = n/a + (n%a != 0);
long long req = m_over_a * n_over_a;  

关于执行 m/am%a 的注意事项。在过去,编译器会执行 2 次昂贵的除法/余数计算。今天,期望一个好的编译器能够看到相邻的代码并一起执行单个 divide_and_remainder 计算是合理的。因此,一旦 m/a 完成,m%a 的成本通常是无关紧要的。

关于c - long long int 在 main() 内部声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52653825/

相关文章:

java - Java中的实例变量是通过引用传递的吗?

python - 有多少种方法可以改变变量的值?

c - C 中指针与整数相加

c - UDP 服务器不响应客户端

c - fread() 完成读取文件后是否会前进指针?

c - 使用递归反转c中的字符串

c - 整数数组标记结束的建议

c - 将共享库链接到可执行文件

php - 更新 sql php 变量

c - 定义为 double 和 int 的函数的输出