Elliot_Lin_洛谷B3957 [GESP202403 三级] 完全平方数(C语言)

文章讲述了如何解决一个编程问题,即在一个非负整数序列中找到满足和为完全平方数的下标对。通过使用嵌套循环和数学方法判断两数之和是否为完全平方数,统计并输出符合条件的组合数。
摘要由CSDN通过智能技术生成

[GESP202403 三级] 完全平方数

题目描述

小杨同学有一个包含 n n n 个非负整数的序列 A A A,他想要知道其中有多少对下标组合 < i , j > <i,j> <i,j> 1 ≤ i < j ≤ n 1 \leq i < j \leq n 1i<jn),使得 A i + A j A_i + A_j Ai+Aj 是完全平方数。

如果 x x x 是完全平方数,则存在非负整数 y y y 使得 y × y = x y \times y = x y×y=x

输入格式

第一行一个非负整数 n n n,表示非负整数个数。
第二入行包含 n n n 个非负整数 A 1 , A 2 , … A n A_1, A_2, \dots A_n A1,A2,An,表示序列 A A A 包含的非负整数。

输出格式

输出一行一个整数表示答案。

样例 #1

样例输入 #1

5
1 4 3 3 5

样例输出 #1

3

提示

对全部的测试数据,保证 1 ≤ n ≤ 1000 1 \leq n \leq 1000 1n1000 0 ≤ A i ≤ 1 0 5 0 \leq A_i \leq 10^5 0Ai105

参考代码

#include<stdio.h>
#include<math.h>
#define N 1000
int main(){
    int n, a[N], count = 0;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    // 判断
    for(int i = 0; i < n; i++){
        for(int j = i+1; j < n; j++){
            // 如果不是平方数,则浮点数x 强制转化为 整数
            int x = (int)sqrt(a[i]+a[j]);
            if(x*x == a[i] + a[j])
                count++; 
        }
    }
    printf("%d", count);
    return 0;
}

2024.5.6.18.39

  • 27
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:It appears the new module is installed correctly. I'm able to import cv2 in both Python versions. However neither recognise the new features the module is supposed to add. >>> cv2.SURF() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'SURF' >>> cv2.xfeatures2d.SURF() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'xfeatures2d' python opencv share improve this question editedSep 27 '14 at 12:20 asked Sep 26 '14 at 12:04 Robbert 3,158 3 24 37 Could you consider editing the original Question since the answer does not reflect the question in the title.– Elliot Woods Mar 2 '17 at 6:08 add a comment 4 Answers active oldest votes up vote 15 down vote accepted 。 引用\[2\]:I encountered this same issue. I'm using python 2.7.6 and OpenCv 3.0 with the additional non-free modules. I do have xfeatures2d present in available modules and can import it, however it was as though xfeatures2d didn't contain SIFT or SURF. No matter how I called them it was the same Error: 。 引用\[3\]:在使用python版的OpenCV进行特征提取时出现"AttributeError: module ‘cv2.cv2’ has no attribute ‘xfeatures2d_SIFT’ "错误 通过网上寻找解决方法,发现是当前opencv版本(4.5)不能使用sift、surf等特征匹配算法。后面使用opencv3.4.2.16和python3.7成功运行了,运行效果如下图所示。 。 问题: AttributeError: module 'cv2' has no attribute 'FREAK_create' 回答: 这个错误通常是由于OpenCV版本的问题引起的。根据引用\[3\]中的描述,当前版本的OpenCV(4.5)不支持FREAK_create等特征匹配算法。建议尝试使用较早版本的OpenCV,如OpenCV 3.4.2.16,并确保与Python 3.7兼容,以解决这个问题。 #### 引用[.reference_title] - *1* *2* [https://stackoverflow.com/questions/26059134/adding-modules-from-opencv-contrib-to-opencv](https://blog.csdn.net/u011854789/article/details/79556022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [( python )OpenCV特征匹配函数错误cv2.xfeatures2d_SURF.create()](https://blog.csdn.net/qq_44924694/article/details/126162476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值