代码均为自做
题目一
阶乘数。输入一个正整数,输出时,先输出这个数本身,跟着一个逗号,再输出这个数的各位数字的阶乘和,等号,阶乘和的计算结果,并判断阶乘和是否等于原数,如果相等输出Yes,否则输出No。各数字均不会超出INT的范围
输入:
145
输出:
145,1!+4!+5!=145
Yes
输入:
1400
输出:
1400,1!+4!+0!+0!=27
No
#include <iostream>
using namespace std;
int factirial(int x) {
int ret = 1;
for (int i = 1; i <= x; i++) {
ret *= i;
}
return ret;
}
int main()
{
int n;
cin >> n;
int *num = new int[10];
int cnt = 0, m = 0;
if (n == 0) {
num[cnt++] = 0;
m = 1;
}
else {
for (int tmp = n; tmp != 0; tmp /= 10) {
num[cnt] = tmp % 10;
m += factirial(num[cnt]);
cnt++;
}
}
cout << n << ",";
for (int i = cnt-1; i >= 0; i--) {
cout << num[i] << "!";
if (i != 0) cout << "+";
else cout << "=";
}
cout << m << endl;
if (n == m) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
题目二
五子棋。输入一个19*19的矩阵,只包含数字0、1、2,表示两人下五子棋的棋牌状态,1、2分别表示两人的棋子,0表示空格。要求判断当前状态下是否有人获胜(横向、竖向或者斜线方向连成5个同色棋子)。题目说明输入样例保证每条线上至多只有连线5个同色棋子,并且保证至多只有1人获胜。如果有人获胜,输出获胜者1或2,加一个冒号,接着输出获胜的五连珠的第一个棋子的坐标,从上到下从左到右序号最小的为第一个,序号从1开始编号。如果无人获胜,输出No。
输入:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0
输出:
No
#include <iostream>
using namespace std;
class ChessBoard {
protected:
int **board;
bool **visit;
public:
ChessBoard() {
board = new int*[20];
visit = new bool*[20];
for (int i = 0; i < 20; i++) {
board[i] = new int[20];
visit[i] = new bool[20];
}
}
void readBoard() {
for (int i = 1; i < 20; i++) {
for (int j = 1; j < 20; j++) {
cin >> board[i][j];
}
}
}
void test() {
for (int j = 1; j < 20; j++) {
for (int i = 1; i < 20; i++) {
visit[j][i] = false;
}
}
int winValue = 0;
int x = 0, y = 0;
for (int i = 1; i < 20; i++) {
for (int j = 1; j < 20; j++) {
if (board[i][j] != 0 && isWin(board[i][j], i, j) == true) {
winValue = board[i][j];
x = j;
y = i;
break;
}
}
}
if (winValue != 0) {
cout << winValue << ":(" << x << "," << y << ")" << endl;
}
else cout << "No" << endl;
}
bool isWin(int s, int y, int x) {
int cnt = 0;
for (int i = y, j = x; j < 20 && board[i][j] == s; j++) {
cnt++;
}
if (cnt == 5) return true;
else cnt = 0;
for (int i = y, j = x; i < 20 && j > 0 && board[i][j] == s; i++, j--) {
cnt++;
}
if (cnt == 5) return true;
else cnt = 0;
for (int i = y, j = x; i < 20 && board[i][j] == s; i++) {
cnt++;
}
if (cnt == 5) return true;
else cnt = 0;
for (int i = y, j = x; i < 20 && j < 20 && board[i][j] == s; i++, j++) {
cnt++;
}
if (cnt == 5) return true;
else cnt = 0;
return false;
}
void printBoard() {
for (int i = 1; i < 20; i++) {
for (int j = 1; j < 20; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
};
int main()
{
ChessBoard cb;
cb.readBoard();
cb.test();
return 0;
}
/*
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0
*/
题目三
排版题。输入若干行字符,表示某电影的演职员表,每行只有一个冒号,冒号前面是职位,冒号后面是姓名,要求把各行冒号对齐,删除多余空格后输出。先输入一个数字,表示排版要求的冒号位置,该位置号保证比各行冒号前的最大字符数还要大。再输入若干行字符,最多50行,每行最多100个字符,除空格、制表符和回车之外都是有效字符,要求每行的冒号处于格式要求的位置,冒号两边与有效单词之间各有一个空格,冒号前面的单词之间只有一个空格(删除多余的空格和制表符),在冒号左边右对齐,前面全由空格填充,冒号后面的单词之间也只有一个空格,在冒号右边左对齐,最后一个单词后不加空格直接换行。
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int maoLoc;
cin >> maoLoc;
char s[50][100];
int spaceCnt[50];
int cnt = 0, index = 0;
bool space = false;
char ch;
while ((ch = getchar()) != EOF) {
if (ch == ' ' || ch == '\t') {
if (index == 0 || space == true) continue;
space = true;
s[cnt][index++] = ' ';
}
else if (ch == '\n') {
if (index == 0) continue;
s[cnt][index] = '\0';
cnt++;
index = 0;
space = false;
}
else if (ch == ':') {
if (space == false) {
s[cnt][index++] = ' ';
}
spaceCnt[cnt] = maoLoc - index;
s[cnt][index++] = ':';
s[cnt][index++] = ' ';
space = true;
}
else {
s[cnt][index++] = ch;
space = false;
}
}
for (int i = 0; i < 50; i++) {
cout << "*";
}
cout << endl;
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < spaceCnt[i]; j++) cout << ' ';
cout << s[i] << endl;
}
return 0;
}
/*
50
we are family : hero dfghk
fasda jfsad:daf dfa
asfa fsd :fdsaf dd
safd: dfsa dsafawer
*/