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; } }
相关文章推荐
- C++学习笔记(刷题ing )Day4 航电OJ2011、2039
- c++学习笔记 2005-9
- c++ 学习笔记(高级linux编程) day2
- c++学习笔记(1)——环境搭建、经典教程推荐及 Visual Studio 2005/2008中如何编译和运行C++程序
- C/C++学习笔记1 - 深入了解scanf()/getchar(cin、cin.get()、cin.getline()、getline()、gets()、getchar()的区别 (OJ时候会用到)
- C++学习笔记(刷题ing ) 航电OJ(日常水题)
- C++(VC 2005)学习笔记
- <<SQL Server 2005 高级程序设计>> 学习笔记(4)
- C++ 学习笔记 <2> C++ IO
- 【菜鸟C++学习笔记】3.函数
- C++学习笔记(九) 异常机制
- c++学习笔记
- C++ STL--map映照容器学习笔记
- C++学习笔记(6)-异常处理
- Python学习笔记 Day2 字符串、变量及列表
- C_PlusPlus学习笔记 - 5_数组、指针和字符串 (C++语言程序设计【第三版】 郑莉等,清华大学出版社)
- C++ TEMPLATE 学习笔记
- C++学习笔记-多重继承&虚拟继承
- C++学习笔记4
- C++学习笔记(二)