旋转矩阵
任意输入两个9阶以下矩阵,要求判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0、90、180、270),如果不是,输出-1。
要求先输入矩阵阶数,然后输入两个矩阵,每行两个数之间可以用任意个空格分隔。行之间用回车分隔,两个矩阵间用任意的回车分隔。(60分)
#include <iostream> #include <cmath> #include <string> using namespace std; int a[10][10]; int b[10][10]; bool judge0(int m,int n) { int i,j; if(m==n) { for(i=0;i<m;i++) for(j=0;j<m;j++) { if(a[i][j]==b[i][j]) { continue; } else { return false; } } return true; } else { return false; } } bool judge90(int m,int n) { int i,j; if(m==n) { for(i=0;i<m;i++) for(j=0;j<m;j++) { if(a[i][j]==b[m-1-j][i]) { continue; } else { return false; } } return true; } else { return false; } } bool judge180(int m,int n) { int i,j; if(m==n) { for(i=0;i<m;i++) for(j=0;j<m;j++) { if(a[i][j]==b[m-1-i][m-1-j]) { continue; } else { return false; } } return true; } else { return false; } } bool judge270(int m,int n) { int i,j; if(m==n) { for(i=0;i<m;i++) for(j=0;j<m;j++) { if(a[i][j]==b[j][m-1-i]) { continue; } else { return false; } } return true; } else { return false; } } int main() { int t,number; int i,j; int m,n; cin>>number; for(t=1;t<=number;t++) { cin>>m; for(i=0;i<m;i++) for(j=0;j<m;j++) { cin>>a[i][j]; } cin>>n; for(i=0;i<n;i++) for(j=0;j<n;j++) { cin>>b[i][j]; } if(judge0(m,n)==true) { cout<<"0"<<endl; } else if(judge90(m,n)==true) { cout<<"90"<<endl; } else if(judge180(m,n)==true) { cout<<"180"<<endl; } else if(judge270(m,n)==true) { cout<<"270"<<endl; } else { cout<<"-1"<<endl; } } return 0; }
3.字符串匹配
从string.in中读入数据,然后用户输入一个短字符串。要求查找string.in中和短字符串的所有匹配,输出行号、匹配字符串到string.out文件中。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如“aa[123]bb”,就是说aa1bb、aa2bb、aa3bb都算匹配。(60分)
#include <iostream> #include <string> #include <iterator> using namespace std; int main() { string a = "abcdef"; string b = "a[bcde]cd[adfe]f"; string _return = a; string::size_type pos = 0; while((pos = b.find_first_of("[")) != string::npos) { if(a.substr(0, pos) == b.substr(0, pos)) { a.erase(0, pos); b.erase(0, pos); pos = b.find_first_of("]"); string tmp = b.substr(0, pos); string find = a.substr(0, 1); if(tmp.find(find.c_str()) != string::npos) { b.erase(0, pos + 1); a.erase(0, 1); } else return -1; } else return -1; } if(a == b) cout << _return << endl; return 0; }
或者
#include <iostream> #include <string> #include <iterator> using namespace std; int main() { char *a = "abcdef"; char *b = "a[bcde]cd[adfe]f"; int i = 0, j = 0; while(a[i] != '\0') { if(b[j] == '[') { j++; bool hasi = false; while(b[j] != ']') { if(b[j] == a[i]) hasi = true; j++; } if(!hasi) break; j++; i++; } else { if(a[i] != b[j]) break; i++; j++; } } if(a[i] == '\0') cout << a << endl; return 0; }