C语言,选择结构。输入两个闭区间,求他们的交集。

C语言,选择结构。输入两个闭区间,求他们的交集。,第1张

#include <stdio.h>

int main()

{

    int low1,up1,low2,up2

    printf("输入第一个闭区间:")

    scanf("%d,%d",&low1,&up1)

    printf("输入第二个闭区间:")

    scanf("%d,%d",&low2,&up2)

    printf("交集:")

    if(low1>up2 || low2>up1)

        printf("空集\n")

    else if(up1>=up2)

    {

        if(low1>low2)

            printf("[%d,%d]",low1,up2)

        else

            printf("[%d,%d]",low2,up2)

    }

    else if(up2>up1)

    {

        if(low2>low1)

            printf("[%d,%d]",low2,up1)

        else

            printf("[%d,%d]",low1,up1)

    }

}

你的格式不对。应该分成两列,A列和B列

A B

1 2

1 3

0 2

0 3

在A5单元格中输入公式:

=max(a1:a4)&"-"&min(b1:b4)

返回的结果是1-2,应该就是你要的交集区间吧?

用函数似乎可以解决,我帮你看看!

给你个VBA代码 ,适用方法简单说下 ,进入VBA编辑器,插入模块,复制代码进去,退回你的数据表格 然后运行代码即可!

Sub rr()

Dim i%, k%, s As String

For i = 2 To Range("b65536").End(xlUp).Row

For k = 2 To Range("b65536").End(xlUp).Row

If i <>k Then

If (Cells(k, 3) >Cells(i, 2) And Cells(i, 2) >Cells(k, 2)) Or (Cells(k, 3) >Cells(i, 3) And Cells(i, 3) >Cells(k, 2)) Or (Cells(k, 3) <Cells(i, 3) And Cells(i, 2) <Cells(k, 2)) Then

s = Cells(k, 1) &"," &s

End If

End If

Next

If Len(s) >0 Then

Cells(i, 4) = Left(s, Len(s) - 1)

Else

Cells(i, 4) = ""

End If

s = ""

Next

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11784431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存