算法笔记的学习心得和知识总结(一)|CodeUp and Pat篇(算法笔记第二章)
2023-09-14 09:15:35 时间
目录结构
算法笔记CodeUp Pat汇总
![](https://img-blog.csdnimg.cn/20200813154411162.png#pic_center)
文章快速说明索引
学习目标:
因为最近一来要准备一下浙大PAT考试,二来复习一下将近遗忘的算法知识。正好把当年快翻烂掉的《算法笔记》再学一遍,权做C/C++和DSA的复习之用!注:因为进入工作岗位之后,基础知识的使用和遗忘会变得十分常见!面向过程 面向对象逐渐变成了面向百度编程,可是对于参加算法或编程竞赛而言 Baidu恐怕也帮不上忙了!古人云:温故而知新,可以为师矣!
1、CodeUp链接 ,点击前往
2、浙大PAT链接,点击前往
3、Tsinghua Online Judge,点击前往
本文有详细的实例(《算法笔记》书中的全部例题,都是可以直接通过评测系统的),如有疑问或者其他想法的小伙伴请在下面留言 Thanks♪(・ω・)ノ!
注:这些例题源码答案主要着重于通过 评测系统 (AC),故而仍有很多优化之处 小伙伴自行斟酌。这些也仅仅是起到一个抛砖引玉,相互借鉴的作用!
其顺序为书中的例题顺序,格式为:习题链接和题目源码
学习内容:(详见目录)
1、算法笔记 胡凡著
学习时间:
2020年11月4日01:53:07-2020年11月21日20:39:43
学习产出:
1、《算法笔记》复习
2、CSDN 技术博客 1篇
一、CodeUp 566
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
#if A
cout << "This is my first c program!" << endl;
#endif
#if B
cout << "********************" << endl;
cout << "Very Good!" << endl;
cout << "********************" << endl;
#endif
#if C
int a = 123, b = 456, sum;
sum = a + b;
cout << "sum=" << sum << endl;
#endif
#if D
int a, b;
cin >> a;
cin >> b;
cout << a + b << endl;
#endif
#if E
double a, b, c, tem;
scanf_s("%lf%lf%lf", &a, &b, &c);
if (a != 0 && (b * b - 4 * a * c) > 0)
{
tem = sqrt(b * b - 4 * a * c);
printf("r1=%7.2f\n", (-1 * b + tem) / (2 * a));
printf("r2=%7.2f", (-1 * b - tem) / (2 * a));
}
#endif
char mystr[8] = { 0 };
scanf("%s", mystr);
printf("%s\n", mystr);
return 0;
}
/**
*备用注释:
*
*
*
*/
注:上面由#if #endif
分割的是对应每道题的源码答案,都是通过CodeUp
和PAT
评测系统的!
二、CodeUp 567
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
#if C2
bool mycompare(double a, double b)
{
return a < b;
}
#endif
#if D
bool mycompare2(int a, int b)
{
return a > b;
}
#endif
#if E
#define shiwan 100000
#define yiwan 10000
#endif
int main()
{
#if A
double a, b, c, tem;
scanf("%lf%lf%lf", &a, &b, &c);
if (a != 0 && (b * b - 4 * a * c) > 0)
{
tem = sqrt(b * b - 4 * a * c);
printf("r1=%7.2f\n", (-1 * b + tem) / (2 * a));
printf("r2=%7.2f", (-1 * b - tem) / (2 * a));
}
else
{
printf("No real roots!\n");
}
#endif
#if B
double m, n;
scanf("%lf%lf", &m, &n);
printf("%.2f %.2f\n", n, m);
#endif
#if C1
double m, n, p, min, mid, max;
scanf("%lf%lf%lf", &m, &n, &p);
if (m < n)
{
if (n < p)
{
min = m;
mid = n;
max = p;
}
else if (m < p)
{
min = m;
mid = p;
max = n;
}
else
{
min = p;
mid = m;
max = n;
}
}
else
{
if (m < p)
{
min = n;
mid = m;
max = p;
}
else if (n < p)
{
min = n;
mid = p;
max = m;
}
else
{
min = p;
mid = n;
max = m;
}
}
printf("%.2f %.2f %.2f\n", min, mid, max);
#endif
#if C2
double myarray[3];
scanf("%lf%lf%lf", &myarray[0], &myarray[1], &myarray[2]);
sort(myarray, myarray + 3, mycompare);
for (int i = 0; i < 3; ++i)
{
if (i < 2)
printf("%.2f ", myarray[i]);
else
printf("%.2f", myarray[i]);
}
printf("\n");
#endif
#if D
int myarray[3];
scanf("%d%d%d", &myarray[0], &myarray[1], &myarray[2]);
sort(myarray, myarray + 3, mycompare2);
printf("%d\n", myarray[0]);
#endif
#if E
double profit, award = 0;
scanf("%lf", &profit);
if (profit > 0)
{
switch ((int)(profit / shiwan))
{
case 0:
award = profit * 0.1;
break;
case 1:
case 2:
award = yiwan + (profit - shiwan) * 0.75;
break;
case 3:
case 4:
award = 1.75 * yiwan + (profit - 2 * shiwan) * 0.05;
break;
case 5:
case 6:
award = 2.75 * yiwan + (profit - 4 * shiwan) * 0.03;
break;
case 7:
case 8:
case 9:
case 10:
award = 3.35 * yiwan + (profit - 6 * shiwan) * 0.015;
break;
default:
award = 3.95 * yiwan + (profit - 10 * shiwan) * 0.01;
}
}
printf("%.2f\n", award);
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
三、CodeUp 568
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int myFibonacci(int n)
{
int first = 1, second = 1, sum = 1;
if (n == 1 || n == 2)
return 1;
n -= 1;
while (--n)
{
sum = first + second;
first = second;
second = sum;
}
return sum;
}
#if F
#define ROW 4
#define COL 5
#endif
int main()
{
#if A
int n = 100, sum = 0;
while (n)
{
sum += n--;
}
printf("%d\n", sum);
#endif
#if B
int n = 100, sum = 0;
do
{
sum += n--;
} while (n);
printf("%d\n", sum);
#endif
#if C
int n = 100, sum = 0;
for (; n > 0; --n)
{
sum += n;
}
printf("%d\n", sum);
#endif
#if D
int N, sum = 0;
scanf_s("%d", &N);
for (int i = 0;;)
{
if (i == N)
break;
sum += ++i;
}
printf("%d\n", sum);
#endif
#if E
int N, sum = 0;
for (int i = 0;;)
{
if (sum > 1000)
break;
sum += ++i;
N = i;
}
printf("%d\n", N);
#endif
#if F
for (int i = 1; i <= ROW; ++i)
{
for (int j = 1; j <= COL; ++j)
{
printf("%3d", i * j);
}
printf("\n");
}
#endif
#if G
int flag = 1;
double sum = 0, n = 1;
while (1 / n >= 1e-6)
{
sum += (1 / n) * flag;
n += 2;
flag *= -1;
}
printf("PI=%.8f\n", 4 * sum);
#endif
#if H
int first = 1, second = 1, sum = 1, n;
scanf_s("%d", &n);
if (n == 1 || n == 2)
return 1;
if (n <= 50)
{
n -= 1;
while (--n)
{
sum = first + second;
first = second;
second = sum;
}
}
printf("%d\n", sum);
#endif
int n = 20, i = 2, up, down;
double sum = 0;
while (n--)
{
up = myFibonacci(i + 1);
down = myFibonacci(i);
sum += (double)up / (double)down;
i++;
}
printf("%.6f\n", sum);
return 0;
}
/**
*备用注释:
*
*
*
*/
四、CodeUp 569
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
#if A1
int myarray1[10] = { 0 }, last = 0;
for (int i = 0; i < 9; ++i)
{
scanf("%d", &myarray1[i]);
}
scanf("%d", &last);
int i = 0, len = 10;
for (; i < 9; ++i)
{
if (last < myarray1[i])
{
break;
}
}
for (int j = 9 - i; j != 0; --j)
{
myarray1[len - 1] = myarray1[len - 2];
len--;
}
myarray1[i] = last;
for (int j = 0; j < 10; ++j)
{
printf("%d\n", myarray1[j]);
}
#endif
#if A2
int myarray1[10] = { 0 }, last = 0;
for (int i = 0; i < 9; ++i)
{
scanf("%d", &myarray1[i]);
}
scanf("%d", &myarray1[9]);
sort(myarray1, myarray1 + 10);
for (int j = 0; j < 10; ++j)
{
printf("%d\n", myarray1[j]);
}
#endif
#if B
int myarray1[10] = { 0 };
for (int i = 0; i < 10; ++i)
{
scanf("%d", &myarray1[i]);
}
for (int j = 9; j >= 0; --j)
{
printf("%d\n", myarray1[j]);
}
#endif
#if C
int n;
scanf("%d", &n);
int** myarray = (int**)malloc(sizeof(int*) * n);
for (int i = 0; i < n; ++i)
{
myarray[i] = (int*)malloc(sizeof(int) * n);
for (int j = 0; j < n; ++j)
myarray[i][j] = 0;
}
if (1 == n)
{
printf("1\n");
}
else if (2 == n)
{
printf("1\n");
printf("1 1\n");
}
else
{
myarray[0][0] = 1;
myarray[1][0] = 1;
myarray[1][1] = 1;
for (int i = 3; i <= n; ++i)
{
myarray[i - 1][0] = 1;
myarray[i - 1][i - 1] = 1;
for (int j = 1; j <= i-1; ++j)
{
myarray[i - 1][j] = myarray[i - 2][j - 1] + myarray[i - 2][j];
}
}
}
for (int i = 0; i < n; ++i)
{
int count = 1;
for (int j = 0; j < n; ++j)
{
if (myarray[i][j] == 0)
{
break;
}
if (count == i + 1)
{
printf("%d", myarray[i][j]);
}
else
{
printf("%d ", myarray[i][j]);
count++;
}
}
printf("\n");
}
for (int i = 0; i < n; ++i)
{
free(myarray[i]);
}
#endif
#if D
char str1[512];
char* str = nullptr;
scanf("%s", str1);
str = str1;
while (*str != '\0')
{
if (*str >= 'A' && *str <= 'Z')
{
*str = ('Z' - (*str - 'A'));
}
else if (*str >= 'a' && *str <= 'z')
{
*str = ('z' - (*str - 'a'));
}
str++;
}
printf("%s\n", str1);
#endif
#if E
char strm[512] = { 0 };
char strn[512] = { 0 };
char* str1 = nullptr;
char* str2 = nullptr;
gets_s(strm);
gets_s(strn);
int result = 0;
str1 = strm, str2 = strn;
while (*str1 != '\0' && *str2 != '\0')
{
if (*str1 != * str2)
{
result = *str1 - *str2;
printf("%d\n", result);
break;
}
str1++;
str2++;
}
#endif
#if F
int myarray[10] = { 0 };
for (int i = 0; i < 10; ++i)
{
scanf("%d", myarray + i);
}
for (int j = 9; j >= 0; --j)
{
printf("%d\n", myarray[j]);
}
#endif
#if G
int myarray[20] = { 0 };
myarray[0] = myarray[1] = 1;
for (int i = 2; i < 20; ++i)
{
myarray[i] = myarray[i - 1] + myarray[i - 2];
}
for (int i = 0; i < 20; ++i)
{
printf("%d\n", myarray[i]);
}
#endif
#if H
int myarray[10] = { 0 };
bool flag = false;
for (int i = 0; i < 10; ++i)
{
scanf("%d", myarray + i);
}
for (int i = 0; i < 9; ++i)
{
flag = true;
for (int j = 0; j < 9 - i; ++j)
{
if (myarray[j] > myarray[j + 1])
{
swap(myarray[j], myarray[j + 1]);
flag = false;
}
}
if (flag)
break;
}
for (int i = 0; i < 10; ++i)
printf("%d\n", myarray[i]);
#endif
#if I
int myarray1[2][3];
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
scanf("%d", &myarray1[i][j]);
}
}
for (int i = 0; i < 3; ++i)
{
int j = 0;
for (; j < 1; ++j)
{
printf("%d ", myarray1[j][i]);
}
printf("%d\n", myarray1[j][i]);
}
#endif
#if J
char strm[512] = { 0 };
char strn[512] = { 0 };
char stro[512] = { 0 };
gets_s(strm);
gets_s(strn);
gets_s(stro);
if (strcmp(strm, strn) < 0 && strcmp(strn, stro) < 0)
{
printf("%s\n", stro);
}
if (strcmp(strn, strm) < 0 && strcmp(stro, strm) < 0)
{
printf("%s\n", strm);
}
if (strcmp(strm, strn) < 0 && strcmp(stro, strn) < 0)
{
printf("%s\n", strn);
}
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
五、CodeUp 570
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
#if A
void myreverse(char* str)
{
int len = strlen(str);
for (int i = 0; i < len / 2; ++i)
{
swap(str[i], str[len - i - 1]);
}
}
#endif
void vowels(char* s1, char* s2)
{
int len = strlen(s1);
for (int i = 0; i < len; ++i)
{
if (*(s1 + i) == 'A' || *(s1 + i) == 'E' || *(s1 + i) == 'I' || *(s1 + i) == 'O' || *(s1 + i) == 'U' ||
*(s1 + i) == 'a' || *(s1 + i) == 'e' || *(s1 + i) == 'i' || *(s1 + i) == 'o' || *(s1 + i) == 'u')
{
*s2 = *(s1 + i);
s2++;
}
}
}
int main()
{
#if A
char mystr[256] = { 0 };
gets_s(mystr);
myreverse(mystr);
printf("%s\n", mystr);
#endif
char mystr[256] = { 0 };
char result[256] = { 0 };
gets_s(mystr);
vowels(mystr, result);
printf("%s\n", result);
return 0;
}
/**
*备用注释:
*
*
*
*/
六、CodeUp 571
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <ctype.h>
using namespace std;
#if E
void inputTen(int myarray[],int n)
{
for (int i = 0; i < n; ++i)
{
scanf("%d", myarray + i);
}
}
void OperateTen(int myarray[],int n)
{
int myarray2[10] = { 0 }, min, max, minIndex, maxIndex;
for (int i = 0; i < n; ++i)
{
myarray2[i] = myarray[i];
}
sort(myarray2, myarray2 + n);
min = myarray2[0];
max = myarray2[n - 1];
for (int i = 0; i < n; ++i)
{
if (myarray[i] == min)
{
minIndex = i;
}
else if (myarray[i] == max)
{
maxIndex = i;
}
}
swap(myarray[0], myarray[minIndex]);
swap(myarray[n - 1], myarray[maxIndex]);
}
void outputTen(int myarray[],int n)
{
for (int i = 0; i < n - 1; ++i)
{
printf("%d ", myarray[i]);
}
printf("%d\n", myarray[n - 1]);
}
#endif
int main()
{
#if A
int* a = NULL, * b = NULL, m, n;
scanf("%d%d", &m, &n);
a = &m, b = &n;
if (*a > * b)
printf("%d %d\n", *a, *b);
else
printf("%d %d\n", *b, *a);
#endif
#if B
int m, n, p;
scanf("%d%d%d", &m, &n, &p);
int* a = &m, * b = &n, * c = &p;
if (*a > * b&&* a > * c)
{
if (*b > * c)
printf("%d %d %d\n", *a, *b, *c);
else
printf("%d %d %d\n", *a, *c, *b);
}
else if (*a < *b && *a < *c)
{
if (*b > * c)
printf("%d %d %d\n", *b, *c, *a);
else
printf("%d %d %d\n", *c, *b, *a);
}
else if (*b > * c)
printf("%d %d %d\n", *b, *a, *c);
else
printf("%d %d %d\n", *c, *a, *b);
#endif
#if C
const char* a = "I love China!";
int n;
scanf("%d", &n);
if (n >= 0 && n < 13)
{
printf("%s\n", a + n);
}
#endif
#if D
char str1[24] = { 0 };
char str2[24] = { 0 };
char str3[24] = { 0 };
gets_s(str1);
gets_s(str2);
gets_s(str3);
char* str11 = strdup(str1), * str21 = strdup(str2), * str31 = strdup(str3);
/*
// 主要是做大写转小写的,本题可以不要这一块
while (*str11 != '\0')
{
*str11 = tolower(*str11);
str11++;
}
while (*str21 != '\0')
{
*str21 = tolower(*str21);
str21++;
}
while (*str31 != '\0')
{
*str31 = tolower(*str31);
str31++;
}
*/
if (strcmp(str1, str2) > 0 && strcmp(str1, str3) > 0)
{
if (strcmp(str2, str3) > 0)
{
printf("%s\n%s\n%s\n", str3, str2, str1);
}
else
printf("%s\n%s\n%s\n", str2, str3, str1);
}
else if (strcmp(str1, str2) < 0 && strcmp(str1, str3) < 0)
{
if (strcmp(str2, str3) > 0)
{
printf("%s\n%s\n%s\n", str1, str3, str2);
}
else
printf("%s\n%s\n%s\n", str1, str2, str3);
}
else if (strcmp(str2, str3) > 0)
{
printf("%s\n%s\n%s\n", str3, str1, str2);
}
else
printf("%s\n%s\n%s\n", str2, str1, str3);
#endif
#if E
int myarray[10] = { 0 };
inputTen(myarray, 10);
OperateTen(myarray, 10);
outputTen(myarray, 10);
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
七、CodeUp 572
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
#if A
struct person {
char name[20];
int count;
}leader[3] = { "Li", 0, "Zhang", 0, "Fun", 0 };
#endif
#if B
struct student {
int num;
char name[20];
char sex;
int age;
};
#endif
#if C
struct Person {
int num;
char name[10];
char sex;
char job;
union {
int class1;
char position[10];
}category;
};
#endif
#if D
struct student {
int num;
char name[20];
int math;
int Chinese;
int English;
};
void input(student* p, int n)
{
for (int i = 0; i < n; ++i)
{
cin >> p[i].num >> p[i].name >> p[i].math >> p[i].Chinese >> p[i].English;
}
}
void print(student* p, int n)
{
for (int i = 0; i < n; ++i)
{
cout << p[i].num << " " << p[i].name << " " << p[i].math << " " << p[i].Chinese << " " << p[i].English << endl;
}
}
#endif
#if E
struct student {
int num;
char name[20];
int math;
int Chinese;
int English;
double average;
};
void input(student* p, int n, int& totalCh, int& totalEn, int& totalMa)
{
for (int i = 0; i < n; ++i)
{
cin >> p[i].num >> p[i].name >> p[i].math >> p[i].Chinese >> p[i].English;
p[i].average = ((double)(p[i].Chinese) + (double)(p[i].English) + (double)(p[i].math)) / 3.0;
totalMa += p[i].math;
totalCh += p[i].Chinese;
totalEn += p[i].English;
}
}
#endif
int main()
{
#if A
int n = 0;
cin >> n;
char str[10] = { 0 };
while (n-- != -1)
{
cin.getline(str, 10);
if (strcmp(leader[0].name, str) == 0)
leader[0].count++;
if (strcmp(leader[1].name, str) == 0)
leader[1].count++;
if (strcmp(leader[2].name, str) == 0)
leader[2].count++;
}
for (int i = 0; i < 3; ++i)
{
cout << leader[i].name << ":" << leader[i].count << endl;
}
#endif
#if B
int n;
cin >> n;
student stuArray[20], * p = stuArray;
for (int i = 0; i < n; ++i)
{
cin >> p[i].num >> p[i].name >> p[i].sex >> p[i].age;
}
for (int i = 0; i < n; ++i)
{
cout << p[i].num << " " << p[i].name << " " << p[i].sex << " " << p[i].age << endl;
}
#endif
#if C
int n;
cin >> n;
Person perArray[100], * p = perArray;
for (int i = 0; i < n; ++i)
{
cin >> p[i].num >> p[i].name >> p[i].sex >> p[i].job;
if (p[i].job == 's')
cin >> p[i].category.class1;
else
cin >> p[i].category.position;
}
for (int i = 0; i < n; ++i)
{
cout << p[i].num << " " << p[i].name << " " << p[i].sex << " " << p[i].job << " ";
if (p[i].job == 's')
cout << p[i].category.class1 << endl;
else
cout << p[i].category.position << endl;
}
#endif
#if D
int n = 5;
student stuArray[20], * p = stuArray;
input(p, n);
print(p, n);
#endif
#if E
int n = 10, maxIndex = 0, totalCh = 0, totalEn = 0, totalMa = 0;
student stuArray[20], * p = stuArray;
input(p, n, totalCh, totalEn, totalMa);
cout << setiosflags(ios::fixed) << setprecision(2) << (double)totalMa / 10.0 << " "
<< setprecision(2) << (double)totalCh / 10.0 << " " << setprecision(2) << (double)totalEn / 10.0 << endl;
for (int i = 1; i < n; ++i)
{
if (p[i].average > p[maxIndex].average)
maxIndex = i;
}
cout << p[maxIndex].num << " " << p[maxIndex].name << " " << p[maxIndex].math
<< " " << p[maxIndex].Chinese << " " << p[maxIndex].English << endl;
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
八、CodeUp 574
其全部源码如下所示:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
#if A
int a, b;
while (EOF != scanf("%d%d", &a, &b))
printf("%d\n", a + b);
#endif
#if B
int a, b, N;
cin >> N;
while (N--)
{
scanf("%d%d", &a, &b);
printf("%d\n", a + b);
}
#endif
#if C
int a, b;
while (scanf("%d%d", &a, &b), a || b)
printf("%d\n", a + b);
#endif
#if D
int a, N;
cin >> N;
while (N != 0)
{
int sum = 0;
while (N--)
{
cin >> a;
sum += a;
if (N == 0)
printf("%d\n", sum);
}
cin >> N;
}
#endif
#if E
int a, N, M;
cin >> N;
while (N--)
{
cin >> M;
int sum = 0;
while (M--)
{
cin >> a;
sum += a;
if (M == 0)
printf("%d\n", sum);
}
}
#endif
#if F
int N, a;
while (EOF != scanf("%d", &N))
{
int sum = 0;
while (N--)
{
cin >> a;
sum += a;
if (N == 0)
printf("%d\n", sum);
}
}
#endif
#if G
int a, b;
while (EOF != scanf("%d%d", &a, &b))
printf("%d\n\n", a + b);
#endif
int a, N, M;
cin >> N;
while (N--)
{
cin >> M;
int sum = 0;
while (M--)
{
cin >> a;
sum += a;
if (M == 0)
printf("%d\n\n", sum);
}
}
return 0;
}
/**
*备用注释:
*
*
*
*/
注:这一部分(《算法笔记》第二章课后习题)已经全部更新到本人 gitee仓库,点击前往 (题号与文件对应)
相关文章
- C++STL初识,概念、六大组件、容器算法迭代器
- 【字节跳动】第十二讲 数据结构与算法 | 青训营笔记
- 深度学习笔记三:反向传播(backpropagation)算法[通俗易懂]
- 算法刷题笔记03:Stack、Queue
- 【笔记】《游戏编程算法与技巧》1-6
- 最短路径之Dijkstra(迪杰斯特拉)算法(无向图)
- OpenSSL密码库算法笔记——第5.4.13章 椭圆曲线点的压缩
- OpenSSL密码库算法笔记——第5.2章 椭圆曲线算法的函数架构图
- 五大常用算法之三:贪心算法[通俗易懂]
- 算法笔记汇总精简版下载_算法与数据结构笔记
- 二分查找算法的实现(Python)
- 【漫画算法学习笔记】第二章——2.1数组
- C语言算法及常量变量相关知识【C语言学习笔记】
- 蓄水池抽样算法
- LeetCode算法-树的遍历
- 协同编辑中使用的 OT 算法是什么?
- KMP算法笔记I ----- 先学会朴素算法
- A.机器学习入门算法(四): 基于支持向量机的分类预测
- 【使用Python实现算法】02 原生类型与内置函数
- Java拼音拆分算法详解编程语言
- Java学习笔记之十一Java中常用的8大排序算法详解总结编程语言
- C语言冒泡排序算法
- 算法详解之回溯法具体实现