- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to count columns for calendar with month and first day
Suppose we have two numbers m and d. Consider a calendar where week days are represented as columns and rows are current days. We want to know how many columns in the calendar should have given the month m and the weekday of the first date of that month d (Assuming that the year is not leap-year).
So, if the input is like m = 11; d = 6, then the output will be 5, because 1-st November is Saturday and 5 columns is enough.
Steps
To solve this, we will follow these steps −
Define an array a of size: 13 := { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } return (a[m] + d + 5) / 7
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int m, int d){ int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; return (a[m] + d + 5) / 7; } int main(){ int m = 11; int d = 6; cout << solve(m, d) << endl; }
Input
11, 6
Output
5
Advertisements