[Educoder homework] C&C++ control structure training
Logically speaking, after completing this course and learning a function and recursion, you can participate W o r l d F i n a l World\ Final World Final了…
T1 branch structure: is it a leap year?
It’s very simple, there are only two types of leap years, 400 400 400's multiple 4 4 4Multiple but not necessary 100 100 Multiple of 100.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int year;
// 请在此添加代码,判断输入的年份是否位闰年,是则输出"Yes",否则输出"No"
/********** Begin *********/
cin >> year;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) puts("Yes");
else puts("No");
/********** End **********/
return 0;
}
T2 branch structure: day of year
As for , we can handle it a little more cleverly. The general idea is to add up the number of days before this month and then add what day of the month it is now.
for 2 2 2 month processing, we can use a t t t to make the operation universal. For example, leap year t t t i.e. 1 1 1,No 则 t t t i.e. 0 0 0, so there is no need to judge again when adding.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
// y-年,m-月,d-日,n-第几天
int y, m, d, n;
// 请在此添加代码,计算并输出指定日期是第几天
/********** Begin *********/
cin >> y >> m >> d ;
int t = (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
n = 0;
for (int i = 1; i < m; i ++ ) {
if (i == 2) n += 28 + t;
else if (i == 4 || i == 6 || i == 9 || i == 11) n += 30;
else n += 31;
}
n += d;
/********** End **********/
printf("%d-%d-%d是第%d天\n",y,m,d,n);
return 0;
}
T3 branch structure: maximum number of rearrangements
Ahem, I used one s o r t sort sort, otherwise there are six judgments, a total of 3! 3! 3!Special circumstances.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// n-输入的数,m-重排后的数
int n, m;
// 请在此添加代码,输入一个小于1000的正整数,重排出最大的数存入m中
/********** Begin *********/
cin >> n ;
int a[3];
a[0] = n / 100;
a[1] = n / 10 - a[0] * 10;
a[2] = n % 10;
sort(a, a + 3);
m = a[2] * 100 + a[1] * 10 + a[0];
/********** End **********/
// 输出重排后的数
cout << m << endl;
return 0;
}
T4 Loop Structure: Black Hole Trap
No different from the previous question, just need to use w h i l e while while loop. Just pay attention to the exit conditions and counters.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
// 请在此添加代码,输出整数进入黑洞过程
/********** Begin *********/
cin >> n ;
int t = 1;
while (n != 495) {
int mdl = n, a[3];
a[0] = mdl / 100;
a[1] = mdl / 10 - a[0] * 10;
a[2] = mdl % 10;
sort(a, a + 3);
printf("%d:%d-%d=%d\n", t, a[2] * 100 + a[1] * 10 + a[0], a[0] * 100 + a[1] * 10 + a[2], (a[2] - a[0]) * 99);
n = (a[2] - a[0]) * 99;
t ++ ;
}
/********** End **********/
return 0;
}
T5 cyclic structure: is it a prime number?
Just use the most efficient method, starting from 2 2 2to n − 1 n-1 n−1, if there is one that can divide it, it is not a prime number.
Optimize it slightly, for example, just start from 2 2 2Judgment reached n \sqrt n n is enough, because if there is one greater than n \sqrt n ntarget number x x x满足 ∃ y \exist y ∃y使得 x y = n xy=n xy=n,nan么 y = n x y=\frac n x and=xnmust be less than n \sqrt n n, that is, it can be judged.
Of course, there are also quite fast algorithms M i l l a r − R a b i n Millar-Rabin Millar−Rabin. Simply put, it is the fastest known algorithm for determining prime numbers based on binary fast multiplication and Fermat's little theorem. But M R MR The MR algorithm is based on random numbers, so its constants may affect accuracy and are difficult for beginners to understand, but I will introduce it in detail.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int n;
// 请在此添加代码,输入正整数n,如果n是素数则输出“Yes”,否则输出“No”
/********** Begin *********/
cin >> n;
if (n == 1) {
puts("No");
return 0;
}
if (n == 2) {
puts("Yes");
return 0;
}
for (int i = 2; i < n; i ++ ) {
if (n % i == 0) {
puts("No");
return 0;
}
}
puts("Yes");
/********** End **********/
return 0;
}
T6 loop structure: sum of prime numbers
It's nothing more, it's just a cycle.
// 包含两种I/O库,可以使用任一种输入输出方式
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int n, k;
// 请在此添加代码,输入n和k,并输出n以内k个素数以及它们的和
/********** Begin *********/
cin >> n >> k;
int sum = 0;
for (int i = n; i; i -- ) {
bool flag = true;
for (int j = 2; j < i; j ++ ) if (i % j == 0) flag = false;
if (flag && i != 1) {
sum += i;
cout << i << ' ' ;
k -- ;
if (!k) {
cout << sum << endl ;
return 0;
}
}
}
cout << sum << endl ;
return 0;
/********** End **********/
return 0;
}