笔试练习

目录

1、完成find函数---在一个二维数组(vector对象)中查找有无一个数字,难点在于我不知道如何获取该二维数组的行数和列数

2、补充:关于C++中vector<vector<int>> A的使用 ****

3、空格替换为其他字符串(多于一个字符的字符串)的问题

4、将字符串或数组元素反转(使用reverse()函数)

5、旋转数组查找最小值

选择题

Last、遇到的坑

1、完成find函数---在一个二维数组(vector对象)中查找有无一个数字,难点在于我不知道如何获取该二维数组的行数和列数

 1 class Solution {
 2 public:
 3     bool Find(int target, vector<vector<int> > array) {
 4         //用rowCount、colCount分别代表数组的行数和列数
 5         int rowCount = array.size();
 6         int colCount = array[0].size();
 7         /*判断是否比数组最小的值小,或者比最大的值大,这样在数组很大时可以提高效率。
 8           数组长度为0也是合法的,所以为了避免越界加上列长为0的判断*/
 9         if(colCount == 0 || target < array[0][0] || target > array[rowCount-1][colCount-1]){
10             return false;
11         }
12         //当row大等于0且col小于列长colCount时,查找数组中是否有target
13         for(int row = rowCount-1, col = 0; row >= 0 && col < colCount;){
14             if(target == array[row][col]){
15                 return true;
16             }else if(target < array[row][col]){
17                 row--;
18             }else if(target > array[row][col]){
19                 col++;
20             }
21         }
22         return false;
23     }
24 };
View Code

方法二(遍历法):

 1 public class Solution {
 2     public static boolean Find(int target, int [][] array) {
 3         if(array == null || array.length == 0 || array[0].length == 0)
 4             return false;
 5  
 6         int len1 = array.length;
 7         int len2 = array[0].length;
 8         for(int i = 0; i < len1; i++) {
 9             for(int j = 0; j < len2; j++){
10                     if(array[i][j] == target)
11                         return true;
12                 }
13         }   
14         return false;
15     }
暴力破解法

方法三:

 1 链接:https://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e?answerType=1&f=discussion
 2 来源:牛客网
 3 
 4 public class Solution {
 5     public static boolean Find(int target, int [][] array) {
 6         if(array == null || array.length == 0 || array[0].length == 0)
 7             return false;
 8  
 9         int len1 = array.length;
10         int len2 = array[0].length;
11         for(int i = 0; i < len1; i++) {
12             if(target == array[i][0] || target == array[i][len2 - 1])
13                 return true;
14             else if(target < array[i][0])
15                 return false;
16             else if(target > array[i][len2 - 1])
17                 continue;
18             else {
19                 if(binaryFind(array[i], target))
20                     return true;
21             }
22         }
23  
24         return false;
25     }
26  
27     public static boolean binaryFind(int[] arr, int target) {
28         int l = 0;
29         int r = arr.length - 1;
30         while(l <= r) {
31             int mid = l + (r - l) / 2;
32             if(arr[mid] == target)
33                 return true;
34             else if(arr[mid] > target)
35                 r = mid - 1;
36             else
37                 l = mid + 1;
38         }
39         return false;
40     }
41 }
遍历每行进行二分查找

2、补充:关于C++中vector<vector<int>> A的使用

以下代码包含了如何向A中插入数据和如何获取A矩阵的行数和列数

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::vector;
 5 using std::cout;
 6 using std::endl;
 7 
 8 int main()
 9 {
10     vector<vector<int>> A;
11     //若想定义A = [[0,1,2],[3,4]],有两种方法
12     //方法一:定义vector B分别为[0,1,2]和[3,4],然后放入vector A。
13     //vector<int> B;
14     //B.push_back(0); 
15     //B.push_back(1);
16     //B.push_back(2);
17     //A.push_back(B);  //将B放入A中
18 
19     //B.clear();  //清除B中的元素
20     //B.push_back(3);
21     //B.push_back(4);
22     //A.push_back(B);  //将B放入A中
23 
24     //方法二
25     for (int i = 0; i < 2; ++i)  A.push_back(vector<int>());  //相当于告诉编译器有两行(或分配两个vector对象的空间)
26     A[0].push_back(0);
27     A[0].push_back(1);
28     A[0].push_back(2);
29     A[1].push_back(3);
30     A[1].push_back(4);
31 
32     int rowCount = A.size();  //获取A的行数
33     for (int i = 0; i < rowCount; i++)
34     {
35         for (int j = 0; j < A[i].size(); j++)  //A[i].size()为获取第i行的列数
36             cout << A[i][j] << "\t";
37         cout << endl;
38     }
39 
40 
41     system("pause");
42     return 0;
43 }
View Code

执行结果:

 3、替换空格为其他字符题

假如char* str = "we are happy"; 那么需要将两个空格都替换为%20,因为本身空格就占一个字符了,而一个空格需要增加三个字符,所以一个空格增加另个字符即可

计算出str中所有的空格数count,需要增加的字符数即:2*coumt;

加入要将空格替换为%200,需要增加的字符数即:(4-1)*count; %200一共占4个字符,减去空格本身的就占的一个字符

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::vector;
 5 using std::cout;
 6 using std::endl;
 7 using std::cin;
 8 
 9 void replaceSpace(char* str, int length);
10 
11 int main()
12 {
13     char str[100];
14     //str[99] = '\0';  //不用添加,添加之后会导致最后报错
15     cout << "请输入一个字符串: ";
16     cin.getline(str, 99);
17     replaceSpace(str, sizeof(str));
18     cout << str << endl;
19 
20 
21     system("pause");
22     return 0;
23 }
24 
25 void replaceSpace(char* str, int length)
26 {
27     int count = 0;
28     int i;
29     for (i = 0; i < length; i++)
30         if (str[i] == ' ')
31             count++;
32     for (i = length - 1; i >= 0; i--)
33     {
34         if (str[i] != ' ')
35             str[i + 2 * count] = str[i];
36         else
37         {
38             count--;
39             str[i + 2 * count] = '%';
40             str[i + 2 * count + 1] = '2';
41             str[i + 2 * count + 2] = '0';
42         }
43     }
44 }
vs运行

注意:getline()函数会将最后键盘输入的换行符替换为\0,故不需要自己手动给char str[]数组添加空字符!!

运行结果:

    

 如果想要将空格替换为%200, 那么只需要将上述代码中的所有2*count替换为3*count即可

 4、将字符串或数组元素反转(使用reverse()函数)

01)使用reverse()函数方法

 1 /**
 2 *  struct ListNode {
 3 *        int val;
 4 *        struct ListNode *next;
 5 *        ListNode(int x) :
 6 *              val(x), next(NULL) {
 7 *        }
 8 *  };
 9 */
