1.有一串首位相连的珠子,总共m颗,每颗珠子都有自己的颜色,全部颜色共有n(n<=10),现在要在里面截取一段,要求包含所有不同的颜色,并且长度越短越好。求如何截取。请详细描述算法思路,可 以用伪代码来辅助描述。并求得时间复杂度和空间复杂度!
伪代码
for 每个珠子
出现新颜色的珠子,该颜色计数器加1,(初始为0);
总颜色计数器加1,初始为0;
if(总颜色数==N)
while(序列首位珠子的颜色不唯一)
序列起始位后移1位,
相应颜色计数器值减1;
更新最佳序列长度
更新序列起始位置
输出起始位置,最佳序列长度,打印序列元素;
(1)数组方法
#include <iostream>
#include <stdlib>
#include <time.h>
using namespace std;
int GetRandomNumber(int N) { int RandomNumber; //srand((int)time(NULL)); //为rand()函数生成不同的随机种子 //每产生一个随机数之前 都调用一次srand而由于计算机运行很快 //所以你每次用time得到的时间都是一样的(time的时间精度较低,只有55ms)。 //这样相当于使用同一个种子产生随机序列,所以产生的随机数总是相同的。应该把srand放在循环外 RandomNumber = rand() % N;//生成随机数 return RandomNumber; } int main() { int N,M; int i, j; int colorsCount = 0; int lastStartindex = 0; int bestStartIndex = 0; cout << "Enter the total number M:"; //int N; cin >> M; cout << endl; cout << "The number of selected N:"; cin >> N; int bestlen = M; int *arr = new int[M]; int *colors = new int[N]; for (i = 0; i < N; i++) { colors[i] = 0; } for (i = 0; i < N; i++) { cout << colors[i] << " "; } cout << endl; srand((unsigned)time(NULL)); //为rand()函数生成不同的随机种子 for (i = 0; i < M; i++) { arr[i] = GetRandomNumber(N); } for (i = 0; i < M; i++) { cout << arr[i] << " "; } cout << endl; for (i = 0; i < M; i++) { if (!colors[arr[i]]) // 如果珠子 arr[i]的颜色还未曾遇到过,颜色总数colorsCount++ { colorsCount++; //colors[arr[i]]++; } colors[arr[i]]++; //相应的颜色计数器加1 if (colorsCount == N) //如果已查询到的颜色数量==给定值 { int j = lastStartindex; //检查第一颗珠子的颜色是否是唯一的,若不是则查询位置后移 while (colors[arr[j]]>1) { colors[arr[j]]--; j++; } //bestStartIndex = j; if (i - j + 1 < bestlen) //如果此次选定的序列长度小于上次选定的长度,更新最小长度 { bestStartIndex = j; // 记录开始位置 bestlen = i - j + 1; //记录最佳长度 if (bestlen == N) //如果正好等于需要颜色数,则说明每一个正好一个。则退出 break; } lastStartindex = j; } //lastStartindex = j; } cout << "bestStartIndex: " << bestStartIndex << endl; cout << "bestLen: " << bestlen << endl; for (int i = bestStartIndex; i < bestlen + bestStartIndex; ++i) cout << arr[i] << " "; cout << endl; system("pause"); return 0; }
后记:
(1)、初始化指定大小的数组
数组的维度在编译的时候应该是已知的,维度必须是常量表达式。
例如: int cnt =10; int arr[cnt] //wrong,cnt不是常亮表达式 <C++ Primer 101>
上面程序中运用new创建动态数组(不是太清楚),不过程序通过了。
(2)、生成随机数
A:srand((unsigned)time(NULL));//为rand()函数生成不同的随机种子
头文件:<stdlib.h> <time.h>
B:从X到Y,有Y-X+1个数,所以要产生从X到Y的数,只需要这样写:
randomNumber=rand()%(Y-X+1)+X; 这样,就可以产生你想要的任何范围内的随机数了。
(3)、Bug
如果珠子的总数M和总颜色数N相差不大时,产生的随机数可能并不包含 [0,N]中所有的数,在while(colorsCount ==N)时会出错,程序无法进入循环。
使用vector
1 #include<iostream>
2 #include<vector>
3 #include<stdlib.h>
4 #include<time.h>
5
6 using namespace std;
7 using std::vector;
8
9 int total_peral_numbers()
10 {
11 int peral_numbers;
12 cout << "Enter the number of perals:";
13 cin >> peral_numbers;
14 cout << endl;
15 return peral_numbers;
16 }
17 int total_color_numbers()
18 {
19 int color_numbers;
20 cout << "Enter the number of color:";
21 cin >> color_numbers;
22 cout << endl;
23 return color_numbers;
24 }
25 int getRandomNumber(int a)
26 {
27 int randomNumber;
28 //srand((unsigned)time(NULL));
29 randomNumber = rand() % a;
30 return randomNumber;
31 }
32 int main()
33 {
34 int i;
35 int m, n;
36 int colorsCount = 0;
37 //初始化珠子总数,颜色数
38 m = total_peral_numbers();
39 n = total_color_numbers();
40 int best_perals_numbers = m;
41 vector<int> perals(m);
42 vector<int> colors(n);
43 vector<int>::iterator perals_start_iterator = perals.begin();
44 //初始化珠子序列
45 srand((unsigned)time(NULL));
46 /*for (i = 0; i < m; i++)
47 {
48 int temp;
49 temp = getRandomNumber(n);
50 perals.push_back(temp);
51 }*/
52 for (vector<int>::iterator start_index = perals.begin(); start_index != perals.end(); ++start_index)
53 {
54 int temp;
55 temp = getRandomNumber(n);
56 *start_index=temp;
57 }
58 for (vector<int>::iterator start_index = perals.begin(); start_index != perals.end(); ++start_index)
59 {
60 cout << *start_index << " ";
61 }
62
63 for (vector<int>::iterator perals_iterator = perals.begin(); perals_iterator != perals.end(); ++perals_iterator)
64 {
65
66 if (!*(colors.begin() + *perals_iterator))
67 colorsCount++;
68 (*(colors.begin() + *perals_iterator))++;
69 if (colorsCount == n)
70 {
71 while (*(colors.begin() + *perals_start_iterator) > 1)
72 {
73 perals_start_iterator++;
74 (*(colors.begin() + *perals_start_iterator))--;
75 }
76 if (perals_iterator - perals_start_iterator + 1 < best_perals_numbers)
77 best_perals_numbers = perals_iterator - perals_start_iterator + 1;
78 if (best_perals_numbers==n)
79 break;
80 }
81 }
82 cout << endl;
83 cout << "The least numbers of perals: " << best_perals_numbers << endl;
84 cout << "The start index: " << perals_start_iterator - perals.begin() << endl;
85 cout << "The string of perals:" << endl;
86 //for (perals_start_iterator; perals_start_iterator < perals_start_iterator + best_perals_numbers; perals_start_iterator++)
87 //{
88 // cout << *perals_start_iterator << " ";
89 //}
90 while (best_perals_numbers > 0)
91 {
92 cout << *perals_start_iterator << " ";
93 perals_start_iterator++;
94 best_perals_numbers--;
95 }
96 system("pause");
97 return 0;
98 }
以上程序表示的为珠子串,下面程序表示珠子环,使用vector方法
//与上面程序只有main不同,其余一样。
1 int main() 2 { 3 int i; 4 int m, n; 5 int colorsCount = 0; 6 vector<int>::iterator cycle_end; 7 //初始化珠子总数,颜色数 8 m = total_peral_numbers(); 9 n = total_color_numbers(); 10 int best_perals_numbers = m; 11 vector<int> perals(m); 12 vector<int> colors(n); 13 vector<int>::iterator perals_start_iterator = perals.begin(); 14 //初始化珠子序列 15 srand((unsigned)time(NULL)); 16 /*for (i = 0; i < m; i++) 17 { 18 int temp; 19 temp = getRandomNumber(n); 20 perals.push_back(temp); 21 }*/ 22 for (vector<int>::iterator start_index = perals.begin(); start_index != perals.end(); ++start_index) 23 { 24 int temp; 25 temp = getRandomNumber(n); 26 *start_index=temp; 27 } 28 for (vector<int>::iterator start_index = perals.begin(); start_index != perals.end(); ++start_index) 29 { 30 cout << *start_index << " "; 31 } 32 33 for (vector<int>::iterator perals_iterator = perals.begin(); perals_iterator != perals.end(); ++perals_iterator) 34 { 35 if (!*(colors.begin() + *perals_iterator)) 36 colorsCount++; 37 (*(colors.begin() + *perals_iterator))++; 38 if (colorsCount == n) 39 { 40 cycle_end = perals_iterator; //记录 41 while (*(colors.begin() + *perals_start_iterator) > 1) 42 { 43 perals_start_iterator++; 44 (*(colors.begin() + *perals_start_iterator))--; 45 } 46 if (perals_iterator - perals_start_iterator + 1 < best_perals_numbers) 47 48 best_perals_numbers = perals_iterator - perals_start_iterator + 1; 49 if (best_perals_numbers==n) 50 break; 51 } 52 } 53 //主要添加了下面这个循环函数 54 for (vector<int>::iterator perals_iterator = perals.begin(); perals_iterator != cycle_end; ++perals_iterator) 55 { 56 if (!*(colors.begin() + *perals_iterator)) 57 colorsCount++; 58 (*(colors.begin() + *perals_iterator))++; 59 if (colorsCount == n) 60 { 61 while (*(colors.begin() + *perals_start_iterator) > 1) 62 { 63 perals_start_iterator++; 64 (*(colors.begin() + *perals_start_iterator))--; 65 } 66 if (perals_iterator - perals_start_iterator + 1 + m < best_perals_numbers) 67 best_perals_numbers = perals_iterator - perals_start_iterator + 1 + m; 68 if (best_perals_numbers == n) 69 break; 70 } 71 }
后记:上面这种对环的求解方法有点复杂。假设Perals = {0,1,2,3,4,5,6,7,8,9},Perals = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9}
2、哥德巴赫猜想
请用你熟悉的语言编写一个函数,输出6—100000(包含6和100000)内所有偶数可以表示为哪两个素数之和。如果一个偶数有多重表示方式,输出一种即可。
要求:给出完整代码,在达到目标情况下尽量高效,简洁
1 #include<iostream> 2 #include <vector> 3 4 using namespace std; 5 using std::vector; 6 7 int main() 8 { 9 int i, j; 10 int counter = 0; 11 int min = 6; 12 int max = 1000; 13 vector<int> Prime; 14 for (i = 2; i < max; ++i) 15 { 16 for (j = 2; j <= sqrt(i); ++j) 17 { 18 if (i%j == 0) 19 { 20 counter++; 21 break; 22 } 23 } 24 if (counter == 0) 25 //cout << i << " "; 26 Prime.push_back(i); 27 counter = 0; 28 } 29 30 vector<int>::iterator Prime_index = Prime.begin(); 31 while (Prime_index != Prime.end()) 32 { 33 cout << *Prime_index << " "; 34 ++Prime_index; 35 } 36 cout << endl; 37 38 for (i = min; i <= max; i+=2) 39 { 40 vector<int>::iterator Prime_min = Prime.begin(); 41 vector<int>::iterator Prime_max = Prime.end()-1; 42 while (Prime_min <= Prime_max) 43 { 44 int test = *Prime_min + *Prime_max; 45 if (test > i) 46 Prime_max--; 47 if (test < i) 48 Prime_min++; 49 if (test == i) 50 { 51 cout << i << " = " << *Prime_min << "+" << *Prime_max << endl; 52 break; 53 } 54 } 55 } 56 system("pause"); 57 return 0; 58 }
参考
1、http://blog.csdn.net/hai8902882/article/details/8299995