首页 > 编程语言 >【每周例题】力扣 C++ 一年中的第几天

【每周例题】力扣 C++ 一年中的第几天

时间:2024-05-19 16:18:54浏览次数:24  
标签:第几天 int 31 30 C++ substr year date 例题

一年中的第几天

题目

一年中的第几天

 思路分析

1.substr函数分割字符串,stoi函数将字符串转为十进制

stoi函数介绍

substr函数介绍

2.判断是否为闰年,如果是闰年,则二月的天数+1

代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	string date;
	cin >> date;
	
	int year, month, day;
	year = stoi(date.substr(0, 4));
	month = stoi(date.substr(5, 2));
	day = stoi(date.substr(8, 2));
	//闰年
	if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
	{
		months[2]++;
	}

	int sum = 0;

	for (int i = 1; i < month; i++)
	{
		sum += months[i];
	}

	cout << sum + day;

	return 0;
}

 力扣代码

class Solution {
public:
    int dayOfYear(string date) 
    {
        int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        int year, month, day;
        year = stoi(date.substr(0, 4));
        month = stoi(date.substr(5, 2));
        day = stoi(date.substr(8, 2));
        //闰年
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        {
            months[2]++;
        }

        int sum = 0;

        for (int i = 1; i < month; i++)
        {
            sum += months[i];
        }

        return sum + day;
    }
};

  

标签:第几天,int,31,30,C++,substr,year,date,例题
From: https://www.cnblogs.com/hcrzhi/p/18200431

相关文章

  • 【每周例题】判断回文串
    判断回文串题目给你一个字符串 x ,如果 x 是一个回文字符串,返回 true ;否则,返回 false 。回文字符串是指正序(从左向右)和倒序(从右向左)读都是一样的。例如,aba 是回文,而 abc 不是。代码#include<bits/stdc++.h>#include<cstring>usingnamespacestd;intmain(){......
  • UE4 C++ 攀爬功能
    UE中的TEXT()UE中使用TEXT()包含字符串后,将字符串转换为宽字符,其将被处理为支持Unicode和跨平台兼容性,而普通类型的字符串为一个窄字符类型,可能在跨平台出现问题。最主要的问题是在FString的构造函数中是接受TCHAR的所以对于FStringFNameFTEXT的构造需要传入TEXT("xxxx")。......
  • 百度 Apollo 使用 bazel 编译 C++ Boost 依赖出现 undefined reference to `boost::pyth
    CSDN搬家失败,手动导出markdown后再导入博客园因为一些原因,楼主想在Apollo自动驾驶框架里使用Boost.python工具来用C++调用Python,从网上找了个例子想编译下试试。C++代码如下(boost.python/EmbeddingPython-PythonWiki):#include<boost/python.hpp>usingnamesp......
  • C/C++技巧
    1.三目运算符语法:表达式1?表达式2:表达式3。表达式1为真则执行表达式2,否则执行表达式3。相比if语句,三目运算符短小简洁,适当使用可以提高代码可读性。另外,如果三目运算符返回左值,可以继续赋值。举例#include<iostream>usingnamespacestd;intmain(){system("......
  • C++学习----make
    基本规则:touchmain.cadd.csub.cadd.hsub.h#新建以上文件main函数:intmain(void){return0;}Makefile文件:main:main.oadd.osub.ogcc-Wall-gmain.oadd.osub.o-omainmain.o:main.cgcc-Wall-g-cmain.c-omain.oadd.o:add.cadd.h......
  • C++学习----gcc
    gcc编译步骤 静态库使用步骤hello_fn.h#ifndef_HELLO_FN_H#define_HELLO_FN_Hvoidhello(constchar*name);#endifhello_fn.c#include<stdio.h>#include"hello_fn.h"voidhello(constchar*name){printf("hello%S!!!\n",name);......
  • Qt/C++音视频开发74-合并标签图形/生成yolo运算结果图形/文字和图形合并成一个/水印滤
    一、前言在使用yolo做人工智能运算后,运算结果除了一个方框,还可能需要增加文字显示在对应方框上,以便标记是何种物体,比如显示是人还是动物,或者还有可能追踪人员,显示该人员的姓名。这种应用场景非常普遍,而且非常有必要,可以非常直观的直接看到对应移动的物体是什么。当然也有个缺点,就......
  • algo c++ 常用接口
    接口网站cppreferencesetunorder_set//unorder_setunorder_set<T>u_set;//insertu_set.insert(Tt);//findandjudgeiteratorit=u_set.find(Tt);if(u_set.find(t)!=it.end()){}//删除u_set.erase(t);技巧如果想要通过一种数据类型种的值构建另一种......
  • C++读取配置文件
    1、读取=号的配置文件(或者:)的配置。#include<iostream>#include<fstream>#include<sstream>#include<map>#include<string>std::map<std::string,std::string>read_config(conststd::string&filename){std::map<std::st......
  • C++ 初始化列表(Initialization List)
    请注意以下继承体系中各class的constructors写法:1classCPoint2{3public:4CPoint(floatx=0.0)5:_x(x){}67floatx(){return_x;}8voidx(floatxval){_x=xval;}9protected:10float_x;11};1213classCPoint2d:......