首页 > 其他分享 >数组

数组

时间:2022-12-16 20:45:35浏览次数:27  
标签:10 数组 int C语言 stud printf

数组

定义和引用一维数组

c语言的数据类型:

  • 基本类型:

    • 整型类型:基本整型(int)、短整型(short int)、长整型(long int)、双长整型(long long int)、字符型(char)、布尔型(bool)
    • 浮点类型:单精度浮点型(float_complex)、双精度浮点型(double_complex)、复数浮点型(float_complex,double_complex,long long_complex)
  • 枚举类型(enum)

  • 空类型(void)

  • 派生类型:指针类型(*)、数组类型([ ])、结构体类型(struct)、共用体类型(union)、函数类型

表示同一类性质的数据可以使用数组来表示

例如:一个班有10名学生,计算10名学生的平均成绩

int stdu_1 = 98
int stdu_2 = 99
...
int stdu_10 = 91
(stdu_1 + ... + stdu_10)/30

如何定义一维数组

int stud[10] 定义了数组后,在内存中提取出一片空间,用来存放有10个整型元素(使用visual C++会开辟4*10=40字节的空间)

用一个 int stud[10] 就相当于定义了10个简单的整型变量,非常简洁方便

定义一维数据的一般形式:

类型说明符  数组名[常量表达式]
  • 数组名的命名规则和变量名的相同,遵守标识符命名规则

  • 在定义数组时,需要指定数组中元素的个数,方括号中的常量表达式用来表示元素的个数,即数组的长度。例如,指定stud[10],表示stud数组有10个元素。注意下标是从0开始的,这10个元素是stud[0], stud[1], stud[2], stud[3], stud[4], stud[5], stud[6], stud[7], stud[8], stud[9]。特别注意:没有元素stud[10]

  • 常量表达式中可以包括常量和符号常量,如int stud[3+5];但不能包含变量,如int stud[n]是不合法的。也就是说C语言不允许对数组的大小做动态定义,即数组的大小不依赖于程序运行过程中变量的值

一维数组的初始化

在定义数组的同时,给各数组元素赋值,称为数组的初始化

  • 在定义数组时,对全部数组元素赋予初值

    • int stud [10]={98,99,97,96,95,97,93,90,91,99}

    • 初始化之后,相当于stud[0] =98, stud[1] =99, stud[2] =97, stud[3] =96, stud[4] =95,stud[5] =97, stud[6] =93, stud[7] =90, stud[8] =91, stud[9] =99

  • 可以只给数组中的一部分元素赋值

    • int stud[10] ={98,99,97,96}
    • 定义stud数组有10个元素,但{ }内只提供了4个初值,表示只给前面4个元素赋初始值,系统 自动给后6个元素赋初值为0;相当于stud[0] =98, stud[1] =99, stud[2] =97, stud[3] =96,stud[4] =0,stud[5] =0, stud[6] =0, stud[7] =0,stud[8] =0, stud[9] =0
    • 数值型未被赋值的初始化为0,如果是字符型数组,则初始化为’\0’
  • 如果想使一个数组中全部元素值为0,可以写成

    • int stud[10] = {0,0,0,0,0,0,0,0,0,0} 或 int stud[10] = {0}
  • 在对全部数组元素赋初始值时,由于数据的个数已经确定,因此可以不指定数组长度

    • 例如:int stud[10] = {98,99,97,96,95,97,93,90,91,99} 或 int stud[] ={98,99,97,96,95,97,93,90,91,99}

    • 虽然没有在[ ]指定数组的长度,但是系统会根据花括号的个数确定stud数组有10个元素

如何使用一维数组元素

引用数组元素的方法为:

数组名[下标]

下标可以是整型常量整型表达式

注意:定义数组时用到的 数组名[常量表达式] 和引用数组元素时用的 数组名[小标] 形式相同,但是含义不同

