HDU1407 测试你是否和LTC水平一样高

题目大意:给出一个num,计算方程x^2+y^2+z^2 = num的第一个正整数解(字典序),0 < num <= 10000。

方法参考了网上的博客,自己打了一波,发现还有很多不懂的地方。

方法1:直接对x、y、z枚举。
用goto跳出多重循环

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std;

int main() {
    int n;
    int pow2[105];
    for (int i = 1; i <= 100; i++) {
        pow2[i] = i*i;
    }
    while(!cin.eof() && cin >> n) {
        for (int a = 1; a <= 100; a++) {
            for (int b = 1; b <= 100; b++) {
                for (int c = 1; c <= 100; c++) {
                    if (pow2[a] + pow2[b] + pow2[c] == n) {
                        cout << a << " " << b << " " << c << endl;
                        goto end;
                    }
                }
            }
        }
        end:;
    }

    return 0;
}

方法2:对x、y枚举,对z使用二分查找。
分清楚m和a[m],勿乱。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std;

int binSearch(int* a, int tar) {
    int l = 1;
    int r = 101;
    while(l < r) {
        int m = l + (r - l) / 2; //写成int m = (l + r) / 2可能会爆
        if      (a[m] == tar) return m;
        else if (a[m] <  tar) l = m + 1;
        else                  r = m;
    }
    return -1;
}

int main() {
    int n;
    int pow2[105];
    for (int i = 1; i <= 100; i++) {
        pow2[i] = i*i;
    }
    while(!cin.eof() && cin >> n) {
        for (int a = 1; a <= 100; a++) {
            for (int b = 1; b <= 100; b++) {
                int c = binSearch(pow2, n - pow2[a] - pow2[b]);
                if (c != -1) {
                    cout << a << " " << b << " " << c << endl;
                    goto end;
                }
            }
        }
        end:;
    }

    return 0;
}

方法3:对x、y枚举,对z用hash查找。
普通数组:用key找value。hash:用value找key。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <malloc.h>
using namespace std;

struct Hash {
    bool exist;
    int key;
};
Hash h[10001]; //很多头文件都有hash,所以改成了h

int main() {
    int pow2[105];
    for (int i = 1; i <= 100; i++) {
        pow2[i] = i*i;
        h[pow2[i]].exist = true;
        h[pow2[i]].key = i;
    }
    int n;
    while(~scanf("%d", &n)) {
        for (int a = 1; a <= 100; a++) {
            for (int b = 1; pow2[a] + pow2[b] <= n; b++) { //b <= 100写法错误,id会小于0
                int id = n - pow2[a] - pow2[b];
                if(h[id].exist) {
                    printf("%d %d %d\n", a, b, h[id].key);
                    goto end;
                }
            }
        }
        end:;
    }

    return 0;  
} 

转载于:https://www.cnblogs.com/Metak/p/6128939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值