雷劈数&幻方

实验报告

院系专业: 数据科学与计算机学院 软件工程

实验(1)题目:雷劈数 实验内容: 编写程序寻找八位数以内的所有雷劈数
实验步骤: 1.穷举 0~100000000 的所有数字 2.对每个数字, 从右边第一位开始分割, 直至最左一位, 并判断是否为雷劈数
实验结果:

8位雷劈数

代码:

#include<iostream>
using namespace std;
int main() {
    for (int i = 1; i < 100000000; i++) {
        int tmp = i;
        int pos = 0;
        while (tmp != 0) {
            pos++;
            tmp /= 10;
        }
        for (int j = 1,  p = 10; j < pos; j++, p *= 10) {
            int pre = i / p;
            int post = i % p;
            if (i == (pre + post)*(pre + post))
                cout << i <<endl;
        }
    }
}

实验(2)题目: magic square
实验内容: 编写程序, 读入一个正方形整数数组, 判断是否为幻方 编写程序, 生成一个边长为奇数的幻方
实验步骤: 1.把正方形数组封装成一个类 square 2.包含的成员方法有读入,打印,判断,生成幻方
实验结果:
课本输入例子
随机输入例子

代码:
main.cpp

#include"magicSquare.hpp"
#include <iostream>
using namespace std;
int main() {
    Square s;
    if (s.setSquare()) {
        cout << "the square you set is :" <<endl;
        s.printSquare();
        cout << "this square is" ;
        if (!s.isMagicSquare()) cout << " not";
        cout << " a magic square" << endl;
    } else {
        cout << "the input datas are invalid" <<endl;
    }  // test ismagic

    Square s2;
    cout << "please enter the size of magic square "<<endl;
    int a;
    cin >> a;
    s2.createMagicSquare(a);
    s2.printSquare();
    return 0;
}

magicSquare.hpp

#define MAGIC_SQUARE
#define MAXSIZE 100
class Square {
  public:
      Square();
      ~Square();
      bool setSquare();
      bool isMagicSquare();
      void createMagicSquare(int _size);
      void printSquare();
  private:
      int size;
      int square[MAXSIZE][MAXSIZE];
      int colSum(int col);
      int rowSum(int row);
      int dioSum();
};

#endif

magicSquare.cpp

#include"magicSquare.hpp"
#include <iostream>
#include <iomanip>
using namespace std;
Square::Square() {
    size = 0;
    for (int i = 0; i < MAXSIZE; i++) {
        for (int j = 0; j< MAXSIZE; j++) {
            square[i][j] = 0;
        }
    }
}

Square::~Square() {}

bool Square::setSquare() {
    cout << "please enter the size of the square : ";
    cin >> size;
    if (size <= 0) return false;
    cout << endl << "please enter the elements of the square : ";
    for (int i = 0; i < size; i++) {
        for (int j = 0; j< size; j++) {
            cin >> square[i][j];
        }
    }
    return true;
}

bool Square::isMagicSquare() {
    int jud = dioSum();
    if (jud == 0) return false;
    for (int i = 0; i < size; i++) {
        if (colSum(i) != jud || rowSum(i) != jud) return false;
    }
    return true;
}

void Square::createMagicSquare(int _size) {
    if (_size % 2 == 0) return;  // the size must be odd number
    size = _size;
    for (int i = 0; i < MAXSIZE; i++) {
        for (int j = 0; j< MAXSIZE; j++) {
            square[i][j] = 0;
        }
    }  // reset the square
    square[0][size/2] = 1;
    int column = size/2, row = 0;
    int lc = size/2, lr = 0;
    for (int i = 2; i <= size * size; i++) {
        if (lr == 0) row = size - 1;
        else row = lr - 1;
        if (lc == size - 1) column = 0;
        else column = lc + 1;
        if (square[row][column] == 0) {
            lc = column;
            lr = row;
        } else {
            lr++;
        }
        square[lr][lc] = i;
    }
}

void Square::printSquare() {
    if (size == 0) cout << "no square" << endl;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j< size; j++) {
            cout << setw(4) << square[i][j] << " ";
        }
        cout << endl;
    }
}

int Square::colSum(int col) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += square[i][col];
    }
    return sum;
}

int Square::rowSum(int row) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += square[row][i];
    }
    return sum;
}

int Square::dioSum() {
    int a = 0;
    int b = 0;
    for (int i = 0; i < size; i++) {
        a += square[i][i];
        b += square[i][size - i - 1];
    }
    if (a == b) return a;
    return 0;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值