当前位置: 首页 > news >正文

类和对象相关题

文章目录

      • 1. 求1+2+3+...+n
      • 2. 计算是这一年的第几天
      • 3. 求两个日期之间的天数
      • 4. 算出第n天是几月几号
      • 5. 计算一个日期加上若干天后是什么日期


1. 求1+2+3+…+n

求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

数据范围: 0 < n <= 200

class func{
public:static int i;static int sum;func(){sum+=i;++i;}
};int func::i = 1;
int func::sum = 0;class Solution {
public:int Sum_Solution(int n) {func* p = new func[n];return func::sum;}
};

2. 计算是这一年的第几天

根据输入的日期,计算是这一年的第几天。

保证年份为4位数且日期合法。

#include <iostream>
using namespace std;class Date{
public:int year;int month;int day;Date(){};// 声明友元friend istream& operator>>(istream& in, Date& d);// 判断闰年bool isLeapYear(int year) const{return((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);}// 获得日期int Getday(int year, int month) const{static const int Getdays[] =  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if(month == 2 && isLeapYear(year))return 29;return Getdays[month - 1];}// 计算是这一年的第几天int my_sum(int year, int month, int day) const{int sum = 0;for(int i = 1;i < month; ++i){sum += Getday(year, i); }sum += day;return sum;}
};// >> 操作符重载
istream& operator>>(istream& in, Date& d){in >> d.year >> d.month >> d.day;return in;
}int main()
{Date d;while(cin >> d){cout << d.my_sum(d.year, d.month, d.day) << endl;}return 0;
}

3. 求两个日期之间的天数

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

#include <bits/stdc++.h>
using namespace std;
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int year){if((year%4==0 && year%100!=0) || year%400==0) return 1;return 0;
}int main(){int day1,day2,mon1,mon2,year1,year2;scanf("%4d%2d%2d",&year1,&mon1,&day1);scanf("%4d%2d%2d",&year2,&mon2,&day2);int sum1=0,sum2=0;for(int yy=0;yy<year1;yy++){if(leap(yy)) sum1+=366;else sum1+=365;} if(leap(year1)) day[2]=29;else day[2]=28;for(int mm=1;mm<mon1;mm++){sum1+=day[mm];}sum1+=day1;for(int yy=0;yy<year2;yy++){if(leap(yy)) sum2+=366;else sum2+=365;} if(leap(year2)) day[2]=29;else day[2]=28;for(int mm=1;mm<mon2;mm++){sum2+=day[mm];}sum2+=day2;cout<<abs(sum1-sum2)+1<<endl;return 0;
}

4. 算出第n天是几月几号

给出年分m和一年中的第n天,算出第n天是几月几号。

#include <iostream>
using namespace std;class Date {public:// 判断是否为闰年bool isLeapYear(int year) const {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return true;return false;}// 获取某年某月的天数int GetMonthDay(int year, int month) const {static const int GetMonthDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && isLeapYear(year))return 29;return GetMonthDay[month - 1];}// 构造函数Date(int year, int x) {this->year = year;if (x <= 31) {month = 1;day = x;} else {x -= 31;month = 2;for (int i = 2; x > GetMonthDay(year, i); ++i) {++month;x -= GetMonthDay(year, i);}day = x;}  }private:int year;int month;int day;friend ostream& operator<<(ostream& out, const Date& d);
};// << 运算符重载
ostream& operator<<(ostream& out, const Date& d) {if (d.month < 10 &&d.day < 10)   out << d.year << "-0" << d.month << "-0" << d.day << endl;else if (d.month < 10)   out << d.year << "-0" << d.month << "-" << d.day <<endl;else if (d.day < 10)   out << d.year << "-" << d.month << "-0" << d.day << endl;else   out << d.year << "-" << d.month << "-" << d.day << endl;return out;
}int main() {int year, x;while (cin >> year >> x){Date d(year, x);cout << d;}return 0;
}

5. 计算一个日期加上若干天后是什么日期

设计一个程序能计算一个日期加上若干天后是什么日期。

#include <iostream>
using namespace std;class Date {
public:// 判断是否为闰年bool isLeapYear(int year) const {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return true;return false;}// 获取某年某月的天数int GetMonthDay(int year, int month) const {static const int GetMonthDay[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && isLeapYear(year))return 29;return GetMonthDay[month - 1];}// 日期天数递增一天void incrementDate() {++day;if (day > GetMonthDay(year, month)) {day = 1;++month;if (month > 12) {month = 1;++year;}}}// 后置++运算符重载Date& operator++(){Date tmp = *this;incrementDate();return *this;}Date(int year, int month, int day): year(year), month(month), day(day) {}private:int year;int month;int day;friend ostream& operator<<(ostream& out, const Date& d);
};// << 运算符重载
ostream& operator<<(ostream& out, const Date& d) {if (d.month < 10 && d.day < 10)   out << d.year << "-0" << d.month << "-0" << d.day << endl;else if (d.month < 10)   out << d.year << "-0" << d.month << "-" << d.day <<endl;else if (d.day < 10)   out << d.year << "-" << d.month << "-0" << d.day << endl;else   out << d.year << "-" << d.month << "-" << d.day << endl;return out;
}int main() {int n;int y, m, d, x;cin >> n;for (int i = 0; i < n; ++i) {cin >> y >> m >> d >> x;Date d1(y, m, d);while(x--){++d1;}cout << d1;}return 0;
}
http://www.lryc.cn/news/478518.html

相关文章:

  • Word大珩助手:超大数字怎么读?35位数字?69位数字?
  • 阿里云k8s-master部署CNI网络插件遇到的问题
  • 【LwIP源码学习4】主线程tcpip_thread
  • 求猫用宠物空气净化器推荐,有没有吸毛强、噪音小的产品
  • pycharm中python控制台出现CommandNotFoundError: No command ‘conda run‘.
  • 架构师备考-架构基本概念
  • 信奥赛C++知识点
  • 高并发内存池扩展 -- 处理大内存,优化释放时需要传入空间大小,加入定长内存池,存放映射关系的容器的锁机制,优化性能(基数树,优势,优化前后对比)
  • Composite(组合)
  • 有Bootloader,为什么还要BROM?
  • 【MATLAB代码】CV和CA模型组成的IMM(滤波方式为UKF),可复制粘贴源代码
  • 【网络】传输层协议TCP(下)
  • 服务器数据恢复—EVA存储故障导致上层应用不可用的数据恢复案例
  • 支持向量机相关证明 解的稀疏性
  • 静态ip和动态ip适合什么场景
  • Istio Gateway发布服务
  • 前端vue3若依框架pnpm run dev启动报错
  • python线条爱心
  • GPU的内存是什么?
  • Linux - 弯路系列1:xshell能够连接上linux,但xftp连不上(子账号可以连接,但不能上传数据)
  • 数组逆序重存放
  • 归并排序:高效算法的深度解析
  • 微服务中常用分布式锁原理及执行流程
  • 声学气膜馆助力企业年会与研学活动完美呈现—轻空间
  • Halcon3D image_points_to_world_plane详解
  • A Consistent Dual-MRC Framework for Emotion-cause Pair Extraction——论文阅读笔记
  • 如何debug(Eclipse)
  • 【comfyui教程】ComfyUI有趣工作流推荐:快速换脸,创意随手掌握!
  • css-flex布局属性
  • 【鸿蒙】HarmonyOS NEXT应用开发快速入门教程之布局篇(下)