您的位置:首页 > 编程语言 > C语言/C++

C++学习笔记(刷题ing )Day2 航电OJ2005、2007

2019-07-02 21:45 253 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_42012437/article/details/94434564

问题1:航电OJ2005
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
代码:这里scanf在vs中编译不通过,要使用scanf_s。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int year, month, days;

while (scanf_s("%d/%d/%d", &year, &month, &days) != EOF)
{
int g[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int count = 0;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
g[2] ++;
}
for (int i = 0; i < month; i++)
{
count = count + g[i];
}
count = count + days;
cout << count << endl;
}
return 0;
}

问题2:航电OJ2007
Problem Description

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample Input
1 3
2 5
Sample Output
4 28
20 152

代码:

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
int m, n = 0;
while (scanf("%d %d", &m, &n)!=EOF)
{
int countPF=0, countLF = 0,temp = 0;
if (m > n) { temp = n; n = m; m = temp; }
if (m % 2 == 0)
{
for (int j=m; j <= n; j+=2)
{
countPF += j * j;
if (j != n)
{
countLF += (j + 1) *(j + 1) * (j + 1);
}

}
}

if (m % 2 != 0)
{
for (int j=m; j <= n; j += 2)
{
countLF += j * j * j;
if (j != n)
{
countPF += (j + 1) *(j + 1);
}

}
}
cout << countPF<<' '<< countLF << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: