南开高级语言程序设计2-1的oj题目答案,本人亲测AC,供大家参考。
2-2的见主页
字符串旋转
题目描述
定义字符串的旋转操作为:
左旋转 L :把字符串前面的若干个字符移动到字符串的尾部,如把字符串abcdef左旋转2位得到字符串cdefab。
右旋转 R :把字符串后面的若干个字符移动到字符串的头部,如把字符串abcdef右旋转2位得到字符串efabcd
输入格式
第一行一个整数N(1<=N<=20)表示有N组测试数据。
接下来的N行,每行有一个字符串S(长度1<S<=15); 操作M; 旋转的位数n(0<=n<=20)。当操作M为L时,代表将字符串左旋转n 位,M为R时,代表将字符串右旋转n位,M为除了R和L的其它无效操作时,按要求输出。
输出格式
相应操作(包括无效操作)之后的字符串,按照顺序输出奇数位置的字符。
样例
输入样例
2
abcdef L 2
abcdef R 2
输出样例
cea
eac
提示
字符串由大小写字母组成,无空格及标点符号。 如果移动的位数大于字符的长度,移动一圈后还要继续移动。
#include <iostream>
#include <string>
using namespace std;
int main(){int n,bit;string s,m;cin>>n;for(int i=1;i<=n;i++) {cin>>s>>m>>bit;int len=s.length();bit%=len;s+=s;if(m[0]=='L') {for(int j=bit;j<bit+len;j+=2)printf("%c",s[j]);}else if(m[0]=='R'){for(int j=len-bit;j<len-bit+len;j+=2)printf("%c",s[j]);}else {for(int j=0;j<len;j+=2)printf("%c",s[j]);}printf("\n");}return 0;
}
学生结构体
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {string id, name;double math, chi, eng;
} s[105];bool cmp1(Student a,Student b) {if(a.math!=b.math) return a.math>b.math;else return a.id<b.id;
}
bool cmp2(Student a,Student b) {if(a.chi!=b.chi) return a.chi>b.chi;else return a.id<b.id;
}
bool cmp3(Student a,Student b) {if(a.eng!=b.eng) return a.eng>b.eng;else return a.id<b.id;
}
int main() {int n,m;cin >> n;for (int i = 1; i <= n; i++)cin >> s[i].id >> s[i].name >> s[i].math >> s[i].chi >> s[i].eng;cin>>m;switch (m) {case 1:sort(s+1,s+1+n,cmp1);break;case 2:sort(s+1,s+1+n,cmp2);break;case 3:sort(s+1,s+1+n,cmp3);break;default:printf("ERROR");return 0;}for (int i = 1; i <= n; i++)cout<<s[i].id<<" "<<s[i].name<<endl;return 0;
}
数组合并
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int a[205];
int main() {int n,m;cin>>m>>n;for(int i=1;i<=n+m;i++)cin>>a[i];sort(a+1,a+1+n+m);int len= unique(a+1,a+1+n+m)-a-1;for(int i=1;i<len;i++)printf("%d ",a[i]);printf("%d\n",a[len]);return 0;
}
矩阵运算
#include <iostream>
#include <string>
#include <algorithm>using namespace std;int a[20][20];
int n, m;
bool row_flag[20], column_flag[20];void print_rows(int row, int head, int tail, bool minus) {if (head > tail || row_flag[row] || head <= 0 || tail > m) return;if(row==1) {printf("%d",a[1][1]);head++;}switch (minus) {case true:for (int i = tail; i >= head; i--)printf(" %d", a[row][i]);break;case false:for (int i = head; i <= tail; i++)printf(" %d", a[row][i]);break;}row_flag[row] = true;
}void print_columns(int column, int head, int tail, bool minus) {if (head > tail || column_flag[column] || head <= 0 || tail > n) return;switch (minus) {case true:for (int i = tail; i >= head; i--)printf(" %d", a[i][column]);break;case false:for (int i = head; i <= tail; i++)printf(" %d", a[i][column]);break;}column_flag[column] = true;
}int main() {cin >> n >> m;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> a[i][j];//蛇形printf("%d", a[1][1]);for (int i = 1; i <= n; i++) {int j;if (i % 2) {//奇数行正序j = 1;if (i == 1) j = 2;for (; j <= m; j++)printf(" %d", a[i][j]);} else {j = m;for (; j > 0; j--)printf(" %d", a[i][j]);}}printf("\n");//回转输出int up, down;int left = 0, right;bool minus = true;for (int i = 1; i <= (n + 1) / 2; i++) {up = i;down = n - i + 1;left++;right = m - left + 1;print_rows(up, left, right, !minus);print_columns(right, up + 1, down - 1, !minus);print_rows(down, left, right, minus);print_columns(left, up + 1, down - 1, minus);if (left >= right) break;}return 0;
}
距离最近的素数
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
bool judge(int x) {for(int i=2;i<=sqrt(x);i++)if(x%i==0)return false;return true;
}
int main() {int n,m;cin>>n;for(int i=1;i<=n;i++) {cin>>m;if(m>=1000000||m<=0) {printf("INCORRECT INPUT!\n");continue;}if(m==1) {printf("2 1\n");continue;}if(judge(m)) {printf("%d 0\n",m);continue;}int left,right,ldis=10000000,rdis=10000000;for(int j=1+m;;j++) {if(judge(j)) {right=j;rdis=j-m;break;}}for(int j=m-1;j>0;j--) {if(judge(j)) {left=j;ldis=m-j;break;}}if(ldis>rdis) {printf("%d %d\n",right,rdis);} else {printf("%d %d\n",left,ldis);}}return 0;
}
重载函数
#include <iostream>
#include <algorithm>
#include <vector>using namespace std;void OddFilter(int a[], int size) {sort(a + 1, a + 1 + size);for (int i = 1; i < size; i++)printf("%d ", a[i]);printf("%d\n", a[size]);
}void OddFilter(char a[], int size) {sort(a + 1, a + 1 + size);for (int i = 1; i < size; i++)printf("%c ", a[i]);printf("%c\n", a[size]);
}int trans(string x) {//判断是否为奇数int ans = 0;for (int i = 0; i < x.length(); i++) {if (x[i] == '.') {if (x[i + 1] > '4')ans += 1;break;}ans *= 10;ans += x[i] - '0';}return ans;
}int main() {int n, m;cin >> n >> m;int a[1005];int b[1005];string str;int x, siz = 0;for (int i = 1; i <= n; i++) {cin >> x;if (x % 2) a[++siz] = x;}if (!siz) printf("No Odd!\n");else OddFilter(a, siz);siz = 0;int y;for (int i = 1; i <= m; i++) {cin >> str;y = trans(str);if (y % 2) b[++siz] = y;}if (!siz) printf("No Odd!\n");else OddFilter(b, siz);int cnt[1000];char c[1005];bool flag[1005];siz = 0;cin >> str;for (int i = 0; i < str.length(); i++)cnt[str[i] - '0']++;for (int i = 0; i < str.length(); i++)if (cnt[str[i] - '0'] % 2 && !flag[str[i] - '0']) {c[++siz] = str[i];flag[str[i] - '0'] = true;}if (!siz) printf("No Odd!\n");else OddFilter(c, siz);return 0;
}