How to remove duplicates of C++ strings and numbers and find saddle points
What this article shares with you is about how to carry out the de-repetition operation of C++ strings and numbers and the search for saddle points. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, let's take a look at it with the editor.
Preface
The deduplication operation of a string or a string of numbers often bothers us, and the calculation of the saddle point is also a headache. Next, I will use the notation and the hash mapping of the array to analyze the deduplication operation and the calculation of the saddle point.
De-duplication of numbers and strings
1. De-duplication of numbers by marking method
# includeint main () {int n, I, j, flag = 1 int a [1000]; scanf ("% d", & n); for (I = 0; I)
< n; i++)//写一个for循环读入数据 { scanf("%d", &a[i]); for (j = 0; j < i; j++) { if (a[i] == a[j]) { flag = 0; break; } } if (flag) printf("%d ", a[i]); flag = 1;//标记的flag重新置1,循环再次继续。 } return 0;} 2、标记法对字符串去重 标记法对字符串进行去重操作#includeint main(){ int i, j, flag=1; char s[1000]; gets(s);//直接读入字符串,包括空格 for (i = 0; i < strlen(s); i++)//直接用strlen计算输入的字符串的长度 { for (j = 0; j < i; j++) { if (s[i] == s[j])//如果有相同字符,则将标记置0,并跳出循环 { flag = 0; break; } } if (flag) printf("%c", s[i]);//因为是每个字符输出,所以是%c flag = 1; } return 0;} 去重之后的输出:
3 hash mapping for de-duplication of numbers
# includeint main () {int n; int a [1000], b [6000] = {0}; / / define two arrays, the second of which is larger than the first; scanf ("% d", & n); for (int I = 0; I)
< n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { b[a[i]]++;//将数组a的数当作数组b的下标,将数组b中a数组作下标的数都变1; if (b[a[i]] >1) a [I] =-1 int for / if the number is repeatedly encountered, then add it again, so b [a [I] > 1 means that you have already encountered} GPS (I = 0; I)
< n; i++) if (a[i] != -1) printf("%d ", a[i]); return 0;} 4、(1)散列映射对字符串去重 #include#includeint main(){ int i, j=0; char a[1000], b[6000] = { 0 }; gets(a);//直接读入字符串,包括空格 for (i = 0; i < strlen(a); i++)//直接用strlen计算输入的字符串的长度 { b[a[i]]++; if (b[a[i]] >1) a [I] =-1; / / if the number is repeatedly encountered, it will be added again, so b [a [I] > 1 means that you have already encountered} for (I = 0; I) once.
< strlen(a); i++) { if(a[i] !=-1) printf("%c", a[i]); } return 0;} (2)散列映射对字符串去重(更好理解的版本) #includeint main(){ char s1[400],s2[400]; int a = 0,b=0; int arr[300] = {0}; gets(s1); for (int i = 0; s1[i]; i++) a++;//计算s1的元素个数 for (int i = a; i >= 0; iMub -) {arr [S1 [I]] +; take the element in the array of S1 as the subscript if (arr [S1 [I]] = = 1) / / if the element of the arr array = 1, then store the element of S1 in S2 {S2 [b] = S1 [I]; baked + Calculate the number of array S2}} for (int I = b-1; I > = 0; iMub -) printf ("% c", S2 [I]); return 0; calculation of two saddle points
What is a saddle point: there is an n × n matrix, from the upper left to the lower right is called the main oblique (inclination 135 °), and from the upper right to the lower left is called the secondary oblique (45 °). The largest element on the primary slash and the smallest element on the secondary slash are called the oblique saddle point of the matrix.
# includeint a [100] [100]; int main () {int p [200], Q [200]; / / p main big, Q main small int n, I, mrech int n; scanf ("% d", & n); for (I = 0; I)
< n; i++) for (j = 0; j < n; j++) scanf("%d", &a[i][j]); for (i = 0; i < 2 * n - 1; i++)//线条数 { p[i] = 0x80000000;//最小整数 q[i] = 0x7fffffff;//最大整数 } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (a[i][j] >P [n + I-j-1]) p [n + I-j-1] = a [I] [j]; / find the maximum of the main slash if (a [I] [j] < Q [I + j]) Q [I + j] = a [I] [j]; / find the minimum of the secondary slash} s = 0; for (I = 0; I < n) For +) {if (a [I] [j] = = p [n + I-j-1] & & a [I] [j] = = Q [I + j]) s + = a [I] [j]) }} printf ("% d", s); return 0;} above is how to remove repetition of C++ strings and numbers and find saddle points. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.