class Solution
{
public:
void push(int node) {
//将数据node入栈到stack1
stack1.push(node);
}
int pop() {
if(stack2.empty())
{
//将stack1的数据入栈到stack2再出栈
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
}
//获取栈顶元素并返回,stack2出栈
int node = stack2.top();
stack2.pop();
return node;
}
private:
stack<int> stack1;
stack<int> stack2;
};
面试题二:
用C语言编写一个函数,根据给定的年月日计算这一天是一年中的第几天(需要考虑闰年)例如:给定参数year=2020,month=3,day=1,返回61,要求代码简洁优雅。
#include <stdio.h>
#define N 12
int main()
{
int arr[N] = {31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,day;
printf("input data:\n");
scanf("%d%d%d",&year,&month,&day);
int i;
int days = 0;
for(i=0;i<month-1;i++)
{
//判断闰年
if((year % 4 == 0 && year %100 == 0) || year %400 ==0)
{
arr[1] = 29;
}
//计算前month月的天数
days = days + arr[i];
}
//添加当月天数
days = days + day;
printf("%d年%d月%d日 是一年中的第 %dday\n",year,month,day,days);
return 0;
}