栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

while双循环指针求两数组的交集

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

while双循环指针求两数组的交集

// See https://aka.ms/new-console-template for more information

using NumSharp;



//测试numsharp
int[,] ints = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

NDArray dArray2 = np.array(new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });

NDArray dArray1 = np.array(ints);

foreach (var item in dArray1)
{
    Console.WriteLine($"{item}");
}


//dArray1.ToArray();
int a = dArray1.size;
int[] b = dArray1.shape;
Console.WriteLine($"{dArray1[0][1]},{a},{b[0]},{b[1]}");

int[,] ints1 = new int[dArray1.shape[0], dArray1.shape[1]];

for (int i = 0; i < ints1.GetLength(0); i++)
{
    for (int j = 0; j < ints1.GetLength(1); j++)
    {
        ints1[i, j] = dArray1[i][j];
    }
}

for (int i = 0; i < ints1.GetLength(0); i++)
{
    for (int j = 0; j < ints1.GetLength(1); j++)
    {
        Console.WriteLine($"写入的值为:{ints1[i, j]}");
    }
}

//结果:






float[] c = new float[] { 1, 2, 3, 4, 5 };
float[] d = new float[] { 2F,4F, 5F, 6F, 7F, 8F };

float s = 0;
float e = 0;
float[] floatSection(float[] firstList, float[] secondList)
{
    var res = new List();
    // 如果有一个数组为空,直接返回交集数组为空
    if (firstList.Length == 0 || secondList.Length == 0)
        return res.ToArray();
    int i = 0, j = 0;

    // 两个数组同时遍历
    while (i < firstList.Length && j < secondList.Length)
    {
        // 取范围的起点中大值作为交集起点
        s = Math.Max(firstList[i], secondList[j]);
        // 取范围的终点中小值作为交集终点
        e = Math.Min(firstList[i], secondList[j]);
        //起点小于终点,加入结果
        if (s <= e) //交点
        {
            res.Add(s);
        }
        if (firstList[i] > secondList[j])
        {
            j++;
        }
        else
        {
            i++;
        }
    }
    return res.ToArray();
}

Console.WriteLine("=========交集============");
foreach (var item in floatSection(c, d))
{
    Console.WriteLine(item);
}






List students = new List
{
    new Student{Id = 1, Name = "aa",Score = "89"},
    new Student{Id = 2, Name = "bb",Score = "11"},
    new Student{Id = 3, Name = "ada",Score = "12"},
    new Student{Id = 4, Name = "cc",Score = "13"},
    new Student{Id = 5, Name = "ee",Score = "15"}
};
List rooms = new List
{
    new Room{Id = 1,RoomName = "vv",Score = "7"},
    new Room{Id = 2,RoomName = "zz",Score = "8"},
    new Room{Id = 3,RoomName = "bb",Score = "12"},
    new Room{Id = 4,RoomName = "nn",Score = "89"},
    new Room{Id = 5,RoomName = "ng",Score = "18"}
};

List stu1 = new List();
void test1()
{
    int i = 0;
    int j = 0;
    foreach (var item in students)
    {
        foreach (var item2 in rooms)
        {
            if (item.Score == item2.Score)
            {
                stu1.Add(item);
            }
        }
    }

    foreach (var item in stu1)
    {
        Console.WriteLine(item.Score);
    }
}
Console.WriteLine("=========遍历============");
test1();

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Score { get; set; }
}

public class Room
{
    public int Id { get; set; }
    public string RoomName { get; set; }
    public string Score { get; set; }
}


结果

1
2
3
4
5
6
2,6,2,3
写入的值为:1
写入的值为:2
写入的值为:3
写入的值为:4
写入的值为:5
写入的值为:6
=========交集============
2
4
5
=========遍历============
89
12

c语言指针:

#include 
#include
#include

struct Person
{
	char name[64];
	int age;
};

typedef struct Person_
{
	char name[64];
	int age;
} MyPerson;

//typedef struct  Person_ MyPerson; //别名
int* func()
{
	int a = 10; //栈上创建的变量
	return &a;
}

void Test01()
{
	int* p = func(); //执行func()就被释放了
	printf("%dn", *p);
	printf("%dn", *p);
}



int main()
{
	struct Person p1 = { "张三", 18 };
	MyPerson p2 = { "李四", 19 };

	

	typedef char* PCHAR;

	PCHAR p3, p4; //定义了2个指针

	//printf("%d", p2.age);
	//E:C++C++全套课2-阶段二 C高级编程1-第一章 C高级1-1 变量和内存分布 08

	
	printf("=================n");
	Test01();
}

C++ 知识

// CPPTest1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include 
#include 


int main1()
{
    short a = 10;
    int b = 10;
    short* p = &a;
    short** pp = &p;
    int* p1 = &b;
    p = &a;
    pp = &p;
    std::cout << "p的值是n"<“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

int main2()
{
    int a = 0x01020304;
    char* p1 = (char*)&a; 
    short* p2 = (short*)&a;
    int* p3 = &a;

    //int* p4; //野指针 p4 指向地址不清楚 int* p4 = &a; p4的地址一定要是定义过的
    /

template
T func(T a, T b)
{
    return a + b;
}

void test03()
{
    int a = 3;
    double b = 3.3;
    //func(a, b);
    std::cout << func(a, b) << std::endl;
}

int myPlus(int a, int b)
{
    return a + b;
}

template
T myPlus2(T a, T b)
{
    return a + b;
}

void Test()
{
    int a = 10;
    int b = 20;
    char c = 'a';
    myPlus(a, c);
    //myPlus2(a, c); //报错,函数模板不能直接进行隐式转换
    myPlus2(a, c); //如果隐式转换,必须加上参数列表/泛型
}

int main3()
{
    //const int a = 10;
    //a = 100; const不能修改
    // int* p = &a; c++不能

    //int a = 10;
    //int* p1 = &a;
    //int** p2 = &p1;
    //std::cout << **p2 << std::endl; //10

    int b[10] = {1,2,3,4,5,6,7,8,9,10};
    int* p3 = b; //等价于 = &b[0]; 首地址
    int* p4 = &b[0];

    std::cout << *p3 << std::endl; //1
    std::cout << *p4 << std::endl; //1

    int c[5] = { 1,2,3,4,5 };
    int* ptr = (int*)(&c + 1); //&c 整个数组的地址,c是c[0]的地址。 &c + 1 是c[5] 之后的未知地区
    int* ptr2 = (int*)(c + 1); //c+1是2的地址
    //*(a + 1)
    printf("======================n");
    printf("%d,%d,%d n", *(c + 1), *(ptr - 1),*(ptr2-1)); 
    //2,5,1
    //c 是c[0]的地址。
    //ptr-1 步长为1个int,刚好回到c数组的最后一个数字

    int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int* p = a;
    printf("======================n");
    for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
    {
        //printf("%d n", a[i]);
        //printf("%dn",*(p + i));
        printf("%dn", p[i]); //p[i] = *(p+i)
    }

    printf("=========dsds=============n");

    test03();
    return 0;
}

typedef struct node
{
    int data;
    struct node* next;
} Node;

int main()
{
    int a = 5;
    Node p1 = { 5 };

    // Node* p; 相当于野指针
    Node* p = &p1;
    p->data = 5;
    std::cout << p->data << std::endl;
    return 0;
}



//template //让编译器看到这句话后面紧跟的函数里有T,不要报错
//void mySwap(T& a, T& b)
//{
//    T tmp = a;
//    a = b;
//    b = tmp;
//}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/983291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号