-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
108 lines (90 loc) · 2.57 KB
/
test.cpp
File metadata and controls
108 lines (90 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <QApplication>
#include <QMainWindow>
#include <QTimer>
#include <QPainter>
#include <QVector>
#include <QPair>
#include <QDebug>
class GreedyChangeDemo : public QMainWindow
{
Q_OBJECT
public:
GreedyChangeDemo(QWidget* parent = nullptr) : QMainWindow(parent)
{
setWindowTitle("Greedy Change Demo");
resize(800, 600);
// 设置零钱面额
coinValues << 25 << 10 << 5 << 1;
// 设置需要找零的金额
targetAmount = 99;
// 初始化动画定时器
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &GreedyChangeDemo::nextStep);
// 开始动画
timer->start(1000);
}
protected:
void paintEvent(QPaintEvent* event) override
{
Q_UNUSED(event);
QPainter painter(this);
painter.fillRect(0, 0, width(), height(), Qt::white);
// 绘制零钱
painter.setPen(Qt::black);
for (int i = 0; i < coinValues.size(); ++i)
{
painter.drawRect(100 + i * 50, height() - 50, 40, 40);
painter.drawText(120 + i * 50, height() - 25, QString::number(coinValues[i]));
}
// 绘制目标金额
painter.setPen(Qt::red);
painter.drawText(width() / 2 - 40, height() - 100, "Target Amount: " + QString::number(targetAmount));
// 绘制当前找零的金额
painter.setPen(Qt::blue);
painter.drawText(width() / 2 - 40, height() - 75, "Current Amount: " + QString::number(currentAmount));
// 绘制已经找到的硬币
painter.setPen(Qt::darkGreen);
int x = 50;
int y = height() - 150;
for (int i = 0; i < currentCoins.size(); ++i)
{
painter.drawRect(x, y, 40, 40);
painter.drawText(x + 20, y + 25, QString::number(currentCoins[i]));
x += 50;
}
}
private slots:
void nextStep()
{
if (currentAmount == targetAmount)
{
// 找零完成,停止动画
timer->stop();
return;
}
for (int i = 0; i < coinValues.size(); ++i)
{
if (currentAmount + coinValues[i] <= targetAmount)
{
currentCoins.push_back(coinValues[i]);
currentAmount += coinValues[i];
break;
}
}
update();
}
private:
QTimer* timer;
QVector<int> coinValues;
QVector<int> currentCoins;
int targetAmount;
int currentAmount = 0;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
GreedyChangeDemo w;
w.show();
return a.exec();
}
#include "main.moc"