中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题自己做的答案,未必是最优解。但是全部通过了。如果有更好的做法,欢迎大家在评论区讨论。
题目原始链接:https://pintia.cn/problem-sets/17/problems/type/7
我的github链接:浙大数据结构里面包括了课程ppt和个人的完整笔记(笔记更新中)。
题目列表:
打印沙漏
#include <iostream>
using namespace std;
int main()
{
// 利用等差数列公式计算。
unsigned char sigmoid = '0';
short int cnt = 0;
int sum = 0;
cin >> cnt;
cin >> sigmoid;
if (cnt == 0 || cnt > 1000)
{
cout << "输入不符合要求,程序退出。" << endl;
return -1;
}
//等差数列的项数和行数一一对应。
int row;
int tempCnt = (int)((cnt - 1) / 2);
for (row = 1; row * (row + 2) <= tempCnt; row++)
{
}
row--;
for (int j = row; j > 0; j--)
{
//打印空格
for (int i = 0; i < row - j; i++)
{
cout << " ";
}
for (int i = 0; i < 2 * j + 1; i++) // 打印符号
{
cout << sigmoid;
sum++;
}
cout << endl;
}
//打印最中间的行
int j = 0;
for (int i = 0; i < row - j; i++)
{
cout << " ";
}
for (int i = 0; i < 2 * j + 1; i++) // 打印符号
{
cout << sigmoid;
sum++;
}
cout << endl;
for (int j = 1; j <= row; j++)
{
//打印空格
for (int i = 0; i < row - j; i++)
{
cout << " ";
}
for (int i = 0; i < 2 * j + 1; i++) // 打印符号
{
cout << sigmoid;
}
cout << endl;
}
cout << cnt - (row * (row + 2) * 2 + 1);
return 0;
}
素数对猜想
* #include <iostream>
using namespace std;
# include <math.h>
int main()
{
unsigned int input;
//int index = 0;
int cnt = 0; //只找3以后的值。三之前有0个。
cin >> input;
if (input >= 100000 || input < 1)
{
cout << "输入错误";
return -1;
}
int temp = 3;
for (int i = 5; i <= input; i += 2) //查找出小于输入值的所有素数。
{
int j = 3;
int up = (int)sqrt(i) + 1;
for (; j < up; j += 1)
{
if (i % j == 0)
{
break;
}
}
if (j == up ) //i为素数
{
if ((i - temp) == 2)
{
cnt++;
//cout << i << " " << temp << endl;
}
temp = i;
}
}
cout << cnt;
return 0;
}
数组元素循环右移问题
#include <iostream>
using namespace std;
int main()
{
int N = 0; //数组元素的总个数
int M = 0; //数组移位的个数
cin >> N >> M;
int* array = new int[N]; //创建数组
for (int i = 0; i < N; i++)
{
cin >> array[i];
}
for (int j = 0; j < M; j++)
{
int temp = array[0];
array[0] = array[N - 1];
for (int i = N - 1; i > 1; i--)
{
array[i] = array[i - 1];
}
array[1] = temp;
}
// 输出数组
for (int i = 0; i < N - 1; i++)
{
cout << array[i] << " ";
}
cout << array[N - 1];
delete[] array;
return 0;
}
Have Fun with Numbers
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSIZE 21 //最大长度应该是21,后面有个结束字符。
// 对比输入的两个数组是否一样,一样返回1
int compare(char* array1, char* array2, int length)
{
// 排序(冒泡排序)
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length - i - 1; j++)
{
char min = 0;
if (array1[j] > array1[j + 1])
{
min = array1[j + 1];
array1[j + 1] = array1[j];
array1[j] = min;
}
}
}
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length - i - 1; j++)
{
char min = 0;
if (array2[j] > array2[j + 1])
{
min = array2[j + 1];
array2[j + 1] = array2[j];
array2[j] = min;
}
}
}
int tag = 1;
for (int i = 0; i < length; i++)
{
if (array1[i] != array2[i])
{
tag = 0;
break;
}
}
return tag;
}
int main()
{
char input[MAXSIZE] = { 0, };
char output[MAXSIZE + 1] = { 0, };
cin >> input;
int tag = 0;
for (int i = strlen(input) - 1; i >= 0; i--)
{
if (input[i] - '0' < 5)
{
int temp = (input[i] - '0') * 2 + tag;
output[i + 1] = temp + '0';
tag = 0;
}
else
{
int temp = (((input[i] - '0') * 2) % 10) + tag;
output[i + 1] = temp + '0';
tag = 1;
}
}
if (tag == 1)
{
output[0] = tag + '0';
cout << "No" << endl;
cout << output;
}
else
{
char inputTemp[MAXSIZE] = { 0, };
char outputTemp[MAXSIZE] = { 0, };
for (int i = 0; i < strlen(input); i++)
{
inputTemp[i] = input[i];
outputTemp[i] = output[i + 1];
}
if (compare(inputTemp, outputTemp, strlen(input)))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
for (int i = 1; i <= strlen(input); i++)
cout << output[i];
}
return 0;
}
Shuffling Machine
#include <iostream>
using namespace std;
int main()
{
char card[54][3] = { 0, }; //使用54*2的字符数组存储牌
char newCard[54][3] = { 0, }; //使用54*2的字符数组存储牌
char acard[4] = { 'S', 'H', 'C', 'D' };
int index = -1;
int cnt = 0;
int order[54] = { 0, };
for (int i = 0; i < 52; i++)
{
cnt++;
if (i % 13 == 0)
{
index++;
}
if (cnt > 9)
{
card[i][0] = acard[index];
card[i][1] = 1 + '0';
card[i][2] = (cnt % 10) + '0';
}
else
{
card[i][0] = acard[index];
card[i][1] = cnt + '0';
card[i][2] = ' ';
}
if (cnt == 13)
cnt = 0;
}
card[52][0] = 'J';
card[52][1] = '1';
card[52][2] = ' ';
card[53][0] = 'J';
card[53][1] = '2';
card[53][2] = ' ';
cin >> cnt; //读入翻转次数
char temp[3] = { 0, };
for (int j = 0; j < 54; j++) //读入顺序的同时进行第一次交换。
{
cin >> order[j];
newCard[order[j] - 1][0] = card[j][0];
newCard[order[j] - 1][1] = card[j][1];
newCard[order[j] - 1][2] = card[j][2];
}
for (int i = 1; i < cnt; i++)
{
for (int j = 0; j < 54; j++) //进行顺序交换
{
card[j][0] = newCard[j][0];
card[j][1] = newCard[j][1];
card[j][2] = newCard[j][2];
}
for (int j = 0; j < 54; j++) //进行顺序交换
{
newCard[order[j] - 1][0] = card[j][0];
newCard[order[j] - 1][1] = card[j][1];
newCard[order[j] - 1][2] = card[j][2];
}
}
for (int i = 0; i < 53; i++)
{
if (newCard[i][2] != ' ')
{
cout << newCard[i][0] << newCard[i][1] << newCard[i][2] << " ";
}
else
{
cout << newCard[i][0] << newCard[i][1] << " ";
}
}
if (newCard[53][2] != ' ')
{
cout << newCard[53][0] << newCard[53][1] << newCard[53][2];
}
else
{
cout << newCard[53][0] << newCard[53][1];
}
return 0;
}