开始之前
相比固定长度的Array,大家可能在编程的时候经常会使用
2024年08月03日
今天要做一个小测验,每个学生在100道题中,随机抽出一个题来,用到了下面这个函数
/// <summary>
/// 从数组中随机取出count个成员得到一个新数组
/// </summary>
public class GetRomdomMemberFromARR
{
/// <summary>
/// 从字符串数组中随机取出count个成员得到一个新数组
/// </summary>
/// <param name="arr">源数组</param>
/// <param name="count">个数</param>
/// <returns></returns>
public string[] G(string[] arr, int count)
{
Random rnd = new Random();
string[] newarray = arr.OrderBy(i => rnd.NextDouble()).Take(count).ToArray();
return newarray;
}
/// <summary>
/// 从int数组中随机取出count个成员得到一个新int数组
/// </summary>
/// <param name="arr">源数组</param>
/// <param name="count">个数</param>
/// <returns></returns>
public int[] G(int[] arr, int count)
{
Random rnd = new Random();
int[] newarray = arr.OrderBy(i => rnd.NextDouble()).Take(count).ToArray();
return newarray;
}
}
2024年08月03日
# include <iostream>
# include <ctime>
# include <cassert>
using namespace std
int * generateRandomArray(int n, int rangeL, int rangeR)
2024年08月03日
来自公众号:C语言与cpp编程
程序在编译、运行等各个过程中,不同性质的数据存放在不同的位置。动态内存是从堆上分配,也叫动态内存分配。程序员自己负责在何时释放内存。动态内存的生存期由程序员决定,使用非常灵活。
2024年08月03日
1、指针与字符串的关系
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int main0101()
{
//char ch[] = "hello wrold";
//char* p = ch;
//printf("%s\n", p);
//printf("%c\n", *p);
char ch[] = "hello wrold";//栈区字符串可以修改
char* p = "hello world";//数据区常量区字符串不可修改
char* p1 = "hello world";
printf("%p\n", p);
printf("%p\n",p1);
//ch[2] = 'm';
//p[2] = 'm';//err
//*(p + 2) = 'm';//err
printf("%s\n", p);
printf("%s\n", ch);
return 0;
}
int main0102 ()
{
//字符串数组
//指针数组 下4行
//char ch1[] = "hello";
//char ch2[] = "world";
//char ch3[] = "dabaobei";
//char* arr[] = { ch1,ch2,ch3 };
char* arr[] = { "hello","world","dabaobei" };//字符串数组
//for (int i = 0; i < 3; i++)
//{
// printf("%c\n", arr[i][0]);//打印3个字符串的首字母
//}
for (int i = 0; i < 3 - 1; i++)//字符串排序
{
for (int j = 0; j < 3 - 1 - i; j++)
{
if (arr[j][0] > arr[j + 1][0])//找首字符进行比较
{
char* temp = arr[j];//交换指针数组元素进行排序
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < 3; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}