1. 翻转顺序打印
初始化一个字符数组为"The best or nothing",并将其中每个单词的字母顺序翻转后打印输出到屏幕。
要求:
1、字符数组的初始化在程序运行时由用户输入;
2、字符数组的翻转和结果输出功能通过函数实现;
3、字符数组不能定义成全局变量。
出处:
https://edu.csdn.net/practice/27137156
代码:
#include <stdio.h> #include <string> void trans(char* p,int len) { char* s = new char[len]; memcpy(s,p,len); for (int i = 0; i < len; i++) { p[i] = s[len-1-i]; } delete[] s; s = 0; } void transfun(char* p,int len) { int start = 0; int i = 0; int shift = 0; while(i < len) { for (i = start; i < len;i++) { if(p[i] == ' ') break; } trans(p+shift,i-start); shift += i-start+1; start = i+1; i +=1; } } void output(char* p) { printf("%s\n",p); } int main() { char buf[1000] = {0}; printf("请输入字符串:"); gets(buf); transfun(buf,strlen(buf)); output(buf); return 0; }
输出:
略
2. 字符金字塔
输入一个正整数n(代表图形的行数),输出如样例形式的图形
输入:5
输出:
1. A 2. ABA 3. ABCBA 4. ABCDCBA 5. ABCDEDCBA
以下程序实现了这一功能,请你填补空白处内容:
···c++
#include <stdio.h> #include <iostream> using namespace std; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N - i; j++) printf(" "); ______________________; printf("\n"); } return 0; } ···
出处:
https://edu.csdn.net/practice/27137157
代码:
#include <stdio.h> #include <iostream> using namespace std; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N - i; j++) printf(" "); for (int j = 0; j < i; j++) printf("%c", (char)(j + 'A')); for (int j = i; j >= 0; j--) printf("%c", (char)(j + 'A')); printf("\n"); } return 0; }
输出:
5↙
A
ABCBA
ABCDCBA
ABCDEDCBA
3. 单词搜索
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例 1:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
示例 2:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true
示例 3:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false
提示:
m == board.length
n = board[i].length
1 <= m, n <= 6
1 <= word.length <= 15
board 和 word 仅由大小写英文字母组成
进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?
以下程序实现了这一功能,请你填补空白处内容:
```c++
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> static bool dfs(char *word, char **board, bool *used, int row, int col, int row_size, int col_size) { if (board[row][col] != *word) { return false; } used[row * col_size + col] = true; if (*(word + 1) == '\0') { return true; } bool result = false; if (row > 0 && !used[(row - 1) * col_size + col]) { result = dfs(word + 1, board, used, row - 1, col, row_size, col_size); } if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col]) { result = dfs(word + 1, board, used, row + 1, col, row_size, col_size); } if (!result && col > 0 && !used[row * col_size + col - 1]) { result = dfs(word + 1, board, used, row, col - 1, row_size, col_size); } if (!result && col < col_size - 1 && !used[row * col_size + col + 1]) { __________________________; } used[row * col_size + col] = false; return result; } static bool exist(char **board, int boardRowSize, int boardColSize, char *word) { int i, j; int len = strlen(word); if (len > boardRowSize * boardColSize) { return false; } bool *used = malloc(boardRowSize * boardColSize); for (i = 0; i < boardRowSize; i++) { for (j = 0; j < boardColSize; j++) { memset(used, false, boardRowSize * boardColSize); if (dfs(word, board, used, i, j, boardRowSize, boardColSize)) { return true; } } } return false; } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Usage: ./test word row1 row2...\n"); exit(-1); } printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false"); return 0; } ```
出处:
https://edu.csdn.net/practice/27137158
代码:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> static bool dfs(char *word, char **board, bool *used, int row, int col, int row_size, int col_size) { if (board[row][col] != *word) { return false; } used[row * col_size + col] = true; if (*(word + 1) == '\0') { return true; } bool result = false; if (row > 0 && !used[(row - 1) * col_size + col]) { result = dfs(word + 1, board, used, row - 1, col, row_size, col_size); } if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col]) { result = dfs(word + 1, board, used, row + 1, col, row_size, col_size); } if (!result && col > 0 && !used[row * col_size + col - 1]) { result = dfs(word + 1, board, used, row, col - 1, row_size, col_size); } if (!result && col < col_size - 1 && !used[row * col_size + col + 1]) { result = dfs(word + 1, board, used, row, col + 1, row_size, col_size); } used[row * col_size + col] = false; return result; } static bool exist(char **board, int boardRowSize, int boardColSize, char *word) { int i, j; int len = strlen(word); if (len > boardRowSize * boardColSize) { return false; } bool *used = (bool*)malloc(boardRowSize * boardColSize); for (i = 0; i < boardRowSize; i++) { for (j = 0; j < boardColSize; j++) { memset(used, false, boardRowSize * boardColSize); if (dfs(word, board, used, i, j, boardRowSize, boardColSize)) { return true; } } } return false; } int main() { char *word1 = (char*)"ABCCED"; char *word2 = (char*)"SEE"; char *word3 = (char*)"ABCB"; char *board[] = {(char*)"ABCE",(char*)"SFCS",(char*)"ADEE"}; printf("%s\n", exist(board, 3, 4, word1) ? "true" : "false"); printf("%s\n", exist(board, 3, 4, word2) ? "true" : "false"); printf("%s\n", exist(board, 3, 4, word3) ? "true" : "false"); }
输出:
true
true
false