10 class Solution {
11 public:
12     vector<int> printListFromTailToHead(ListNode* head) {
13         vector<int> A;
14         while (head) {
15             A.push_back(head->val);
16             head = head->next;
17         }
18         reverse(A.begin(), A.end());
19         return A;
20     }
21 };
使用reverse()将数组反转

02)使用栈

 1 public ArrayList printListFromTailToHead(ListNode listNode) {
 2     ArrayList list = new ArrayList();
 3     stack<int> mystack;  //stack需要包含头文件#include <stack>
 4     while (listNode != null) {
 5         mystack.push(listNode.val);
 6         listNode = listNode.next;
 7     }
 8     while (!mystack.empty()) {
 9         list.add(mystack.pop());
10     }
11     return list;
12 }
View Code

栈容器用法介绍:https://blog.csdn.net/lyj2014211626/article/details/66967761

03)递归方法

 1 public class Solution {
 2     ArrayList list = new ArrayList();
 3     public ArrayList printListFromTailToHead(ListNode listNode) {
 4         if(listNode!=null){
 5             printListFromTailToHead(listNode.next);
 6             list.add(listNode.val);
 7         }
 8         return list;
 9     }
10 }
View Code

递归调用笔记:https://www.cnblogs.com/YiYA-blog/p/10525031.html#m7

c++中字符串反转的3种方法:https://blog.csdn.net/Szu_AKer/article/details/52422191

5、旋转数组查找最小值

 01)不需要主函数版本

 1 class Solution {
 2 public:
 3     int minNumberInRotateArray(vector<int> rotateArray) {
 4         if(rotateArray.empty())
 5             return 0;
 6         for(int i=0; i<rotateArray.size()-1;i++)
 7         {
 8             if(rotateArray[i]>rotateArray[i+1])
 9                 return rotateArray[i+1];
10         }
11         return rotateArray[0];
12     }
13 };
View Code

02)需要主函数,并且要写上输入输出(小米笔试题)

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::vector;
 5 using std::cin;
 6 using std::cout;
 7 using std::endl;
 8 
 9 int find(vector<int> vec)