例如:

 int a ; 
 int b[3]={...};	//前面有int,是定义数组,指定义一个包含3个元素的数组
 a = b[2]		//b[2]表示引用b数组中序号为2的元素 

示例:

#include "stdio.h"
void main()
{
    int stud[10] = {98, 99, 97, 96, 95, 97, 93, 90, 91, 99};
    printf("%d\n", stud[5]);	//printf("%d\n",stud[2+3]);
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
97

[Done] exited with code=3 in 0.772 seconds

1、对10个数依次赋值为1,2,3,4,5,6,7,8,9并以逆序的方式进行输出

示例:

#include "stdio.h"
void main()
{
    int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (int i = 8; i >= 0; i--)
        printf("%d\t", num[i]);
    printf("\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
9	8	7	6	5	4	3	2	1	

[Done] exited with code=10 in 0.19 seconds

2、斐波那契数列

示例:

#include "stdio.h"
void main()
{
    int s[30] = {1, 1};
    for (int i = 2; i < 30; i++)
        s[i] = s[i - 2] + s[i - 1];
    for (int i = 0; i < 30; i++)
        printf("%d\t", s[i]);
    printf("\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
1	1	2	3	5	8	13	21	34	55	89	144	233	377	610	987	1597	2584	4181	6765	10946	17711	28657	46368	75025	121393	196418	317811	514229	832040	

[Done] exited with code=10 in 0.182 seconds

二维数组

如何定义二维数组

二维数组定义的一般形式:

类型说明符  数组名[常量表达式][常量表达式]   

例如:

int stud[10][3]		//10*3的数组,包含30个int类型空间
float a[3][4]		//3*4的数组,包含12个float类型空间
float b[5][10]		//5*10的数组,包含50个float类型空间

扩展三维

c语言还允许使用多维数组

例如:

int	c[3][4][5]		//3*4*5的三维数组,包含60个int类型空间

二维数组初始化

使用花括号中包含花括号的形式对二维数组进行初始化

  • 分行给二维数组赋初值

    int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    1	2	3	4
    5	6	7	8
    9	10	11	12
    
  • 将所有数据写在一个花括号内,按数组元素在内存中的排列顺序对各元素赋初值

    int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    

    缺点:数据多时,则会写成一大片,难以检查,书写容易遗漏

  • 可以对部分元素赋初值

    int a[3][4]={{1},{5},{9}};
    //对各行第1列(序号为0的列)的元素赋初值,其余元素值补0
    1	0	0	0
    5	0	0	0
    9	0	0	0
    
    int a[3][4]={{1},{0,6},{0,0,11}}
    1	0	0	0
    0	6	0	0
    0	0	11	0
    
    int a[3][4]={{1},{5,6}}
    1	0	0	0
    5	6	0	0
    0	0	0	0
    
  • 如果对全部元素都赋值,则定义数组时对第1维的长度可以不指定,但第2维的数据不能省

    int a[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
    = int a[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
    

    系统会根据数据总个数和第2维的长度算出第1维的长度

使用二维数组的元素

引用二维数组元素的方法为:

数组名[下标][下标]

在引用数组元素时,下标值应在已定义的数组大小的范围内

例如:

int a[3][4]...	//定义a为3*4的二维数组
...
a[3][4] = 3;	//不存在a[3][4]
//因为a可用的行下标范围为0~2 ,列下标的范围为0~3

示例:

#include "stdio.h"
void main()
{
    int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    a[3][4] = 20;
    printf("%d\n", a[3][4]);
    for (int x = 0; x < 3; x++)
        for (int y = 0; y < 4; y++)
            printf("%d\t", a[x][y]);
    printf("\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
20
1	2	3	4	5	6	7	8	9	10	11	12	

[Done] exited with code=10 in 0.194 seconds

示例:将一个二维数组行和列的元素互换,存到另一个二维数组中

\[a=\left[ \begin{matrix} 1 & 2 & 3 \\4 & 5 & 6\end{matrix} \right] \]

\[b=\left[ \begin{matrix} 1 & 2 \\ 3 & 4 \\ 5 & 6\end{matrix} \right] \]

#include "stdio.h"
void main()
{
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}}, b[3][2];
    int x, y;
    for (x = 0; x <= 1; x++)
    {
        for (y = 0; y <= 2; y++)
        {
            printf("%5d", a[x][y]);
            b[y][x] = a[x][y];
        }
        printf("\n");
    }
    printf("\n");
    for (x = 0; x <= 2; x++)
    {
        for (y = 0; y <= 1; y++)
            printf("%5d", b[x][y]);
        printf("\n");
    }
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    1    2    3
    4    5    6

    1    4
    2    5
    3    6

[Done] exited with code=10 in 0.203 seconds

字符数组

定义字符数组

形式:

char  字符数组名[常量表达式]

例如:

char  a[10];

字符数组的初始化

char b[10]={'g','o','o','d',' ','t','i','m','e','.'};
//把10个字符一次赋给 b[0] ~ b[9] 这10个元素
  • 由于字符型数据是以整数形式(ASCII代码)存放的,因此也可以用整型数组来存放字符数据

    例如:

    char	b[10]={'g','o','o','d',' ','t','i','m','e','.'};
    int	c[10]={'g','o','o','d',' ','t','i','m','e','.'};	//会浪费空间
    
  • 如果定义了字符数组但未进行初始化,则数组各元素的值是不可预料的

    示例:

    #include "stdio.h"
    void main()
    {
        int i;
        char b[10];
        for (i = 0; i < 10; i++)
            printf("%5d", b[i]);
        printf("\n");
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
      -80    0    0    0    0    0   16    0    0    0
    
    [Done] exited with code=10 in 0.174 seconds
    
  • 如果花括号中提供的初值个数大于数组长度,则出现语法错误

    例如:

    char	b[10]={'g','o','o','d',' ','t','i','m','e','.','s'};
    //定义的十个元素,但是有11个初始化值
    
  • 如果初值个数小于数组长度,则后面未被赋值的元素自动补为’ \0 ’

    char  b[10]={'g','o','o','d',' ','t','i'};
    g	o	o	d	空格	t	i	\0	\0	\0
    
  • 如果数组的元素都是确定的,定义字符数组的常量表达式可以省略

    例如:

    char  b[]={'g','o','o','d',' ','t','i','m','e','.'};
    
  • 也可以定义二维字符数组及多维数组

    例如:

    char b[5][5]={{' ',' ','*'},{' ','*',' ','*'},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'}};
    

字符数组的使里

一维形式:

数组名[下标]

示例:

#include "stdio.h"
void main()
{
    int i;
    char b[10] = {'g', 'o', 'o', 'd', ' ', 't', 'i', 'm', 'e', '.'};
    for (i = 0; i < 10; i++)
        printf("%c", b[i]);
    printf("\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
good time.

[Done] exited with code=10 in 0.211 seconds

二维形式:

数组名[下标][下标]

示例:输出菱形图

#include <stdio.h>
void main()
{
    char b[5][5] = {{' ', ' ', '*'}, {' ', '*', ' ', '*'}, {'*', ' ', ' ', ' ', '*'}, {' ', '*', ' ', '*'}, {' ', ' ', '*'}};
    int i, j;
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 5; j++)
            printf("%c", b[i][j]);
        printf("\n");
    }
}

结果:

	*
  *	  *
*		*
  *	  *
  	*
  	
Press any key to continue

字符串和字符串结束标志

在C语言中,字符串使用字符数组来使用。字符串中的字符是逐个存放到数组元素中的

使用字符串常量来使字符数组初始化的方法:

例如:

char c[10] = {"good time."}
char c[] = {"good time."}
char c[] = "good time."

示例:

#include "stdio.h"
void main()
{
    char a[10] = {"good time."};
    char b[] = {"good time."};
    char c[] = "good time.";
    for (int i = 0; i < 10; i++)
        printf("%c", a[i]);
    printf("\n");
    for (int i = 0; i < 10; i++)
        printf("%c", b[i]);
    printf("\n");
    for (int i = 0; i < 10; i++)
        printf("%c", c[i]);
    printf("\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
good time.
good time.
good time.

[Done] exited with code=10 in 0.186 seconds

:在初始化定义后,系统自动在字符串常量后面加上一个’ \0 ’,让计算机能够知道遇到字符’ \0 ’时,表示字符串结束。并且char c[] = {“good time.”}的长度为11个字符

优点:这种定义字符串的方法更加直观、方便、符合人们的习惯

char c[] = "good time."
等价于 c[] ={'g','o','o','d',' ','t','i','m','e','.','\0’}
不等价于 c[] ={'g','o','o','d',' ','t','i','m','e','.'}

字符数组并不要求它的最后一个字符为‘ \0 ’,甚至可以不包含‘ \0 ’,

字符数组的输入输出

字符数组的输入输出有2种方式

  1. 逐个字符输入输出。用格式符“ %c ”输入或输出一个字符

    示例:

    #include "stdio.h"
    void main()
    {
        char a[] = {"good time."};
        for (int i = 0; i < 10; i++)
        {
            printf("%c", a[i]);
        }
        printf("\n");
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    good time.
    
    [Done] exited with code=10 in 0.183 seconds
    
  2. 将整个字符串一次输入或输出。用“ %s ”格式符,意思是对字符串的输入输出

    示例:

    #include "stdio.h"
    void main()
    {
        char a[] = {"good time."};
        printf("%s\n", a);
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    good time.
    
    [Done] exited with code=0 in 0.185 seconds
    

:遇到 \0 就停止输出

说明:

  • 输出的字符中不包含结束符‘ \0 ’

  • 用“ %s ”格式符输出字符串时,printf函数输出项是字符数组名,而不是数组元素名

  • 如果数组长度大于字符串的实际长度,也只输出 \0 前面的数据

    示例:

    #include "stdio.h"
    void main()
    {
        char a[20] = {"good time."};
        printf("%s\n", a);
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    good time.
    
    [Done] exited with code=0 in 0.188 seconds
    
  • 如果一个字符数组中包含一个以上’ \0 ’,则遇第一个‘ \0 ’时输出就结束

    示例:

    #include "stdio.h"
    void main()
    {
        char a[] = {'g', 'o', 'o', 'd', ' ', '\0', 't', 'i', 'm', 'e', '.'};
        for (int i = 0; i < 10; i++)
        {
            printf("%c", a[i]);
        }
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    good 
    [Done] exited with code=101 in 0.203 seconds
    
  • 可以使用scanf函数输入一个字符串,多个字符串输入,可使用空格分隔

    • 注:C语言中数组名代表该数组第一个元素的地址。不需要再加地址符

    示例:

    /*单个字符数组*/
    #include "stdio.h"
    void main()
    {
        char a[];
        scanf("%s", a);
        printf("%s\n", a);
    }
    
    /*多个个字符数组*/
    #include "stdio.h"
    void main()
    {
        char a[], b[];
        scanf("%s%s", a, b);
        printf("%s", a);
        printf("%s", b);
    }
    

    结果:

    /*单个字符数组*/
    hello world		//此行为输入内容
    hello world		//此行为输出内容
    
    Press any key to continue
    
    /*多个个字符数组*/
    hello world		//此行为输入内容
    hello
    world			//此行与上一行为输出内容
    
    Press any key to continue
    
  • 在之前的字符数组中,如果采用 %s 输出,会出现异常的结尾信息,由于找不到 \0

    示例:

    #include "stdio.h"
    void main()
    {
        char a[5] = {'h', 'e', 'l', 'l', 'o'};
        printf("%s\n", a);
    }
    

    结果:

    [Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
    hello��
    
    [Done] exited with code=0 in 0.18 seconds
    

使用字符串处理函数

C语言提供了一些用来专门处理字符串的函数

puts 函数(输出字符串的函数)

形式:

puts(字符数组)

示例:

#include "stdio.h"
void main()
{
    char a[20] = "hello world";
    puts(a);
    puts(a);
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
hello world
hello world

[Done] exited with code=0 in 0.183 seconds

作用:将一个字符串(以\0结束的字符序列)输出到终端。puts输出时将字符串结束标志 \0 转换为 \n

gets 函数(输入字符串的函数)

形式:

gets(字符数组)

示例:

#include "stdio.h"
void main()
{
    char a[20];
    gets(a);
    printf("%s\n", a);
}

结果:

hello world
hello world

Press any key to continue

作用:从终端输入一个字符串到字符数组

:用 puts 和 gets 函数只能输入或输出一个字符串,不能写成 puts(数组1,数组2),gets(字符串1,字符串2)

strcat 函数(字符串连接函数)

形式:

strcat(字符数组1,字符数组2)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello ", b[] = "world";
    printf("%s\n", strcat(a, b));
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
hello world

[Done] exited with code=0 in 0.203 seconds

作用:把字符串2连接到字符串1的后面,结果放在字符数组1中

注:

  • 字符数组1必须足够大,以便容纳新字符串
  • 连接时会将字符串1后面的 \0 取消,只在新字符串最后保留一个 \0

graph TD c语言本身的语法 --> 函数库 --> 代码... c语言本身的语法 --> 代码...

#include<函数库名>

在使用字符串处理函数时,应当在程序文件的开头用 #include <string.h>


strcpy 和 strncpy 函数(字符串赋值函数)

strcpy形式:

strcpy(字符数组1,字符串2)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello", b[] = "world";
    strcpy(a, b);
    printf("%s\n", a);
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
world

[Done] exited with code=0 in 0.193 seconds

作用:把字符串2复制到字符数组1中去

注:

  • 字符数组1必须足够大,以便容纳字符串2

  • 字符串2可以是字符数组名,也可以是一个字符串常量

    char a[] = "hello";
    strcpy(a, "world");
    printf("%s\n", a);
    
  • 不能用赋值语句将一个字符串常量或字符数组直接给一个字符数组。字符数组名是一个地址常量,是不能被改变的值

    char a[] = "hello";
    c = "world";			//c[0] = 'c'; 这种可以,但前者不行
    printf("%s\n", a);
    

strncpy 形式:

strncpy(字符数组1,字符串2,整数)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello", b[] = "world";
    strncpy(a, b, 3);
    printf("%s\n", a);
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
worlo		//hello前三个字符被替换为wor

[Done] exited with code=0 in 0.185 seconds

作用:将字符串2中最前面的整数个字符复制到字符数组1中

strcmp(字符串比较函数)

形式:

strcmp(字符串1,字符串2)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello", b[] = "helln";
    /*以下为错误写法*/
    // if (a > b)
    //     printf("a > b\n");
    // else
    //     printf("a < b\n");
    /*以下为正确写法*/
    if (strcmp(a, b) > 0)
        printf("a >= b\n");
    else
        printf("a < b\n");
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
a >= b

[Done] exited with code=0 in 0.186 seconds

作用:比较字符串1和字符串2,将两个字符串自左至右逐个字符相比(按ASCII码大小比较),直到出现不同的字符或遇到 \0 为止

规则:

  • 如果字符串1与字符串2相同,则函数返回值为0
  • 如果字符串1>字符串2,则函数值为一个正整数
  • 如果字符串1<字符串2,则函数值为一个负整数
  • 在英文字典中位置在后面的为‘大’

strlen函数(测量字符串长度函数)

形式:

strlen(字符数组)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello world";
    printf("%d\n", strlen(a));
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
11

[Done] exited with code=2 in 0.186 seconds

作用:测量字符数组长度的函数,不包括' \0 '

strlwr函数(转换为小写的函数)

形式:

strlwr(字符串)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "HELLO WORLD";
    printf("%s\n", strlwr(a));
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
hello world

[Done] exited with code=0 in 0.185 seconds

作用:将字符串中大写字母换成小写字母

strupr函数(转换为大写的函数)

形式:

strupr(字符串)

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char a[] = "hello world";
    printf("%s\n", strupr(a));
}

结果:

[Running] cd "d:\vscode(C语言)\" && gcc 数组.c -o 数组 && "d:\vscode(C语言)\"数组
HELLO WORLD

[Done] exited with code=0 in 0.177 seconds

作用:将字符串中小写字母换成大写字母

扩展

输入一行字符,统计其中有多少个单词,单词之间使用空格分开

示例:

#include "stdio.h"
void main()
{
    char word[];
    int i, sum = 0;
    gets(word);
    for (i = 0; word[i] != '\0'; i++)
        if (word[i] == ' ')
            sum++;
    sum += 1;
    printf("单词(或句子) %s 的个数为 %d 个\n", word, sum);
}

结果:

How are you
单词(或句子) How are you 的个数为 3 个

Press any key to continue

编写代码实现strcpy函数,将y中的字符全部赋值到x中

示例:

#include "stdio.h"
#include "string.h"
void main()
{
    char x[], y[];
    unsigned int i;
    gets(y);
    for (i = 0; i <= strlen(y); i++)
        x[i] = y[i];
    printf("%S", x);
}

结果:

hello
hello

Press any key to continue

标签:10,数组,int,C语言,stud,printf
From: https://www.cnblogs.com/ruoxianshi/p/16988248.html

相关文章

  • Magento导出所有分类ID和名称到数组中
    ​​//获取所有激活状态的分类模型集合​​​​$categories​​​​=Mage::getModel(​​​​'catalog/category'​​​​)->ge......
  • 力扣---918. 环形子数组的最大和
    给定一个长度为n的环形整数数组 nums ,返回 nums 的非空子数组的最大可能和 。环形数组 意味着数组的末端将会与开头相连呈环状。形式上,nums[i]的下一个元素是......
  • PHP 获取二维数组中某个key的集合
    本文为代码分享,也是在工作中看到一些“大牛”的代码,做做分享。具体是这样的,如下一个二维数组,是从库中读取出来的。代码清单: 1.$user=array(2.array(3.'id'......
  • 【LeeCode】53. 最大子数组和
    【题目描述】给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组是数组中的一个连续部分。​​​​https://leetcode.......
  • 力扣---53. 最大子数组和
    给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组是数组中的一个连续部分。示例1:输入:nums=[-2,1,-3,4,-1,2,1,-......
  • 字符输入流遍历读数据 使用字符数组容器 1216
    importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;publicclassTest4{publicstaticvoidmain(String[]args)throws......
  • 【LeeCode】2488. 统计中位数为 K 的子数组 -- 太难了
    【题目描述】给你一个长度为n的数组nums,该数组由从1到n的不同整数组成。另给你一个正整数k。统计并返回num中的中位数等于k的非空子数组的数目。注意:数组......
  • opencl实现图像旋转(二维数组)。
    改写自:https://zhuanlan.zhihu.com/p/451101452,该用零拷贝方案。1、host函数1#include<CL/cl.h>2#include<stdio.h>3#include<stdlib.h>4#include<......
  • 代码随想录-数组
    代码随想录数组数组--二分查找题目:力扣题目链接给定一个n个元素有序的(升序)整型数组nums和一个目标值target,写一个函数搜索nums中的target,如果目标值存在返回......
  • 集合转数组 toArray
    使用集合转数组的方法,必须使用集合的toArray(T[]array),传入的是类型完全一致、长度为0的空数组。反例:直接使用toArray无参方法存在问题,此方法返回值只能是Object[]......