• 技术文章 >Python技术 >Python基础教程

    python从键盘输入若干个整数

    小妮浅浅小妮浅浅2021-05-13 11:46:48原创9553

    1、说明

    用一个函数实现数据,输入的功能,其首部为:

    1

    int Input(int s[])

    Input的参数为输入的数据,函数返回值大于0表示该行输入的数据个数,0表示输入结束。

    2、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    #include <stdio.h>

    #include <string.h>

    #include <math.h>

    #include <stdlib.h>

    // // 拷贝到平台的时候把my_fuction_lib.h注释掉.

    //#include "my_fuction_lib.h"

      

    //在此下方插入自定义函数对的声明:

    void statistics(int n, int s[], int *max, int *min, float *avg)

    {

        max[0] = min[0] =s[0];

        float sum = max[0];

        for(int i = 1;i<n;i++)

        {

            if(s[i] > max[0]) max[0] = s[i];

            else if (s[i] <min[0]) min[0] = s[i];

            sum += s[i];

        }

        avg[0] = sum/n;

    }

    int Input(int s[])

    {

        /*

        以下写法不可取,因为scanf里的i++后,回到while的判断中,是s[i]就不是刚才读组的值了

        因此无法判断刚才读到的是否结束标志0.

        int i = 0;

        scanf("%d",&s[i]);

        while(  s[i] != 0)

        {

            scanf("%d",&s[i++]);

        } */

        int i = 0;

        /* 初始化s[0] */

        scanf("%d",&s[i]);

        for(;s[i++] != 0;)

        {

            scanf("%d",&s[i]);

        }

         

        return i-1;

    }

    //主函数main

    int main()

    {

        int s[100];

      

        int min[1];

        int max[1];

        float avg[1];

        int n;

      

      

        int i = 0;

      

         

       // while(scanf("%d",&s[i++]) && s[i] != 0);

        n = Input(s);

        statistics(n, s, max, min, avg);

        printf("Num=%d\n", n);

        printf("Max=%d\n",max[0]);

        printf("Min=%d\n",min[0]);

        printf("Avg=%.3f\n",avg[0]);

      

      

        return 0;

    }

    以上就是python从键盘输入若干个整数的方法,使用input函数就可以进行输入了。看懂的小伙伴赶快动手尝试下吧。更多Python学习指路:python基础教程

    专题推荐:python输入
    上一篇:python变量中self的添加 下一篇:python中slice的三个参数

    相关文章推荐

    • 一看就懂的Python输入和输出、格式化字符串方法• python输入用空格吗• 如何用python输入数字• python输入时怎么换行• python输入三个数求平均值• python输入一个列表求平均值• python输入身份证号输出出生年月• python输入成绩求平均分

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网