求助

B3957 [GESP202403 三级] 完全平方数

qsbb @ 2024-08-27 19:48:00

#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000010];
int main(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    int num = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j--){
            int k = sqrt(a[i] + a[j]);
            if(a[i] + a[j] == k * k){
                num ++;
            }
        }
    }
    cout << num;
}

by kanqiqin @ 2024-08-27 19:53:15

#include <bits/stdc++.h>
using namespace std;

int a[1010];

bool pf(int x)
{
    if((int)sqrt(x) * (int)sqrt(x) == x)
    {
        return true;
    }
    return false;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j < i; j++)
        {
            if(pf(a[i] + a[j]))
            {
                cnt++;
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

by meifan666 @ 2024-08-27 19:53:22

@just01 1<=i<j<=n,你的j初始化为i+1


by kanqiqin @ 2024-08-27 19:53:47

球关


by meifan666 @ 2024-08-27 19:53:57

@just01 还有,要判断到n


by Dress @ 2024-08-27 19:56:32

@just01 已AC求关QWQ

#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000010];
int main(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    int num = 0;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){//j = 0 -> j = i + 1  j-- -> j++
            int k = sqrt((int) a[i] + a[j]);//sqrt(a[i] + a[j]) -> sqrt((int) a[i] + a[j])
            if(sqrt(a[i] + a[j]) == k){//a[i] + a[j] == k * k -> sqrt(a[i] + a[j]) == k
                num ++;
            }
        }
    }
    cout << num;
}

AC记录


|