USACO-Section1.4 airprog[枚举]

题目大意:

一个等差数列是一个能表示成a, a+b, a+2b,…, a+nb (n=0,1,2,3,…)的数列。

在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平方的数的集合,其中p和q为非负整数)S中长度为n的等差数列。

样例输入:

5
7

样例输出:

1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

题解:

先把可能的情况全部存下来,在用两次循环暴力搜索所有可能性,枚举所有情况。注意范围上限。

C++
/*
ID: mujinui1
PROG: ariprog
LANG: C++
*/
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;
int ma=0;
int main() {
    ifstream fin("ariprog.in");
    ofstream fout("ariprog.out");
    int n,m,count=0;
    fin>>n>>m;
    int a[70000];
    for(int p=0;p<=m;p++){
        for(int q=0;q<=m;q++){
            a[count++]=p*p+q*q;
        }
    }
//  for(int i=0;i<count;i++){
//      cout<<i<<" "<<a[i]<<endl;
//  }
for(int i=0;i<count-1;i++){
    for(int j=i;j<count;j++){
        if(a[i]>a[j]){
            int temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}

for(int i=1;i<=m*m;i++){
    for(int j=0;j<=m*m;j++){
        if(j+(n-1)*i>a[count-1]){
            break;
        }
        else{
            int flag=0;
            for(int len=0;len<n;len++){
              for(int k=0;k<count;k++){
                if(a[k]==j+i*len){
                    flag++;
                    break;
                }
              }
            if(flag==n){
                ma++;
                fout<<j<<" "<<i<<endl;
             }
           }    
        }
    }
}
if(ma==0){
    fout<<"NONE"<<endl;
}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值