C language blog jobs 04 Array

0. show PTA scores



1. This chapter summarizes the study

1.1 learning content summary

1.int a[10];为定义数组,表示数组有10个数
2.数组的下标都是从0开始,到n-1结束
3.数组里元素的个数不能大于数组的长度
4.数组长度必须是常量,不能是变量。如int a[x]错误
5.静态数组使用方法,static 类型名 数组名[数组长度]
6.静态数组若没有初始化,系统会自动给数组元素赋值0
7.二维数组a[i][j],i代表行标,j代表列标,行和列都从0开始,列长度不能省略
8.字符数组的结束符为'\0',用其来控制循环
9.字符数组定义:例如char a[100]
查找数据:可用顺序查找法
           scanf输入要查找的值x
           for(从数组下标0到n-1)
           {if所要查找的值与数组中某个值相等
                输出该数的下标}
数组中插入数据
  for{找出a[i]>x;
       退出循环}
  for(循环从n开始,且循环条件为k>i)
     {另数组中的后一个数等于前一个数}
  另a[i]=x;
  for(条件为i<n+1)
   {输出数组}
数组中删除数据:
  法一:定义一个新数组
      for(遍历数组)
      {flag=0;
        for(二次遍历数组)
        {if(a[i]=a[j]说明有重复数据)
          flag=1;       }   
         if(flag==0)说明该数据不重复
            b[j]=a[i]这样便能将重复的数据都删除
              }
  法二:for(i=0;i<=k;i++;k表示要删除的次数)
           {输入x为数据的下标;
                  for(j=x;循环从x开始)    
                     a[j-1]=a[j]  另前一个数等于后一个数    }
       for循环至n-k  printf输出
排序方法
  法一:选择排序法
       再数组中再到最小数,将min与a[0]交换
       再从a[1]到a[n-1]找min,a[1]与min交换
       ....以此类推
  法二:冒泡排序法
        从头到尾比较相邻两个数
        运用两层循环,若a[j]>a[j+1],交换两个数的值

Case array enumeration method: sequential search method, one by comparing elements in an array to find duplicate data

哈希数组案例:查找重复数据
              定义一个数组static int hash[max]该数组里的数都为0
              for循环输入数据
              if(hash[用输入的数据作下标])
              {if(hash[x]==1)说明有重复数据
                 else让hash[x]=1   }

1.2 This chapter learning experience

1. array of content and more complex, especially some random array of characters, and many ways to mess too easy, more difficult to use
2. amount of about 700 lines of code to achieve the goal

2.PTA lab assignments

2.1 Topic name: binary search

2.1.1 pseudocode

Define an array a [10] = {1,2,3,4,5,6,7,8,9,10}
is defined as the upper array index lower left, top right lower standard;
int B is an intermediate value, Key
Scanf an input key
for {
b = (Upper + Top) / 2 alternative b is an intermediate value
comparison a [b] the size of the key is
, if equal, printf output b
if it exceeds, top = b-1
, if less than, Upper = b +. 1
}

2.1.2 Code Screenshot

2.1.3 build test data

Input data Output Data Explanation
4 weizhi: 3 Topics requirements data
1 weizhi: 0 Endpoint data
11 not found There is no data in the array

2.1.4 PTA submit a list and description

  • First commit errors due to find a [b] = a break after the end of the cycle there is no key, so that operation times out, and when a [b]> key and when a [b] <key operation error following the time
  • Submit the modified second error break error, but when the operation after a [b]> key and when a [b] <key error still
  • Modify a [b]> key, top = b-1, a [b] <key, upper = b + 1, the answer is correct

    2.2 Title name: string into decimal integer

    2.2.1 Data Processing

    Definition of the variable i, k, j; characters are defined character array a [100] to store the input, defined b [100] to store the hexadecimal character is defined as the number after the decimal conversion, defined flag to control whether the output negative

    2.2.2 Code Screenshot


    2.2.3 build test data

    Input data Output Data Explanation
    + -P-XF4 -1 +! # -3905 Topics requirements data
    10# 16 Full digital data
    from# 171 All-lowercase data
    FROM# 171 All capital letters data

    2.2.4 PTA submit a list and description

  • Because if there is no notice should be output negative sign which led to the first test point error
  • After the flag is defined to control whether to output or minus error, the error code is as follows
    IF (I> = flag == 0 &&. 1)
    {
    for (J =. 1-I; J> = 0; J,)
    {
    IF (A [J ] == '-')
    {
    In Flag =. 1;
    BREAK;
    }
    }
    }
    this code is not only take into account the negative sign before the first hexadecimal character, and if that error before the first non-character hexadecimal negative sign, while a minus sign before the second hexadecimal characters, or will output a negative number, so the error
  • After the fill flag = 2 do not forget flag = 1 or 0 output in output minus
  • After modifying the above code is correct,

    2.3 Topic 3 Name: E-mail address to determine the legality

    2.3.1 Data Processing

    Characters are defined character array str [100] to store the input, the output of the definition flag "YES" or "NO" is determined as defined i, i for loop

    2.3.2 Code Screenshot


    2.3.3 build test data

    Input data Output Data Explanation
    [email protected] YES Topics requirements data
    #[email protected] NO Non-alphanumeric input, underline, and '.' @ Symbol
    102q@_12.com NO @ Enter letters and numbers before and after the failure of data
    [email protected] NO There are other data after com

    2.3.4 PTA submit a list and description

  • Segment error is due to input characters using scanf, after the switch to fgets
  • Partially correct 1: Since the condition str [i]> 'a' and str [i] <z does not equal sign
  • Partially correct 2: There are no data on the determination com, add the following code:
    the else IF (STR [I +. 1] && 'C' && STR [I + 2] == 'O' && STR [I +. 3] == 'm')
    {
    I = I +. 4;
    ! IF (STR [I] = '\ 0')
    {
    In Flag = 0;
    BREAK;
    }
    }
  • Partially correct 3: After an error still modify the code, debugging find '\ 0' before there is a data character '\ n-', is automatically added when fgets input, i = i + 4 to i = i + 5
  • Finally submit correct

    3. Read Code


    Entitled output array A and the array B has no data, the code cycles less advantage, the idea is simple, easy to understand
    and identify data has iterate through A and B, change and A [i] values 1, the final choice of the output value a [i] is not equal to the data 1 is
    much longer than is provided for storing an array of data by a convenient

Guess you like

Origin www.cnblogs.com/sym2446/p/11815729.html