1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int arr[3][2] = {{1, 2}, {3, 4}, {5, 6}};
  5. int brr[2][3] = {0};
  6. for(int i = 0; i < 3; i++) {
  7. for(int j = 0; j < 2; j++) {
  8. brr[j][i] = arr[i][j];
  9. }
  10. }
  11. for(int i = 0; i < 2; i++) {
  12. for(int j = 0; j < 3; j++) {
  13. cout << brr[i][j] << " ";
  14. }
  15. cout << endl;
  16. }
  17. return 0;
  18. }

回文数英语单词:isPlalindrome

回文数判定程序

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(){
  5. int len;
  6. string huiwenshu;
  7. cin >> huiwenshu;
  8. len = huiwenshu.size();
  9. bool realy = true;
  10. for(int i = 0;i < len / 2;i++){
  11. if(huiwenshu[i] != huiwenshu[len - i - 1]){
  12. realy = false;
  13. break;
  14. }
  15. }
  16. if(realy){
  17. cout << "Yes";
  18. }
  19. else cout << "No";
  20. return 0;
  21. }

函数思维

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. bool isPlalindrome(string s){
  5. int len = s.size();
  6. for(int i = 0;i < len;i++){
  7. if(s[i] != s[len - 1 - i]){
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. int main(){
  14. string s;
  15. cin >> s;
  16. if(isPlalindrome){
  17. cout << "Yes";
  18. }
  19. else cout << "No";
  20. return 0;
  21. }

求字符串第一个仅出现一次的字母

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(){
  5. int t[256];
  6. string s;
  7. int i;
  8. cin >> s;
  9. for(i = 0;i < 256;i++){
  10. t[i] = 0;
  11. }
  12. for(i = 0;i < s.length();i++){
  13. t[s[i]]++;
  14. }
  15. for(i = 0;i < s.length();i++){
  16. if(t[s[i]] == 1){
  17. cout << s[i] << endl;
  18. return 0;
  19. }
  20. }
  21. cout << "no" << endl;
  22. return 0;
  23. }

排序算法

归并排序

给定两个有序数列,合并成一个升序数列

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. const int SIZE = 100;
  5. int na,nb,a[SIZE],b[SIZE],i,j,k;
  6. cin >> na;
  7. for(i = 1;i <= na;i++){
  8. cin >> a[i];
  9. }
  10. cin >> nb;
  11. for(i = 1;i <= nb;i++){
  12. cin >> b[i];
  13. }
  14. i = 1;
  15. j = 1;
  16. while((i <= na) && (j <= nb)){
  17. if(a[i] <= b[j]){
  18. cout << a[i] << " ";
  19. }
  20. else{
  21. cout << b[j] << " ";
  22. j++;
  23. }
  24. }
  25. if(i <= na){
  26. for(k = i;k <= na;k++){
  27. cout << a[k] << " ";
  28. }
  29. }
  30. if(j <= nb){
  31. for(k = j;k <= nb;k++){
  32. cout << b[k] << " ";
  33. }
  34. }
  35. return 0;
  36. }