1 / 15

统计与计数

统计与计数. 概 述. 统计与计数是程序设计中遇到的常见问题。其基本方法是:设置一初值为 0 的 计数变量,对每个对象进行必要判断后,若对象满足统计条件,则计数变量自加 1 ,这样当对所有对象进行判断后,计数变量的值就是统计的结果。. 例 1 :统计从键盘输入的字母、数字、空白键(包 括空格、制表、换行符)及其它字符的个数,直到 输入复合键 ctrl+z 为止( stdio.h 中定义 EOF 为复合 键 ctrl+z )。 设如下计数变量: nl etter 为键入字母个数; nd igit 为键入数字个数;

mayda
Download Presentation

统计与计数

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 统计与计数

  2. 概 述 统计与计数是程序设计中遇到的常见问题。其基本方法是:设置一初值为0的计数变量,对每个对象进行必要判断后,若对象满足统计条件,则计数变量自加1,这样当对所有对象进行判断后,计数变量的值就是统计的结果。

  3. 例1:统计从键盘输入的字母、数字、空白键(包例1:统计从键盘输入的字母、数字、空白键(包 括空格、制表、换行符)及其它字符的个数,直到 输入复合键 ctrl+z为止(stdio.h中定义EOF为复合 键ctrl+z)。 设如下计数变量: nletter为键入字母个数; ndigit 为键入数字个数; nwhite 为键入空白键个数 (含 ' '、'\t'、'\n'); nother 为键入其它字符个数。

  4. # include <stdio.h> main ( ) { int nl, nd, nw, no, c; nl = nd = nw = no = 0; while ((c=getchar( ))!= EOF) if ( 'a'<=c&&c<='z'||'A'<=c&&c<='Z' ) nl++; else if ( '0'<=c&&c<='9' ) nd++; else if ( c==' '||c=='\t'||c=='\n' ) nw++; else no++; printf( "nletter=%d, ndigit=%d\n", nl, nd ); printf( "nwhite=%d, nother=%d\n", nw, no ); }

  5. 例2:输入一行字符, 以换行符作结束。问输入各数 码的个数。 • 设计数用数组n[10], 其元素 n[0]、n[1]、…、n[9] 分别用作与下标对应的数码0、1、…、9的计数器。 • 对数码x计数,可用表达式n[x-'0']++。

  6. #include <stdio.h> main ( ) { char s[80], c; int i, n[10]; for ( i=0; i<10; i ++) n[i] = 0; i = 0; while ((c=getchar ( ))!='\n') s[i++] = c; s[i]='\0'; for ( i=0; s[i]!='\0'; i++) if (s[i]>='0'&&s[i]<='9') n[s[i]-'0']++; /*各数码计数*/ for ( i=0; i<10; i++ ) printf ("n[%d]=%d\n", i, n[i]); }

  7. 例3:输入若干非0实数,直到输入0时停止,要求例3:输入若干非0实数,直到输入0时停止,要求 输入的实数最多不超过20个,统计其中正数的个数, 负数的个数。 设如下计数变量: n统计输入的数据总个数; posn统计正数的个数; negn统计负数的个数。

  8. main() { int n=0, posn=0, negn; double a; printf("Input real numbers:\n"); while(n<=20) { scanf("%lf", &a); if (a==0) break; if (a>0) posn++; n++; } negn=n-posn; printf("posn=%d, negn=%d\n", posn, negn); }

  9. 例4:对输入的行、单词和字符进行统计。这里的例4:对输入的行、单词和字符进行统计。这里的 单词定义是指不包括空格、制表符及换行符的字符 序列。当输入复合键 ctrl+z时(stdio.h中定义EOF为 复合键ctrl+z) ,表示输入结束。 • 设如下计数变量: nline 为输入的行数, nchars为键入的字符个数, nwords为输入的单词数。 • 另设一判断一个单词的标志变量word。word值为: 0,表示前一个字符是空格、制表符或换行符; 1,表示前一个字符不是空格、制表符或换行符。

  10. • 统计单词的基本算法思想  如果当前字符是空格、制表符或换行符,则word 置0;  如果当前字符不是空格、制表符或换行符,且前 一字符是空格、制表符或换行符(word=0); 则出现新单词,nw加1,word置1;  其余情况nw 和word均保持不变。

  11. # include <stdio.h> main ( ) { int nl, nw, nc, c, word ; nl = nc = nw = word = 0; while ((c=getchar( ))!= EOF) { nc++; if ( c== '\n' ) nl ++; if ( c==' ' || c=='\t' || c=='\n' ) word = 0; else if ( word==0 ) { word=1; nw++; } } printf( "Lines=%d\nWordsr=%d\nChars=%d\n", nl, nw, nc ); }

  12. 例5:输入10个数,输出这10个数中仅出现一次的 数。 如输入:12 36 72 36 87 99 87 87 12 35 则输出:72 99 35 • 设一计数变量n,统计一个数出现次数。 • 基本算法思想 对于每个数,与全部数一一比较,如遇相等,则 n加1。  一个数与全部数一一比较完后,如计数值n为1, 则表示该数仅出现一次,输出该数。

  13. main() { int a[10], i, j, n; for ( i = 0; i < 10; i++ ) scanf("%d", &a[i] ); for ( i = 0; i < 10; i++ ) { n = 0; for ( j = 0; j < 10; j++ ) if ( a[i]==a[j] ) n++ ; if ( n == 1 ) printf( "%d ", a[i] ); } }

  14. 练习题 1. 输入整数n (<=40)和 n个学生某门课程的成绩 (整数),要求统计0~9、10~19、…、80~89、 99~100各分数段的人数,以及平均成绩(四舍 五入取整)和高于平均成绩的人数。 2. 输入一行字符, 输出其中出现1次以上的字符。 3. 输入 n (< 100 )个整数,找出其中出现次数最多 的数(当有多个不同的数有相同的最多出现次数时,选择最大的数)。

  15. 4. 定义一个函数,统计3行4列的整数二维数组中有 多少个正数、多少个负数,多少个零,并返回统 计结果。

More Related