牛客oj 习题2.5Hello Word for U&&习题2.6日期差值

这题难点在这句话:n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N }

理解为n2在3到N之间,k的值小于等于n2,等号最右边的值是k,那么n1=n3都等于k,也就都小于等于n2。

理解了这句话,这题就是送,画个图列出表达式就可以秒了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

int main(){
//	freopen("in.txt", "r", stdin);
	string str;
	while(cin >> str){
		int N = str.size();
		int i = 0;
		while(3*i+1 <= N) i++;
		i--;
		int n2 = N-2*i;
		for(int j = 0; j < i; j++){
			printf("%c", str[j]);
			for(int k = 0; k < n2-2; k++) printf(" ");
			printf("%c\n", str[N-j-1]);
		}
		for(int k = i; k < N-i; k++) printf("%c", str[k]);
		printf("\n");
	}
	return 0;
}

这题只有一句话,细心。(NumberOfYear类型错写成bool错了一次)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

struct Date {
	int year;
	int month;
	int day;
};

Date small, big;

int table[2][13] = {
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

bool IsLeapYear(int year){
	if((year%400 == 0) || (year%100 != 0 && year%4 == 0)) return true;
	return false;
}

int NumberOfYear(int year){
	if(IsLeapYear(year)) return 366;
	else return 365;
}

int main(){
//	freopen("in.txt", "r", stdin);
	int longtime1, longtime2;
	while(cin >> longtime1 >> longtime2){
		int smalltime = min(longtime1, longtime2);
		int bigtime = max(longtime1, longtime2);
		small.year = longtime1/10000;
		small.month = longtime1%10000/100;
		small.day = longtime1%100;
		big.year = longtime2/10000;
		big.month = longtime2%10000/100;
		big.day = longtime2%100;
		//前面都是输入 
		int numbig, numsmall;
		numbig = numsmall = 0;
		//大日期转化为天数 
		while(big.year > small.year){
			big.year--;
			numbig += NumberOfYear(big.year);
		}
		int row = IsLeapYear(big.year);
		for(int i = 0; i < big.month; i++){
			numbig += table[row][i];
		}
		numbig += big.day;
		//小日期转化为天数 
		row = IsLeapYear(small.year);
		for(int i = 0; i < small.month; i++){
			numsmall += table[row][i];
		}
		numsmall += small.day;
		printf("%d\n", numbig-numsmall+1);
	}
	return 0;
}
发布了411 篇原创文章 · 获赞 72 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/104349759