前兩題比較基礎,第三題也不是很難,剛做完,記錄一下,04/13更新第三題做法
————————————————————————————————
第一題:
輸入的字符串超過8個字符的,按8個截一段,最后不足8個的補0到8個
最后將重新得到的字符串按升序排列
輸入描述:輸入一個數字N ,N個字符串 ,中間以空格隔開
輸出描述:排序后的字符串
例:輸入:2 abc 123456789 輸出: 12345678 90000000 abc00000
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 5 using namespace std; 6 7 int main() { 8 int num; 9 while (cin >> num) 10 { 11 //vector<string> str_array; 12 vector<string> str_array_new; 13 for (int i = 0; i < num; i++) { 14 string str; 15 cin >> str; 16 //str_array.push_back(str); 17 while (str.length() > 8) 18 { 19 string str_temp(str.begin(), str.begin() + 8); 20 str_array_new.push_back(str_temp); 21 str.erase(str.begin(),str.begin() + 8); 22 } 23 while (str.length() < 8) 24 { 25 str.push_back('0'); 26 } 27 str_array_new.push_back(str); 28 } 29 int length = str_array_new.size(); 30 for (int i = 0; i < length; i++) 31 { 32 for (int j = i + 1 ; j < length; j++) 33 { 34 if (str_array_new[j][0] < str_array_new[i][0]) { 35 string temp = str_array_new[i]; 36 str_array_new[i] = str_array_new[j]; 37 str_array_new[j] = temp; 38 } 39 } 40 } 41 for (int i = 0; i < length; i++) 42 { 43 cout << str_array_new[i] << ' '; 44 } 45 cout << endl; 46 } 47 48 system("pause"); 49 return 0; 50 }
第二題:
題目描述:輸入一個字符串,含有括號(大括號,小括號,中括號),數字和字母,數字(n)之后必跟一個括號(測試用例里的括號都是匹配的),代表括號內的字符串重復(n)次。括號里可以有嵌套,即括號里含有括號。現在將輸入的字符串逆序展開;
輸入描述:字符串,例:abc3(A)
輸出描述:字符串,例:AAAcba
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 6 using namespace std; 7 8 int have_kuohao(string str) { 9 for (int i = 0; i < str.length(); i++) 10 { 11 if (str[i] == '(' || str[i] == '[' || str[i] == '{') 12 { 13 return 1; 14 } 15 16 } 17 return 0; 18 } 19 int main() 20 { 21 string str; 22 while (cin >> str) 23 { 24 //括號嵌套問題 25 //沒有括號直接輸出 26 if (have_kuohao(str) == 0) 27 { 28 reverse(str.begin(),str.end()); 29 cout << str << endl; 30 continue; 31 } 32 else 33 { 34 // 35 while (have_kuohao(str)) 36 { 37 int left; 38 int right; 39 for (int i = 0; i < str.length(); i++) 40 { 41 if (str[i] == '(' || str[i] == '[' || str[i] == '{') 42 { 43 left = i; 44 } 45 if (str[i] == ')' || str[i] == ']' || str[i] == '}') 46 { 47 right = i; 48 break; 49 } 50 } 51 //展開插入 52 int times = (int)(str[left - 1] - '0'); 53 string temp_str(str.begin() + left + 1, str.begin() + right); 54 //在右括號后插入 55 int insert_end = right; 56 for (int i = 0; i < times - 1; i++) 57 { 58 for (int j = 0; j < temp_str.length(); j++) 59 { 60 str.insert(str.begin() + insert_end + 1, temp_str[j]); 61 //cout << str << endl; 62 insert_end ++; 63 } 64 } 65 //刪除括號和數字 66 str.erase(str.begin() + right); 67 str.erase(str.begin() + left); 68 str.erase(str.begin() + left - 1 ); 69 } 70 reverse(str.begin(), str.end()); 71 //倒序輸出 72 cout << str <<endl; 73 } 74 } 75 76 77 system("pause"); 78 return 0; 79 }
第三題:時間不夠了,沒寫出來
題目描述:
輸入一個N*M矩陣柵格,每個柵格都有一個高程值代表海拔高度,小明從出發點到終點有幾條不同路徑?每次只能往更高海拔的地方去,走過的地方不能再走,只能前后左右走。
例:輸入:
5 4
0 1 0 0
0 2 3 3
0 3 0 0
0 4 5 6
0 7 6 0
0 1 4 1
第一行代表M*N,網格大小
后面是M*N的矩陣
最后一行前兩個數字代表 起始坐標,后面兩個數字代表 目的坐標(坐標從左上角(0,0)開始)。
輸出 :2
即兩條不同路徑
解法:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<stack> 5 using namespace std; 6 7 8 //建立一個結構體 9 10 struct grid { 11 int high = 0; 12 //flag為0代表未到達過 13 int arived_flag = 0; 14 }; 15 //深度優先遍歷搜索函數 16 int DFT_result(vector<vector<grid>> map,int m,int n, int des_zuobiao[2]) { 17 int result = 0; 18 int M = map.size(); 19 int N = map[0].size(); 20 if (m == des_zuobiao[0] && n == des_zuobiao[1]) 21 { 22 result++; 23 return result; 24 } 25 else 26 { 27 //當前位置設置為已走過 28 map[m][n].arived_flag = 1; 29 //判斷是否越界,上下左右鄰接點 30 if (m - 1 >= 0) 31 { 32 if (map[m - 1][n].arived_flag != 1 && map[m - 1][n].high > map[m][n].high) 33 { 34 result += DFT_result(map, m - 1, n, des_zuobiao); 35 } 36 } 37 if (m + 1 <= M - 1) 38 { 39 if (map[m + 1][n].arived_flag != 1 && map[m + 1][n].high > map[m][n].high) 40 { 41 result += DFT_result(map, m + 1, n, des_zuobiao); 42 } 43 } 44 if (n - 1 >= 0) 45 { 46 if (map[m][n - 1].arived_flag != 1 && map[m][n - 1].high > map[m][n].high) 47 { 48 result += DFT_result(map, m, n - 1, des_zuobiao); 49 } 50 } 51 if (n + 1 <= N - 1) 52 { 53 if (map[m][n + 1].arived_flag != 1 && map[m][n + 1].high > map[m][n].high) 54 { 55 result += DFT_result(map, m, n + 1, des_zuobiao); 56 } 57 } 58 59 } 60 61 62 return result; 63 } 64 int main(){ 65 int map_M = 0; 66 int map_N = 0; 67 cin >> map_M; 68 cin >> map_N; 69 70 vector<vector<grid>> map; 71 for (int i = 0; i < map_M; i++) 72 { 73 vector<grid> row; 74 for (int j = 0; j < map_N; j++) { 75 int high = 0; 76 cin >> high; 77 grid grid_temp{ high, 0 }; 78 row.push_back(grid_temp); 79 //cin >> map[map_M][map_N]; 80 } 81 map.push_back(row); 82 } 83 84 int pre_zuobiao[2]; 85 int des_zuobiao[2]; 86 cin >> pre_zuobiao[0]; 87 cin >> pre_zuobiao[1]; 88 cin >> des_zuobiao[0]; 89 cin >> des_zuobiao[1]; 90 91 int result = DFT_result(map, pre_zuobiao[0], pre_zuobiao[1], des_zuobiao); 92 cout << result << endl; 93 system("pause"); 94 }