10 {
11     if (vec.size() == 0)
12         return 0;
13     for (int i = 0; i < vec.size() - 1; i++)
14     {
15         if (vec[i] > vec[i + 1])
16             return vec[i + 1];
17     }
18     return vec[0];
19 }
20 
21 int main()
22 {
23     vector<int> vec;
24     int n;
25     int Min = 0;
26     while (cin >> n)
27         vec.push_back(n);
28     Min = find(vec);
29     cout << Min << endl;
30 
31     system("pause");
32     return 0;
33 }
View Code

选择题

1、假定x和y为double型,则表达式x=2,y=x+3/2的值是3.00000

3/2是表示int型,int型自动取整变为1,x表示是浮点型,浮点型加整形转换为y 浮点型

2、数组的定义方法

 3、计算机操作系统的功能是:管理计算机资源并提供用户接口

 4、服务器相关

5、不仔细

6、软件调试技术:

试探法(强行排错法),
回溯法:人工沿着程序的控制流程往跟踪代码,直到找出错误根源为止
对分查找法:缩小错误的范围,
归纳演绎
原因排除法

 7、以下值不等于3的表达式是_____________()

 

 8、以下代码的输出结果是:

1 #include <stdio.h>
2 main()
3 {
4   char a[10]={ ‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,0},*p;
5   int i;
6   i=8;
7   p=a+i;
8   printf("%s\n",p-3);
9 }

 

1 输出会把剩下的全部输出!!!参数是头地址!
2 最后一个数字0对应转义字符 ‘\0’,为C语言中字符串的结尾。
3 因为输出%s,结果是6789;
4 如果输出%c,结果是6;
5 字符串的结束标志是0和'\0',注意'0'不是结束标志.strlen是以0和'\0'计算长度的 不包含0和'\0'。sizeof计算占用字节大小时包含0和'\0'所占的字节
解析

 %s是输出字符串,这个数组是字符串数组,`/0’是在最后,所以会将/0之前的所有数据全部输出

9、直接递归调用和间接递归调用

直接递归调用就是在函数a(或过程)中直接引用(调用)函数a本身
间接递归调用就是在函数a(或过程)中调用另外一个函数b,而该函数b又引用(调用)了函数a

 Last、遇到的坑

1、string对象不可以像数组那样随便赋值

1 ...
2 string str1 = "Hello-67aa";
3 string str2;
4 for(int i=0; i<str1.size();i++)
5     str2[i] = str1[i];
6 
7 这样的赋值操作是不对的,str将始终为空!!
8 直接使用str2 = str`就好了
string对象赋值的问题

 

转载于:https://www.cnblogs.com/YiYA-blog/p/11516669.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Perl编程语言是一种强大的脚本编程语言,适用于各种任务和领域,包括文本处理、系统管理、网络编程等。下面是一个Perl编程的笔试练习题的解答: 题目:编一个Perl脚本,统计一个文本文件中每个单词出现的次数,并按照频率从高到低的顺序输出。 解答: ```perl #!/usr/bin/perl use strict; use warnings; my $filename = 'text.txt'; open(my $file, "<", $filename) or die "无法打开文件:$!"; my %word_count; while (my $line = <$file>) { chomp($line); my @words = split(' ', $line); foreach my $word (@words) { $word =~ s/[^a-zA-Z]//g; # 只保留字母字符 next if $word eq ''; # 跳过空字符串 $word_count{lc($word)}++; } } close($file); foreach my $word (sort { $word_count{$b} <=> $word_count{$a} } keys %word_count) { print "$word: $word_count{$word}\n"; } ``` 这个Perl脚本首先打开指定的文本文件,并使用`<`操作符将文件内容读入到循环变量`$line`中。在每次迭代中,我们将每一行字符串使用空格进行分割,得到一个单词数组`@words`。接下来,我们使用一个循环遍历`@words`数组中的每个单词,并进行一些处理: 1. 使用正则表达式`s/[^a-zA-Z]//g`去除单词中的非字母字符; 2. 使用`next if $word eq '';`跳过空字符串; 3. 使用`lc($word)`将单词转换为小字母形式,并将其作为哈希表`%word_count`的键,将该单词的出现次数加一。 在循环结束后,脚本将按照单词出现次数从高到低的顺序对`%word_count`进行排序,并循环打印输出每个单词和其出现次数。 以上是对Perl编程的笔试练习题的参考解答,希望对您有帮助。如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值