首页 \ 问答 \ 有效地计算闰日(Efficiently calculate leap days)

有效地计算闰日(Efficiently calculate leap days)

我使用以下函数计算两年间的闰天数:

static int CountLeapDays(int startYear, int endYear) 
{
    int Days = 0;

    while (true)
    {
        if ((startYear % 4 == 0 && startYear % 100 != 0) || startYear % 400 == 0)
            Days++;
        if (startYear + 1 == endYear) break;
        startYear++;
    }

    return Days;
}

有没有其他方法来写这个,所以它不需要循环?


Im using the following function to calculate the number of leap days between two years:

static int CountLeapDays(int startYear, int endYear) 
{
    int Days = 0;

    while (true)
    {
        if ((startYear % 4 == 0 && startYear % 100 != 0) || startYear % 400 == 0)
            Days++;
        if (startYear + 1 == endYear) break;
        startYear++;
    }

    return Days;
}

Is there another way to write this so it doesnt need to loop?


原文:https://stackoverflow.com/questions/14878356
更新时间:2023-02-21 06:02

最满意答案

这是一个错误:窄字符scanf格式字符串解析器无法将文字字符与负值匹配。 Microsoft Connect上有一个活动的错误: sscanf的问题 。 这将在Visual Studio 2015和Windows 10使用的Universal CRT的下一次更新中修复。


This is a bug: the narrow character scanf format string parser has trouble matching literal characters with negative values. There is an active bug for this on Microsoft Connect: Problems with sscanf. This will be fixed in the next update to the Universal CRT used by Visual Studio 2015 and Windows 10.

相关问答

更多
  • 这是一个堆栈跟踪 f(0) print 0 f(1) print 1 f(2) print 2 f(3) // 3 < 3 == false print 2 print 1 print 0 Here's a stack trace f(0) print 0 f(1) print 1 f(2) ...
  • 这是一个错误:窄字符scanf格式字符串解析器无法将文字字符与负值匹配。 Microsoft Connect上有一个活动的错误: sscanf的问题 。 这将在Visual Studio 2015和Windows 10使用的Universal CRT的下一次更新中修复。 This is a bug: the narrow character scanf format string parser has trouble matching literal characters with negative val ...
  • 这是一个逐行细分: def intF(n, d, l=40): 很明显。 n是数字, d是另一个数字(除数), l是小数点后打印的位数。 s=str(n*10**l / d) 这有点不寻常。 这不是依赖于浮点运算,而是将n乘以10 ** l ,即乘以1后跟l位数。 这样最终结果就不会有任何浮点错误 - 假设d总是一个整数。 (但当然任何剩余的数字都会被截断。另外,在Python 3中用/替换/来获得相同的行为。) 此时, s将是整数的字符串表示 - 再次假设d是整数 - 但它将与float(n) ...
  • int main(int argc, const char** argv) { 典型的函数入口点。 std::ifstream* myfile = NULL; if (argc == 2) { 确保有足够的参数从argv[1]获取文件名。 file = myfile = new std::ifstream(argv[1]); 动态分配文件输入流并尝试使用它来打开argv[1]指定的文件。 然后将此文件流分配给两个指针file和myfile 。 我承认没有注意到有两个指 ...
  • 使用C ++风格 #include #include #include #include #include std::vector> ParseFile(const std::string& filename) { std::vector> data; std::ifstream file(filename.c_ ...
  • 它是预准备语句的位置参数 请参阅: http : //php.net/manual/en/function.pg-prepare.php 你实际上并没有“删除”引号,你必须在执行pg_execute时将int数组的SQL数组而不是字符串值传递给参数 一个例子: // Assume that $values[] is an array containing the values you are interested in. $values = array(1, 4, 5, 8); // To select ...
  • 函数sscanf_s是Microsoft发明,旨在比标准sscanf函数“更安全”。 因此,如果使用"%s"或"%c" ,它需要两个参数:一个是写入输入的缓冲区,另一个是指定缓冲区可用大小的整数 ,这样一个长输入字符串不会导致缓冲区溢出。 不幸的是,对于函数的用户(比如你自己)来说,这并不那么明显,导致函数的错误使用(这会降低隐含的“安全性”)。 链接到Microsoft文档 ; 请参阅“备注”部分。 所以你在这里有什么是未定义的行为,因为sscanf_s试图从堆栈中读取一个你没有放入的整数。 你最终幸运的 ...
  • @ blazs的答案似乎是正确的。 如果这有助于你理解,那很好。 这是一个像我这样的视觉学习者的答案... 您提供的重现是: T(n) = c + 2T(n−1) 所以,在循环树的每个节点上,你都要做一个持续的工作。 考虑到你在每个节点上所做的工作是不变的,如果你能找到树中节点总数的上限,你就发现了复杂性! 根据你的递归,你可以有效地将大小为n的问题分解为两个大小为n - 1的问题 。 所以,在每个级别上,树实际上增长到前一级的两倍大小。 它是一个深度为n的完整二叉树。 2完整二叉树中的节点总数由简单公式( ...
  • 每次调用递归函数时,都会创建一个新的上下文并在最后一个上下文之上堆积 。 此上下文存储在堆栈中。 在第一次输入up_and_down()时(例如: up_and_down(1) ),您有以下背景: ----------- | LEVEL 1 | ----------- 上面的矩形表示调用up_and_down(1)内的上下文。 第二次递归调用此函数(即: up_and_down(2) ),上下文是: ----------- | LEVEL 2 | ----------- ----------- | LEV ...
  • 根据文档 , sscanf_s的%c转换说明符需要一个额外的参数来检查缓冲区大小: int result = sscanf_s(lineHeader,"%c %f %f %f", &b, rsize_t{1}, &vertex.x, &vertex.y, &vertex.z); // It's a single char ^^^^^^^^^^ According to the documentation, sscanf_s's %c conver ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。