3 Star 61 Fork 5

programmercarl / kamacoder-solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0062.平方差.md 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
huanheart 提交于 2024-02-28 23:18 . Update 0062.平方差.md

62. 平方差

题目链接

C

C++

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int l,r;cin>>l>>r;
    unordered_map<int,int> map;
    int ans=0;
    for(int y=0;y<=1000;y++)
    {
        for(int z=0;z<=1000;z++)
        {
            int x=y*y-z*z;
            if(x>=l&&x<=r&&map[x]==0)
            {
                // cout<<x<<endl;
                map[x]=1;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
//这里的整数只考虑0以及正整数

Java

import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int L = scan.nextInt();
    int R = scan.nextInt();
    int ans = 0;
    for (;L<=R;L++) {
      if (L % 2 != 0 || L % 4 == 0) {
        ans++;
      }
    }
    System.out.println(ans);
  }
}

Python

# x = y^2 - z^2 = (y-z)(y+z)  x和y-z和y+z奇偶相同
l,r = map(int,input().split())
ans = 0
# 当x为奇数,都可以满足
ans += (r+1)//2 - l//2
# 当x为偶数,需要是4的倍数
ans += r//4 - (l-1)//4
print(ans)

JS

Go

1
https://gitee.com/programmercarl/kamacoder-solutions.git
git@gitee.com:programmercarl/kamacoder-solutions.git
programmercarl
kamacoder-solutions
kamacoder-solutions
main

搜索帮助