目录结构
算法笔记CodeUp Pat汇总
文章快速说明索引
学习目标:
因为最近一来要准备一下浙大PAT考试,二来复习一下将近遗忘的算法知识。正好把当年快翻烂掉的《算法笔记》再学一遍,权做C/C++和DSA的复习之用!注:因为进入工作岗位之后,基础知识的使用和遗忘会变得十分常见!面向过程 面向对象逐渐变成了面向百度编程,可是对于参加算法或编程竞赛而言 Baidu恐怕也帮不上忙了!古人云:温故而知新,可以为师矣!
1、CodeUp链接 ,点击前往
2、浙大PAT链接,点击前往
3、Tsinghua Online Judge,点击前往
本文有详细的实例(《算法笔记》书中的全部例题,都是可以直接通过评测系统的),如有疑问或者其他想法的小伙伴请在下面留言 Thanks♪(・ω・)ノ!
注:这些例题源码答案主要着重于通过 评测系统 (AC),故而仍有很多优化之处 小伙伴自行斟酌。这些也仅仅是起到一个抛砖引玉,相互借鉴的作用!
其顺序为书中的例题顺序,格式为:习题链接和题目源码
学习内容:(详见目录)
1、算法笔记 胡凡著
学习时间:
2020年11月21日06:14:39
学习产出:
1、《算法笔记》复习
2、CSDN 技术博客 1篇
一、PAT B1001
评测结果如下:
本题提交代码为:
/**══════════════════════════════════╗
*作 者: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 <assert.h>
using namespace std;
int func(int n)
{
int addTime = 0;
if (2 == n || n == 1)
return n - 1;
while (n != 1)
{
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = (3 * n + 1) / 2;
}
addTime++;
}
return addTime;
}
int main()
{
#if 1
int n = 0, result = 0;
cin >> n;
assert(n > 0 && n < 1001);
result = func(n);
cout << result << endl;
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
二、PAT B1032
评测结果如下:
本题提交代码为:
/**══════════════════════════════════╗
*作 者: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 <assert.h>
using namespace std;
#if PAT1001
int func(int n)
{
int addTime = 0;
if (2 == n || n == 1)
return n - 1;
while (n != 1)
{
if (n % 2 == 0)
{
n /= 2;
}
else
{
n = (3 * n + 1) / 2;
}
addTime++;
}
return addTime;
}
#endif
int main()
{
#if PAT1001
int n = 0, result = 0;
cin >> n;
assert(n > 0 && n < 1001);
result = func(n);
cout << result << endl;
#endif
int N, num, array[100010] = { 0 }, school, score, maxIndex = 1;//记录每个学校的总分
cin >> N;
num = N;
while (N--)
{
cin >> school >> score;
array[school] += score;
}
for (int i = 2; i <= num; ++i)
{
if (array[i] > array[maxIndex])
maxIndex = i;
}
cout << maxIndex << " " << array[maxIndex] << endl;
return 0;
}
/**
*备用注释:
*
*
*
*/
三、CodeUp 575
其全部源码如下:
/**══════════════════════════════════╗
*作 者: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 <stdbool.h>
using namespace std;
int main()
{
#if A
int mytree[10001] = { 0 }, L, M;
while (scanf("%d%d", &L, &M), L || M)
{
int result = 0;
for (int i = 0; i <= L; ++i)
{
mytree[i] = 1;
}
while (M--)
{
int begin, end;
scanf("%d%d", &begin, &end);
for (int i = begin; i <= end; ++i)
mytree[i] = 0;
}
for (int i = 0; i <= L; ++i)
{
if (mytree[i] == 1)
result++;
}
printf("%d\n", result);
}
#endif
#if B
char Astr[20] = { 0 };
char Bstr[20] = { 0 };
while (EOF != scanf("%s %s", Astr, Bstr))
{
char* pa = Astr, * pb = Bstr;
int Aint = 0, Bint = 0;
int flag = -1;
while (*pa != '\0')
{
if (*pa != ',' && *pa != '-')
{
Aint *= 10;
Aint += (*pa - '0');
}
pa++;
}
while (*pb != '\0')
{
if (*pb != ',' && *pb != '-')
{
Bint *= 10;
Bint += (*pb - '0');
}
pb++;
}
if (*Astr == '-')
Aint *= flag;
if (*Bstr == '-')
Bint *= flag;
memset(Astr, 0, 20);
memset(Bstr, 0, 20);
printf("%d\n", Aint + Bint);
}
#endif
#if C
int Astr[11] = { 0 };
int Bstr[11] = { 0 };
int data1 = 0, data2 = 0;
while (EOF != scanf("%d%d", &data1, &data2))
{
int result = 0, lenA = 0, lenB = 0;
for (int i = 0; data1 != 0; ++i)
{
Astr[i] = (data1 % 10);
data1 /= 10;
lenA++;
}
for (int i = 0; data2 != 0; ++i)
{
Bstr[i] = (data2 % 10);
data2 /= 10;
lenB++;
}
for (int i = 0; i < lenA; ++i)
{
for (int j = 0; j < lenB; ++j)
{
result += (Astr[i] * Bstr[j]);
}
}
memset(Astr, 0, 11 * sizeof(int));
memset(Bstr, 0, 11 * sizeof(int));
printf("%d\n", result);
}
#endif
#if D
int N, count1 = 0, count2 = 0, data;
while (EOF != scanf("%d", &N))
{
while (N--)
{
scanf("%d", &data);
if (data % 2 == 0)
count2++;
else
count1++;
}
if (count2 > count1)
{
printf("NO\n");
}
else
printf("YES\n");
}
#endif
#if E
int myarray[100001] = { 0 };
int distenseToFisrst[100001] = { 0 };
int N, M;
while (EOF != scanf("%d", &N))
{
int total = 0, left, right;
for (int i = 1; i <= N; ++i)
{
scanf("%d", myarray + i);
/* 意思就是:第一个距离就是点2到点1的距离,也就是distenseToFisrst[1] */
distenseToFisrst[i] = distenseToFisrst[i - 1] + myarray[i];
total += myarray[i];
}
scanf("%d", &M);
while (M--)
{
int result1 = 0, temp;
scanf("%d%d", &left, &right);
if (left > right)
{
temp = right;
right = left;
left = temp;
}
/* 尽量避免双层循环,会时间超时 */
/*
for (int i = left; i < right; ++i)
{
result1 += myarray[i];
} */
result1 = distenseToFisrst[right - 1] - distenseToFisrst[left - 1];
if (result1 < (total - result1))
{
printf("%d\n", result1);
}
else
printf("%d\n", total - result1);
}
}
#endif
#if F
int Aint, Bint, Cint, N = 0;
cin >> N;
for (int i = 0; i < N; ++i)
{
cin >> Aint >> Bint >> Cint;
if ((Aint / 2.0 + Bint / 2.0) > (Cint / 2.0))
{
cout << "Case #" << i + 1 << ": true" << endl;
}
else
cout << "Case #" << i + 1 << ": false" << endl;
}
#endif
#if G
int N, A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, data;
while (EOF != scanf("%d", &N))
{
int flag = 1, count = 0;
bool existflag = false;
while (N--)
{
scanf("%d", &data);
switch (data % 5)
{
case 0:
if (data % 2 == 0)
{
A1 += data;
}
break;
case 1:
existflag = true;
A2 += (data * flag);
flag *= -1;
break;
case 2:
A3++;
break;
case 3:
A4 += data;
count++;
break;
case 4:
if (data > A5)
A5 = data;
}
}
if (A1 == 0)
printf("N ");
else
printf("%d ", A1);
if (A2 == 0 && !existflag)
printf("N ");
else
printf("%d ", A2);
if (A3 == 0)
printf("N ");
else
printf("%d ", A3);
if (A4 == 0)
printf("N ");
else
printf("%.1lf ", (double)A4 / count);
if (A5 == 0)
printf("N ");
else
printf("%d\n", A5);
A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
}
#endif
#if H
int Aint, Bint, Da, Db, Pa = 0, Pb = 0;
while (EOF != scanf("%d%d%d%d", &Aint, &Da, &Bint ,&Db))
{
while (Aint != 0)
{
if (Aint % 10 == Da)
{
Pa *= 10;
Pa += Da;
}
Aint /= 10;
}
while (Bint != 0)
{
if (Bint % 10 == Db)
{
Pb *= 10;
Pb += Db;
}
Bint /= 10;
}
printf("%d\n", Pa + Pb);
Pa = Pb = 0;
}
#endif
int N, successOf1 = 0, failureOf1 = 0, average = 0, total = 0;
char first, second;
/* C[0]-'B' C[1]-'C' C[2]-'J' */
char C[3] = { 'B','C','J' };
int succCount[3] = { 0 }, failureCount[3] = { 0 }, maxCount1 = 0, maxCount2 = 0;
int maxIndex1 = 0, maxIndex2 = 0;
cin >> N;
total = N;
while (N--)
{
cin >> first >> second;
if (first == second)
{
average++;
continue;
}
if (first == 'C')
{
if (second == 'J')
{
successOf1++;
succCount[1]++;
}
else
failureCount[0]++;
}
if (first == 'J')
{
if (second == 'B')
{
successOf1++;
succCount[2]++;
}
else
failureCount[1]++;
}
if (first == 'B')
{
if (second == 'C')
{
successOf1++;
succCount[0]++;
}
else
failureCount[2]++;
}
}
failureOf1 = total - successOf1 - average;
printf("%d %d %d\n", successOf1, average, failureOf1);
printf("%d %d %d\n", failureOf1, average, successOf1);
for (int i = 0; i < 3; ++i)
{
if (succCount[i] > maxCount1)
{
maxCount1 = succCount[i];
maxIndex1 = i;
}
if (failureCount[i] > maxCount2)
{
maxCount2 = failureCount[i];
maxIndex2 = i;
}
}
printf("%c %c\n", C[maxIndex1], C[maxIndex2]);
return 0;
}
/**
*备用注释:
*
*
*
*/
四、CodeUp1934
其全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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;
int main()
{
int n, N;
int myarray[201] = { 0 };
while (EOF != scanf("%d", &n))
{
int x;
for (int i = 0; i < n; ++i)
{
scanf("%d", myarray + i);
}
scanf("%d", &x);
/*
sort(myarray, myarray + n);
int low = 0, high = n, mid;
while (low <= high)
{
mid = (high - low) / 2 + low;
if (x == myarray[mid])
{
printf("%d\n", mid);
break;
}
else if (x < myarray[mid])
{
high = mid;
}
else
low = mid;
} */
int i = 0;
for (; i < n; ++i)
{
if (myarray[i] == x)
{
printf("%d\n", i);
break;
}
}
if (i == n)
{
printf("%d\n", -1);
}
memset(myarray, 0, 201 * sizeof(int));
}
return 0;
}
/**
*备用注释:
*
*
*
*/
五、CodeUp 576
本题全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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 C
struct Student
{
char num[64];
char name[128];
char sex[64];
int age;
};
#endif
#if E
struct Student
{
int num;
char name[128];
char sex[64];
int age;
};
#endif
int main()
{
#if A
int N, score, n, index = 0;
int myarray[1001] = { 0 };
int myresult[1001] = { 0 };
while (scanf("%d", &N), N)
{
n = N;
for (int i = 0; i < n; ++i)
{
scanf("%d", myarray + i);
}
scanf("%d", &score);
for (int i = 0; i < n; ++i)
{
if (myarray[i] == score)
myresult[index]++;
}
memset(myarray, 0, 1001 * sizeof(int));
index++;
}
for (int i = 0; i < index; ++i)
{
printf("%d\n", myresult[i]);
}
#endif
#if B
int n, N;
int myarray[201] = { 0 };
while (EOF != scanf("%d", &n))
{
int x;
for (int i = 0; i < n; ++i)
{
scanf("%d", myarray + i);
}
scanf("%d", &x);
/*
sort(myarray, myarray + n);
int low = 0, high = n, mid;
while (low <= high)
{
mid = (high - low) / 2 + low;
if (x == myarray[mid])
{
printf("%d\n", mid);
break;
}
else if (x < myarray[mid])
{
high = mid;
}
else
low = mid;
} */
int i = 0;
for (; i < n; ++i)
{
if (myarray[i] == x)
{
printf("%d\n", i);
break;
}
}
if (i == n)
{
printf("%d\n", -1);
}
memset(myarray, 0, 201 * sizeof(int));
}
#endif
#if C
int N, M;
while (EOF != scanf("%d", &N))
{
Student student[1000];
for (int i = 0; i < N; ++i)
{
scanf("%s", (student + i)->num);
scanf("%s", (student + i)->name);
scanf("%s", (student + i)->sex);
scanf("%d", &((student + i)->age));
}
scanf("%d", &M);
for (int i = 0; i < M; ++i)
{
char index[64] = { 0 };
scanf("%s", &index);
int j = 0;
for (; j < N; ++j)
{
if (strcmp(index ,student[j].num)==0)
{
printf("%s %s %s %d\n", student[j].num, student[j].name, student[j].sex, student[j].age);
break;
}
}
if (j == N)
{
printf("No Answer!\n");
}
}
}
#endif
#if D
int n, m;
int a[101] = { 0 };
int b[101] = { 0 };
while (EOF != scanf("%d", &n))
{
for (int i = 0; i < n; ++i)
{
scanf("%d", a + i);
}
scanf("%d", &m);
for (int j = 0; j < m; ++j)
{
scanf("%d", b + j);
}
for (int i = 0; i < m; ++i)
{
int j = 0;
for (; j < n; ++j)
{
if (a[j] == b[i])
{
printf("YES\n");
break;
}
}
if (j == n)
printf("NO\n");
}
memset(a, 0, sizeof(int) * 101);
memset(b, 0, sizeof(int) * 101);
}
#endif
#if E
int N;
while (EOF != scanf("%d", &N))
{
while (N--)
{
int M, num;
scanf("%d", &M);
Student student[21];
for (int i = 0; i < M; ++i)
{
scanf("%d", &((student + i)->num));
scanf("%s", (student + i)->name);
scanf("%s", (student + i)->sex);
scanf("%d", &((student + i)->age));
}
scanf("%d", &num);
for (int i = 0; i < M; ++i)
{
if (num == student[i].num)
printf("%d %s %s %d\n", student[i].num, student[i].name, student[i].sex, student[i].age);
}
}
}
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
六、PAT B1036
评测结果如下:
本题提交代码为:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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;
int main()
{
int col, row;
char c;
cin >> col >> c;
if (col % 2 == 1)
{
row = col / 2 + 1;
}
else
row = col / 2;
for (int i = 0; i < row; ++i)
{
if (i == 0 || i == (row - 1))
{
for (int j = 0; j < col; ++j)
cout << c;
cout << endl;
}
else
{
cout << c;
for (int j = 0; j< col - 2; ++j)
cout << " ";
cout << c << endl;
}
}
return 0;
}
/**
*备用注释:
*
*
*
*/
七、CodeUp 577
本题全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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;
int main()
{
#if A
int h, max, distance, H;
char c = '*';
while (EOF != scanf("%d", &h))
{
H = h;
max = 3 * h - 2;
distance = max - h;
for (int i = 0; i < H; ++i)
{
for (int j = 0; j < distance; ++j)
cout << " ";
for (int j = 0; j < h; ++j)
cout << c;
cout << endl;
h += 2;
distance -= 2;
}
}
#endif
#if B
int N, size, mid;
char str[100] = { 0 };
while (EOF != scanf("%s", str))
{
N = strlen(str);
size = (N + 2) / 3;
mid = N - 2 * size;
int i = 0;
for (; i < size-1; ++i)
{
printf("%c", str[i]);
for (int j = 0; j < mid; ++j)
printf(" ");
printf("%c\n", str[N - i - 1]);
}
for (int j = i; j < N - i; ++j)
{
printf("%c", str[j]);
}
printf("\n");
}
#endif
#if C
int m, h, max, distance, H;
char c = '*';
scanf("%d", &m);
while (m--)
{
scanf("%d", &h);
H = h;
max = 3 * h - 2;
distance = (max - h) / 2;
for (int i = 0; i < H; ++i)
{
for (int j = 0; j < distance; ++j)
cout << " ";
for (int j = 0; j < h; ++j)
cout << c;
for (int j = 0; j < distance; ++j)
cout << " ";
cout << endl;
h += 2;
distance -= 1;
}
}
#endif
#if D
int n;
while (EOF != scanf("%d", &n))
{
char myarray[50][99];
for (int i = 0; i <= (2 * n - 1) / 2; ++i)
{
for (int j = 0; j < 2 * n - 1; ++j)
{
myarray[i][j] = ' ';
}
}
for (int j = 0; j < 2 * n - 1; ++j)
{
if (j % 2 == 0)
myarray[0][j] = '*';
}
for (int i = 1; i <= (2 * n + 1)/2; ++i)
{
int begin = i;
for (int j = begin; j <= (2 * n - 2 - begin); ++j)
{
if (myarray[begin - 1][j] == '*')
myarray[begin][j] = ' ';
else
myarray[begin][j] = '*';
}
}
for (int i = 0; i <= (2 * n - 1)/2; ++i)
{
for (int j = 0; j < 2 * n - 1; ++j)
{
printf("%c", myarray[i][j]);
}
printf("\n");
}
for (int i = (2 * n + 1) / 2; i <= 2 * n - 2; ++i)
{
for (int j = 0; j < 2 * n - 1; ++j)
{
printf("%c", myarray[2 * n - 2 - i][j]);
}
printf("\n");
}
}
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
八、CodeUp 578
本题全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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 1
bool myLeap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
#endif
#if B
//基姆拉尔森公式
int calc_week(int y, int m, int d)
{
if (m == 1 || m == 2) { //month 13,14,3,...12
y--;
m += 12;
}
int tag = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
return tag;
}
#endif
int main()
{
#if A
int year1, year2;
char year1Str[9] = { 0 }, year2Str[9] = { 0 };
int yearArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
while (EOF != scanf("%s%s", year1Str, year2Str))
{
int y1 = 0, y2 = 0, m1 = 0, m2 = 0, d1 = 0, d2 = 0, result = 1;
char* str1, * str2;
char ystr[5] = { 0 }, mstr[3] = { 0 }, dstr[3] = { 0 };
year1 = atoi(year1Str);
year2 = atoi(year2Str);
if (year1 > year2)
{
str1 = year2Str;
str2 = year1Str;
}
else
{
str1 = year1Str;
str2 = year2Str;
}
strncpy(ystr, str1, 4 * sizeof(char));
strncpy(mstr, str1 + 4, 2 * sizeof(char));
strncpy(dstr, str1 + 6, 2 * sizeof(char));
y1 = atoi(ystr);
m1 = atoi(mstr);
d1 = atoi(dstr);
strncpy(ystr, str2, 4 * sizeof(char));
strncpy(mstr, str2 + 4, 2 * sizeof(char));
strncpy(dstr, str2 + 6, 2 * sizeof(char));
y2 = atoi(ystr);
m2 = atoi(mstr);
d2 = atoi(dstr);
while (y1 < y2 || m1 < m2 || d1 < d2)
{
if (myLeap(y1))
{
yearArr[1] = 29;
}
else
{
yearArr[1] = 28;
}
d1++;
if (yearArr[m1 - 1] + 1 == d1)
{
m1++;
d1 = 1;
}
if (m1 == 13)
{
y1++;
m1 = 1;
}
result++;
}
printf("%d\n", result);
}
#endif
#if B
int day, year, month;
char name[10] = { 0 };
const char* MonArr[12] = { "January","February","March","April",
"May","June","July","August",
"September","October","November","December" };
const char* WeeArr[7] = { "Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday" };
while (EOF != scanf("%d%s%d", &day, name, &year))
{
int result;
for (int i = 0; i < 12; ++i)
{
if (strcmp(name, MonArr[i]) == 0)
{
month = i + 1;
break;
}
}
result = calc_week(year, month, day);
printf("%s\n", WeeArr[result]);
}
#endif
#if C
int m, n;
while (~scanf("%d%d", &m, &n))
{
int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int yearArr[12] = { 31,59,90,120,151,181,212,243,273,304,334,365 };
if (myLeap(m))
{
monthArr[1] = 29;
for (int i = 1; i < 12; ++i)
yearArr[i]++;
}
for (int i = 0, j = n; ;++i)
{
if (j - monthArr[i] > 0)
{
j -= monthArr[i];
}
else
{
printf("%04d-%02d-%02d\n", m, i + 1, j);
break;
}
}
}
#endif
#if D
int m;
scanf("%d", &m);
while (m--)
{
// 测试数据已不存在闰年
int year, month, day;
bool flag = false;
int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
scanf("%d%d%d", &year, &month, &day);
if (day == monthArr[month - 1])
{
flag = true;
day = 1;
month++;
}
if (month == 13)
{
year++;
month = 1;
}
if (flag)
printf("%04d-%02d-%02d\n", year, month, day);
else
printf("%04d-%02d-%02d\n", year, month, day + 1);
}
#endif
int m;
scanf("%d", &m);
while (m--)
{
int year, month, day, sum;
bool flag = false;
int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
scanf("%d%d%d%d", &year, &month, &day, &sum);
while (sum > 0)
{
if (month == 13)
{
year++;
month = 1;
}
if (myLeap(year))
{
monthArr[1] = 29;
}
else
{
monthArr[1] = 28;
}
for (int i = month; i < 13; ++i)
{
if (day + sum > monthArr[month - 1])
{
sum -= (monthArr[month - 1] - day);
sum--;
month++;
day = 1;
}
else
{
day += sum;
sum = 0;
break;
}
}
}
printf("%04d-%02d-%02d\n", year, month, day);
}
return 0;
}
/**
*备用注释:
*
*
*
*/
九、PAT B1022
评测结果如下:
本题提交代码为:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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;
int main()
{
unsigned int Aint, Bint, Cint;
int Dint;
while (~scanf("%d%d%d", &Aint, &Bint, &Dint))
{
int result[65] = { 0 };
int index = 0;
Cint = Aint + Bint;
while (Cint != 0)
{
result[index++] = (Cint % Dint);
Cint /= Dint;
}
if (index == 0)
{
printf("%d\n", result[0]);
}
else
{
for (int i = index - 1; i >= 0; --i)
{
printf("%d", result[i]);
}
printf("\n");
}
}
return 0;
}
/**
*备用注释:
*
*
*
*/
十、CodeUp 579
本题全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#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;
char mylowwer(char c)
{
if (c <= 'Z' && c >= 'A')
return c - 'A' + 'a';
}
char myupper(char c)
{
if (c <= 'z' && c >= 'a')
return c + 'A' - 'a';
}
char mydivfunc(char* numArr, int base)
{
char shangArr[256] = { 0 };
int len = strlen(numArr);
if (len == 0)
return '0';
bool firstnotzero = true;
int beichushu = 0, yushu = 0, shang;
for (int i = 0, j = 0; i < len; ++i)
{
beichushu = yushu * 10 + numArr[i] - '0';
yushu = beichushu % base;
shang = beichushu / base;
if (0==shang && firstnotzero)
{
/* Nothing to do */
}
else
{
shangArr[j++] = shang + '0';
firstnotzero = false;
}
}
strncpy(numArr, shangArr, strlen(shangArr) + 1);
return yushu + '0';
}
int main()
{
#if PAT_B1022
unsigned int Aint, Bint, Cint;
int Dint;
while (~scanf("%d%d%d", &Aint, &Bint, &Dint))
{
int result[65] = { 0 };
int index = 0;
Cint = Aint + Bint;
while (Cint != 0)
{
result[index++] = (Cint % Dint);
Cint /= Dint;
}
if (index == 0)
{
printf("%d\n", result[0]);
}
else
{
for (int i = index - 1; i >= 0; --i)
{
printf("%d", result[i]);
}
printf("\n");
}
}
#endif
#if A
unsigned int Aint, Bint, Cint;
int Dint;
while (scanf("%d", &Dint), Dint)
{
scanf("%d%d", &Aint, &Bint);
int result[65] = { 0 };
int index = 0;
Cint = Aint + Bint;
while (Cint != 0)
{
result[index++] = (Cint % Dint);
Cint /= Dint;
}
if (index == 0)
{
printf("%d\n", result[0]);
}
else
{
for (int i = index - 1; i >= 0; --i)
{
printf("%d", result[i]);
}
printf("\n");
}
}
#endif
#if B
char sixteenNum[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
char n[16] = { 0 };
unsigned int a, b, temp = 0;
while (~scanf("%d%s%d", &a, n, &b))
{
char result[65] = { 0 };
int index = 0;
if (a == 10)
{
temp = atoi(n);
}
else
{
int len = strlen(n);
int product = 1;
for (int i = len - 1; i >= 0; --i)
{
if (isdigit(n[i]))
{
temp += (n[i] - '0') * product;
}
else
{
temp += (mylowwer(n[i]) - 'a' + 10) * product;
}
product *= a;
}
}
if (0 == temp)
{
printf("0\n");
}
else
{
while (temp != 0)
{
int achar = temp % b;
result[index++] = myupper(sixteenNum[achar]);
temp /= b;
}
for (int i = index - 1; i >= 0; --i)
{
printf("%c", result[i]);
}
printf("\n");
}
}
#endif
#if C
char numArr_raw[256] = { 0 };
char numArr[256] = { 0 };
char result[512] = { 0 };
while (~scanf("%s", numArr_raw))
{
int a = 2, i = 0;
int product = 1;
int len = strlen(numArr_raw);
for (int j = 0; j < len; ++j)
{
if (numArr_raw[j] != '0')
{
strncpy(numArr, numArr_raw + j, strlen(numArr_raw) + 1);
break;
}
}
do
{
result[i++] = mydivfunc(numArr, 2);
} while (atoi(numArr) != 0);
for (int j = strlen(result) - 1; j >= 0; --j)
{
printf("%c", result[j]);
}
printf("\n");
memset(result, 0, 512 * sizeof(char));
}
#endif
#if D
int N;
while (~scanf("%d", &N))
{
if (0 == N)
{
printf("0\n");
continue;
}
int result[64] = { 0 }, index = 0;
while (N != 0)
{
result[index++] = N % 8;
N /= 8;
}
for (int i = index - 1; i >= 0; --i)
{
printf("%d", result[i]);
}
printf("\n");
}
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
十一、PAT B1009
愚蠢的提交(C语言),这个提交有问题 代码写的极差 仅作为一个参考:
不能通过的原因如下:
1、最后一个单词不可以是一位的
Hello 1
代码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
int main()
{
#if I
char str[256] = { 0 };
while (~scanf("%s", str))
{
bool flag = true;
int len = strlen(str);
for (int i = 0; i < len / 2; ++i)
{
if (str[i] != str[len - 1 - i])
flag = false;
}
if (flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
memset(str, 0, 256 * sizeof(char));
}
#endif
char str[81] = { 0 };
char* wordsIndex[40];
while (gets(str))
{
int len = strlen(str);
bool inWord = false, beginFlag = false;
int j = 0, begin = 0;
for (int i = 0; i < len; ++i)
{
if ((inWord && str[i] == ' ') || (inWord && i == len - 1))
{
int wordLen = i - begin + 2;
wordsIndex[j] = (char*)malloc(wordLen * sizeof(char));
memset(wordsIndex[j], 0, wordLen * sizeof(char));
if (i == len - 1)
{
strncpy(wordsIndex[j], str + begin, wordLen);
wordsIndex[j][i - begin + 1] = '\0';
}
else
{
strncpy(wordsIndex[j], str + begin, wordLen - 1);
wordsIndex[j][i - begin] = '\0';
}
j++;
inWord = false;
beginFlag = false;
}
else if(' ' != str[i])
{
if (!beginFlag)
{
begin = i;
beginFlag = true;
}
inWord = true;
}
}
for (int i = j - 1; i > 0; --i)
{
printf("%s ", wordsIndex[i]);
free(wordsIndex[i]);
wordsIndex[i] = NULL;
}
if (j != 0)
{
printf("%s", wordsIndex[0]);
free(wordsIndex[0]);
wordsIndex[0] = NULL;
}
printf("\n");
}
return 0;
}
/**
*备用注释:
*
*
*
*/
下面的正确的代码提交:
十二、CodeUp 580
本题全部源码如下:
/**══════════════════════════════════╗
*作 者:songbaobao ║
*职 业:我以我血荐轩辕 ║
*CSND地址:https://blog.csdn.net/weixin_43949535 ║
**GitHub :https://github.com/TsinghuaLucky912/My_own_C-_study_and_blog║
**GitEE :https://gitee.com/lucky912_admin/code-up_-pat ║
*═══════════════════════════════════╣
*创建时间:
*功能描述:
*
*
*═══════════════════════════════════╣
*结束时间:
*═══════════════════════════════════╝
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
// __.' ~. .~ `.__
// .'// 西南\./联大 \\`.
// .'// | \\`.
// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
// .'//.-" `-. | .-' "-.\\`.
// .'//______.============-.. \ | / ..-============.______\\`.
//.'______________________________\|/______________________________`.
*/
#include <iostream>
#include <vector>
#include <string.h>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <list>
#include <stdlib.h>
#include <map>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdbool.h>
using namespace std;
char mylower(char c)
{
if (c <= 'Z' && c >= 'A')
{
c = c - 'A' + 'a';
return c;
}
}
int main()
{
#if I
char str[256] = { 0 };
while (~scanf("%s", str))
{
bool flag = true;
int len = strlen(str);
for (int i = 0; i < len / 2; ++i)
{
if (str[i] != str[len - 1 - i])
flag = false;
}
if (flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
memset(str, 0, 256 * sizeof(char));
}
#endif
#if A
char str1[101] = { 0 };
char str2[101] = { 0 };
while (~scanf("%s%s", str1, str2))
{
char result[203] = { 0 };
int j = 0;
for (int i = 0; str1[i]!='\0'; ++i)
{
result[j++] = str1[i];
}
for (int i = 0; str2[i] != '\0'; ++i)
{
result[j++] = str2[i];
}
result[j] = '\0';
printf("%s\n", result);
memset(str1, 0, 101 * sizeof(char));
memset(str2, 0, 101 * sizeof(char));
}
#endif
#if B
char str[101];
int len = 0;
while (gets_s(str))
{
len = strlen(str);
bool flag = true;
for (int i = 0; i < len; ++i)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
if (i == 0 || str[i - 1] == ' ' || str[i - 1] == '\n' || str[i - 1] == '\r' || str[i - 1] == '\t'
|| str[i - 1] == '.' || str[i - 1] == ',')
str[i] = str[i] - 32;
}
printf("%c", str[i]);
}
printf("\n");
}
#endif
#if H
int m, num = 0;
scanf("%d", &m);
char mystr[101][21];
while (m--)
{
scanf("%s", mystr[num++]);
}
for (int i = 0; i <= num - 1; ++i)
{
int index = 1;
if (i >= 3)
{
bool first = true;
for (int j = i;index != 5; --j)
{
if (first)
{
first = false;
printf("%d=%s", index++, mystr[j]);
}
else
printf(" %d=%s", index++, mystr[j]);
}
printf("\n");
}
else
{
bool first = true;
for (int j = i; j >= 0; --j)
{
if (first)
{
first = false;
printf("%d=%s", index++, mystr[j]);
}
else
printf(" %d=%s", index++, mystr[j]);
}
printf("\n");
}
}
#endif
#if G
int m;
scanf("%d", &m);
while (m--)
{
char mystr[2][51];
int len1, len2;
for (int i = 0; i < 2; ++i)
{
scanf("%s", mystr[i]);
}
len1 = strlen(mystr[0]);
len2 = strlen(mystr[1]);
if (len1 == len2)
{
printf("%s is equal long to %s\n", mystr[0], mystr[1]);
}
else if (len1 > len2)
{
printf("%s is longer than %s\n", mystr[0], mystr[1]);
}
else
{
printf("%s is shorter than %s\n", mystr[0], mystr[1]);
}
}
#endif
#if F
char mystr[201] = { 0 };
while (gets_s(mystr) != NULL)
{
int len = 0;
while (mystr[len] != '\0')
{
len++;
}
for (int i = len - 1; i >= 0; --i)
{
printf("%c", mystr[i]);
}
printf("\n");
}
#endif
#if E
char mystr[201] = { 0 };
while (gets_s(mystr)!=NULL)
{
int len = 0;
char c;
scanf("%c", &c);
getchar();// 这个地方主要是干掉 scanf后面的那个回车符号
while (mystr[len] != '\0')
{
len++;
}
for (int i = 0; i < len; ++i)
{
if (mystr[i] != c)
printf("%c", mystr[i]);
}
printf("\n");
memset(mystr, 0, 201 * sizeof(char));
}
#endif
#if D
char str[1001] = { 0 };
while (gets_s(str) != NULL)
{
char mystr[1001][1001];
int row = 0, col = 0, len = strlen(str);
for (int i = 0; i <= len; ++i)
{
if (i<len && str[i] != ' ')
mystr[row][col++] = str[i];
else if (i == strlen(str) || str[i] == ' ')
{
mystr[row][col] = '\0';
row++;
col = 0;
}
}
char target[1001] = { 0 };
gets_s(target);
char word[1001] = { 0 };
gets_s(word);
for (int i = 0; i < row; ++i)
{
if (0 == strcmp(mystr[i], target))
{
if (i == 0)
printf("%s", word);
else
printf(" %s", word);
}
else
{
if (i == 0)
printf("%s", mystr[i]);
else
printf(" %s", mystr[i]);
}
}
printf("\n");
memset(str, 0, 1001 * sizeof(char));
}
#endif
#if C
char str1[1001] = { 0 };
char str2[1001] = { 0 };
gets_s(str1);
for (int i = 0; i < strlen(str1); ++i)
{
if (str1[i] <= 'Z' && str1[i] >= 'A')
{
str1[i] = str1[i] - 'A' + 'a';
}
}
while (gets_s(str2) != NULL)
{
int len = strlen(str1);
for (int i = 0; i < strlen(str2); )
{
bool flag = false;
if (mylower(str2[i]) == str1[0])
{
for (int j = 0; j < len; ++j)
{
if (mylower(str2[j + i]) == str1[j])
{
flag = true;
}
else
{
flag = false;
if (str2[i] != ' ')
printf("%c", str2[i]);
i++;
break;
}
}
}
else
{
if (str2[i] != ' ')
printf("%c", str2[i]);
i++;
}
if (flag)
{
i += len;
}
}
printf("\n");
}
#endif
return 0;
}
/**
*备用注释:
*
*
*
*/
人生苦短,需要砥砺前行 。加油,打工人!