Common Errors and defects in C Language beginner code (9)

Source: Internet
Author: User

The number of letters is now a string consisting of lower-case letters. You need to find the most frequently-occurring letters in the string. If the maximum number of letters is multiple, the minimum output is the one. Input: enter a positive integer T (0 <T <25) In the first line, followed by a string s in Line T. The length of s is less than 1010. Output: each group of data outputs occupies one row, and the output contains the most frequent characters. Example: input 3 abcd bbaa jsdhfjkshdfjksahdfjkhsajkf. Output: a j source code # include <stdio. h> # include <string. h> int maxchar (char x [1010]) {int I, j, temp, max; int a [26] = {0}; for (I = 0, temp = 0; I <strlen (x); I ++) {temp = x [I]-97; a [temp] + = 1 ;}for (I = 1, max = a [0], j = 0; I <26; I ++) {if (max <a [I]) {j = I; max = a [I] ;}} return j + 97;} int maxchar (char x [1010]); int main () {char s [1010], c [26]; int T, I; Scanf ("% d", & T); for (I = 0; I <T; I ++) {scanf ("% s", s ); c [I] = maxchar (s) ;}for (I = 0; I <T; I ++) {printf ("% c \ n ", c [I]);} return 0;} evaluation: Overall: I have learned to write function type declarations out of function definitions, but before other function definitions in main, in general, there is still a sense of head and foot. Main (): char s [1010], c [26]; int T, I; s array obviously should not be defined here, this array is only used inside the first for statement. The c array should not be defined as 26 elements, because the question is "0 <T <25 ". Scanf ("% d", & T); this statement cannot be blamed, but data validity is not taken into account after input. For (I = 0; I <T; I ++) {scanf ("% s", s); c [I] = maxchar (s );} for (I = 0; I <T; I ++) {printf ("% c \ n", c [I]);} the original author apparently did not understand the routine of such ACM problems (see Common Errors and flaws in C Language beginner code (5) 11 floor ~ 13 floor ). However, ACM's description of such problems is indeed easy for outsiders to misunderstand. Should ACM review itself? Aside from having no knowledge about ACM, one of the few considerations here is that you have not checked the validity of the input data (of course, ACM does not care about this ). Invalid Data should be ignored if the validity of the input data is considered: scanf ("% d", & T); if (0 <T & T <25) {// solve the problem} or "dead" while (scanf ("% d", & T ),! (0 <T & T <25) {// prompt to re-enter} for more thoughtful consideration, you can while (scanf ("% d", & T )! = 1 |! (0 <T & T <25) {while (getchar ()! = '\ N'); // prompt to re-enter} In addition, scanf ("% s", s) in this Code; if it is written as scanf ("% 1009 s ", s); That's impeccable. Although ACM's test data won't go wrong, this is a problem to be considered. The most important quality for programmers is thoughtful thinking. Maxchar () function: int maxchar (char x [1010]) {int I, j, temp, max; int a [26] = {0}; for (I = 0, temp = 0; I <strlen (x); I ++) {temp = x [I]-97; a [temp] + = 1 ;}for (I = 1, max = a [0], j = 0; I <26; I ++) {if (max <a [I]) {j = I; max = a [I] ;}} return j + 97;} int maxchar (char x [1010]); the same problem exists in Function Definition and function type declaration, it is 1010 in. This should not be written. if you write a compiler, it is meaningless. Int a [26] = {0}; this is written as int a ['Z'-'A' + 1] = {0. Note that it should be assumed that the ASCII code is used. If it is not an ASCII code, the code cannot be written like this. For (I = 0, temp = 0; I <strlen (x); I ++) {temp = x [I]-97; a [temp] + = 1 ;} there are many slots. First, temp is obviously redundant. Secondly, the 97 is too ugly. The typical tan haoqiang style. It should be written as 'A '. A [temp] + = 1; can be directly written as a [x [I]-'a'] + = 1; then, I <strlen (x) is poorly written, because strlen (x) is called here, it means that this function is called every time in the loop process. However, for this loop, strlen (x) is actually a constant, it does not need to be called every time. This is also a common problem for beginners. They can't help but have the impulse to call library functions without considering whether there is better writing. Strlen (x) is one of the most abused library functions. In fact, here we simply write x [I]! = '\ 0. It can be seen that the # include <string. h> at the beginning of the source file is completely redundant. In addition, the function of this statement is relatively independent from the function of the next for statement. It is better to abstract each statement into an independent function. For (I = 1, max = a [0], j = 0; I <26; I ++) {if (max <a [I]) {j = I; max = a [I] ;}} this place is a bit stupid, mainly because there are too many variables. The author uses max to record the maximum element and j to record its subscript. In fact, only one j is enough for (I = 1, j = 0; I <26; I ++) {if (a [j] <a [I]) {j = I ;}} finally return j + 97; this is also the way tan haoqiang's stream is not going into the stream, which is very ugly. It should be written as return j + 'a'; the number of reconstruction/* letters now gives you a string consisting of lower-case letters. You need to find the most frequently used letters in the string, if there are multiple letters at most, the least output is the one. Input: enter a positive integer T (0 <T <25) In the first line, followed by a string s in Line T. The length of s is less than 1010. Output: each group of data outputs occupies one row, and the output contains the most frequent characters. Example: Enter 3 abcd bbaa jsdhfjkshdfjksahdfjkhsajkf. Output: a j Author: Xue Fei Source: http://www.cnblogs.com/pmer/ "Common Errors and flaws in C Language beginner code" series blog */# include <stdio. h> # define S_LEN 1009 # define MAX_LEN (S_LEN + 1) # define N (x) N _ (x) # define N _ (x) # x char find_major (char *); void count (int [], char *); unsigned be_most (int [], unsigned); int main (void) {int T; puts ("number of rows? "); Scanf (" % d ", & T); if (! (0 <T & T <25) return 1; while (T --> 0) {char s [MAX_LEN]; scanf ("%" N (S_LEN) "s", s); printf ("% c \ n", find_major (s);} return 0;} char find_major (char * s) {int num ['Z'-'A' + 1] = {0}; count (num, s); // returns 'A' + be_most (num, sizeof num/sizeof num [0]); // returns the maximum number of characters} void count (int num [], char * s) {while (* s! = '\ 0') num [* s ++-'a'] ++;} unsigned be_most (int a [], unsigned n) {unsigned max = 0u; unsigned I; for (I = 1u; I <n; I ++) if (a [max] <a [I]) max = I; return max; // maximum element subscript}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.