闰年计算 - 家庭作业 [英] Leap Year Calculation - Homework
本文介绍了闰年计算 - 家庭作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
using namespace std;
int main() {
int what_year;
cout << "Enter calendar year ";
cin >> what_year;
if (what_year - (n * 4) = 0 ) {
cout << "leap year";
}
else
{
cout << "wont work";
}
system("Pause");
return 0;
}
尝试为课程创建程序,找到闰年..
Trying to make a program for class, to find a leap year.. not sure how to ask C++ if an integer is divisible by a number?
推荐答案
闰年规则是
if year modulo 400 is 0 then
is_leap_year
else if year modulo 100 is 0 then
not_leap_year
else if year modulo 4 is 0 then
is_leap_year
else
not_leap_year
http://en.wikipedia.org/wiki/Leap_year#Algorithm
您可以使用取模运算符查看一个数字是否可以被另一个数字均分,
You can use the modulo operator to see if one number is evenly divisible by another, that is if there is no remainder from the division.
2000%400 = 0 //可以被整除除
2000 % 400 = 0 // Evenly divisible by 400
2001%400 = 1 //不能被400整除
2001 % 400 = 1 // Not evenly divisible by 400
有趣的是,没有应用400部分,这导致2000年2月29日不存在这些系统。
Interestingly, several prominent software implementations did not apply the "400" part, which caused February 29, 2000 not to exist for those systems.
这篇关于闰年计算 - 家庭作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文