热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

计算一个数组中可以表示为两个完美正方形之差的元素

计算一个数组中可以表示为两个完美正方形之差的元素原文:https

计算一个数组中可以表示为两个完美正方形之差的元素

原文:https://www . geeksforgeeks . org/count-可以表示为两个完美正方形之差的数组元素/

给定一个数组 arr[] ,任务是计算数组中可以用两个完美平方数之差的形式表示的元素个数。a^2 - b^2
例:

输入: arr[] = {1,2,3}
输出: 2
解释:
有两个这样的元素可以表示为
两个数的平方之差–
元素 1–1^2 - 0^2 = 1
元素 3–2^2 - 1^2 = 3
因此,这样的元素计数为 2。
输入: arr[] = {2,5,6}
输出: 1
解释:
这样的元素只有一个。即–
元素 5–3^2 - 2^2 = 9 - 4 = 5
因此,该元素的计数为 1。

方法:问题中的关键观察点是可以表示为两个数的平方差的数,当除以 4 时,绝不会得到 2 作为余数。
例如:

N = 4 => 4^2 - 0^2
N = 6 = >不能表示为6 \% 4 = 2
N = 8 =>3^2 - 1^2
N = 10 =>不能表示为10 \% 4 = 2

因此,迭代数组并计算数组中此类元素的数量。
下面是上述方法的实现:

C++


// C++ implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
#include <bits/stdc++.h>
using namespace std;
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
int count_num(int arr[], int n)
{
    // Initialize count
    int count = 0;
    // Loop to iterate
    // over the array
    for (int i = 0; i < n; i++)
        // Condition to check if the
        // number can be represented
        // as the difference of squares
        if ((arr[i] % 4) != 2)
            count++;
    cout << count;
    return 0;
}
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    count_num(arr, n);
    return 0;
}


Java 语言(一种计算机语言,尤用于创建网站)


// Java implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
class GFG{
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
static void count_num(int []arr, int n)
{
    // Initialize count
    int count = 0;
    // Loop to iterate
    // over the array
    for(int i = 0; i < n; i++)
    {
       // Condition to check if the
       // number can be represented
       // as the difference of squares
       if ((arr[i] % 4) != 2)
           count++;
    }
    System.out.println(count);
}
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
    count_num(arr, n);
}
}
// This code is contributed by AnkitRai01


Python 3


# Python3 implementation to count the
# number of elements in the array
# which can be represented as difference
# of the two elements
# Function to return the
# Count of required count
# of such elements
def count_num(arr, n):
    # Initialize count
    count = 0
    # Loop to iterate over the
    # array of elements
    for i in arr:
        # Condition to check if the
        # number can be represented
        # as the difference
        # of two squares
        if ((i % 4) != 2):
            count = count + 1
    return count
# Driver Code
if __name__ == "__main__":
    arr = [1, 2, 3]
    n = len(arr)
    # Function Call
    print(count_num(arr, n))


C


// C# implementation to count the
// number of elements which can be
// represented as the difference
// of the two square
using System;
class GFG{
// Function to count of such elements
// in the array which can be represented
// as the difference of the two squares
static void count_num(int []arr, int n)
{
    // Initialize count
    int count = 0;
    // Loop to iterate
    // over the array
    for(int i = 0; i < n; i++)
    {
        // Condition to check if the
        // number can be represented
        // as the difference of squares
        if ((arr[i] % 4) != 2)
            count++;
    }
    Console.WriteLine(count);
}
// Driver code
public static void Main(string[] args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    count_num(arr, n);
}
}
// This code is contributed by shivanisinghss2110


java 描述语言


<script>
    // Javascript implementation to count the
    // number of elements which can be
    // represented as the difference
    // of the two square
    // Function to count of such elements
    // in the array which can be represented
    // as the difference of the two squares
    function count_num(arr, n)
    {
        // Initialize count
        let count = 0;
        // Loop to iterate
        // over the array
        for (let i = 0; i < n; i++)
            // Condition to check if the
            // number can be represented
            // as the difference of squares
            if ((arr[i] % 4) != 2)
                count++;
        document.write(count);
        return 0;
    }
    let arr = [ 1, 2, 3 ];
    let n = arr.length;
    count_num(arr, n);
script>

Output: 

2


推荐阅读
  • Java 中 Writer flush()方法,示例 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文介绍了如何使用 Spring Boot DevTools 实现应用程序在开发过程中自动重启。这一特性显著提高了开发效率,特别是在集成开发环境(IDE)中工作时,能够提供快速的反馈循环。默认情况下,DevTools 会监控类路径上的文件变化,并根据需要触发应用重启。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
author-avatar
八戒走你
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有