编程两道——悼念2016.3.25模拟糟糕的模拟考

Posted Avery-Lyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程两道——悼念2016.3.25模拟糟糕的模拟考相关的知识,希望对你有一定的参考价值。

很久不做算法题,模拟考一团糟,今天自己实现记录下:

1.生成N位格雷码,e.g.[00,01,10,11]

 1 #include <iostream>
 2 #include <string>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 
 7 const int n = 4;
 8 int size = 0;
 9 int num = int(pow(2, n));
10 string* codes = new string[num];
11 bool push(string* arr, string target){
12     for (int i = 0; i < size; i++){
13         if (target == arr[i]) return false;
14     }
15     arr[size] = target;
16     size++;
17     return true;
18 }
19 void make(string last){
20     if (size == num) return;
21     int pos = last.length() - 1;
22     bool flag = false;
23     string copy;
24     while (flag == false){
25         copy = last;
26         if (copy[pos] == 1){
27             copy[pos] = 0;
28         }
29         else if (copy[pos] == 0){
30             copy[pos] = 1;
31         }
32         pos = ((pos--) + last.length()) % last.length();
33         flag = push(codes, copy);
34     }
35     last = copy;
36     make(last);
37 }
38 
39 int main(){    
40     string str(n,0);
41     push(codes, str);
42     make(str);
43     for (int i = 0; i < num;i++)
44         cout << codes[i]<<endl;
45     system("pause");
46     return 0;
47 }

2.两个不大于1000位的大整数乘法:

 1 #include <iostream>
 2 #include <string>
 3 #define NUM(alp) (alp-‘0‘)
 4 #define CHAR(num) (num+‘0‘)
 5 using namespace std;
 6 
 7 int** writeMatrix(string num1,string num2){
 8     int** matrix = new int*[num2.length()];
 9     for (int i = 0; i < num2.length(); i++){
10         matrix[i] = new int[num1.length()];
11         memset(matrix[i], 0, num1.length());
12     }
13     int carry = 0,left=0;
14     for (int i = num2.length() - 1; i >= 0; i--){
15         for (int j = num1.length() - 1; j >= 0; j--){
16             //cout << " i: " << num2.length() - i - 1 << " j: " << num1.length() << " i*j: " << NUM(num2[i])*NUM(num1[j]);
17             matrix[-i+num2.length() - 1][j] = NUM(num2[i])*NUM(num1[j]);
18         }        
19     }
20     return matrix;
21 }
22 string calcuMatrix(int** matrix,int row,int col){
23     int carry = 0, left = 0, sum = 0;
24     string res="";
25     for (int i = 0, j = col - 1; j >= 0; j--){
26         sum = 0;
27         for (int m = i, n = j; m <= (row - 1) && n <= (col - 1); m++, n++){
28             sum += matrix[m][n];
29         }
30         left = (sum + carry) % 10  ;
31         carry = sum / 10;
32         res += CHAR(left);
33     }
34     for (int i = 1, j = 0; i < row; i++){
35         sum = 0;
36         for (int m = i, n = j; m <= (row - 1) && n <= (col - 1); m++, n++){
37             sum += matrix[m][n];
38         }
39         left = (sum + carry) % 10 ;
40         carry = sum / 10;
41         res += CHAR(left);
42     }
43     if (carry > 0){
44         while (carry % 10 != 0){
45             res += CHAR(carry % 10);
46             carry /= 10;
47         }            
48     }
49     return res;
50 }
51 string reverse(string str){
52     char ch;
53     for (int i = str.length() - 1,j=0; i >= (str.length()-1)/2; i--,j++){
54         ch = str[i];
55         str[i] = str[j];
56         str[j] = ch;
57     }
58     return str;
59 }
60 int main(){
61     string num1, num2;
62     string res;
63     cin >> num1;
64     cin >> num2;
65     int row = num2.length(),col=num1.length();
66     cout << reverse(calcuMatrix(writeMatrix(num1, num2), row, col));
67     system("pause");
68     return 0;
69 }

 

原来笔试不是要不完整的代码写出来的(能完整且正确当然最好),而是考察思路的,长见识了。。。。。。

以上是关于编程两道——悼念2016.3.25模拟糟糕的模拟考的主要内容,如果未能解决你的问题,请参考以下文章

一个蒟蒻的挣扎模拟赛2解题报告

[OI - 模拟考] LYOI2018模拟考N

2019浙大计算机考研机试模拟赛——概念专题

校内模拟考

iOS 7.0.3 - 在 iPad 3 上表现糟糕,但在模拟器中表现出色

C语言模拟排队编程,急求完整答案