Home > Back-end >  For help
For help

Time:12-02

Design program according to the order from big to small order output function f (a, b)=2 * a * a * b * b the minimum 100 function value and the corresponding values of two parameters, in which a and b are natural Numbers,
Requirements: (1) as a function of storage structure should be as far as possible to save space,
(2) the designed algorithm and time complexity of the process should be as small as possible,

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
#include

Struct data
{
int a;
int b;
};

Int my_cmp (const void * I, const void * j)
{
Struct data * d1=(struct data *) I;
Struct data * d2=(struct data *) j;
Int x=2 * d1 - & gt; A * d1 - & gt; A + d1 - & gt; B * d1 - & gt; b;
Int y=2 * d2 - & gt; A * d2 - & gt; A + d2 - & gt; B * d2 - & gt; b;
Return the x - y;
}

Int main (void)
{
Int I, j, value, c=0;
Struct data d [200];

for(i=1; i<=10; I++)
{
for(j=1; j<=20; J++)
{
D [c]. A=I;
D [c]. B=j;
C + +;
}
}

Tree (d., 200, sizeof (struct data), my_cmp);

For (c=0; C<100; C + +)
{
Value=https://bbs.csdn.net/topics/2 * d [c] [c]. A * d a + d [c]. [c]. B * d b;
Printf (" % d: % d \ \ t t t % d % d \ \ n ", c, d [c]. A, d [c]. B, value);
}

return 0;
}


Results:
 E: \ Workspace> TCC - run the demo. C 
0:1 1 3
1:1 2 6
2:2 1 9
3:1, 3, 11
4:2 2 12
5:2, 3, 17
6:1 April 18
7:3 1 19
8:2 3 22
9:2 April 24
10:1 5 27
11:3 3 and 27
12:33 4 1
13:2 5 33
14:3, 4, 34
15:4 2 36
16:1 6 38
17:4 3 41
18:3 5 43
19:6 44 2
20:4 4 48
21: five 51
122:1 7 51
23:3 6 54
24:5 2 54
2 July 25:57
26:4, 5 57
27:5 3 59
28:1 8 66
29:5 April 66
30:3 July 67
31:4 6 68
32:2 8 72
33: June 1 73
34:5 5 75
35: June 2, 76
36:6 3 81
37:4 July 81
38:3 8 82
39:1 9 83
40:5 6 86
41:6 4 88
42:2 September 89
43:4 8 96
44: June 5, 97
45:3 September 99
46:5, 7, 99
47: July 1, 99
48: July 2, 102
49:1 10 102
50: July 3, 107
51: June 6, 108
52:2 to 10 108
53:4 September 113
54: July 4 114
55:5 August 114
56:3 118
57:6 7 121
58: July 5, 123
59:1 11 123
60:2 11 129
61:8 1 129
62:5 September 131
63:8 2 132
64:4, 10, 132
65:7 June 134
66:6 8 136
67:8 March 137
68:3 November 139
69:8 April 144
12, 146, 70:1
71:7 July 147
72:10 150
73:2 12 152
74:8 5 153
75: June 9 153
76:4 November 153
77:7 8 162
78:3 12 162
79:9 1 163
80:8 June 164
81:9 2 166
82:5 November 171
83:9 3 171
84:1 13 171
85:6 to 10 172
86:4, 12, 176
87:2 13 177
88:8 July 177
89:9 April 178
90:7 September 179
91:3 13 187
92:9 5 187
93:8 August 192
94:6 November 193
95: May 12 194
96:7 to 10 198
14, 198, 97:1
98:9 6 198
99:10 1 201

CodePudding user response:

Can use the method of insertion sort side traverse a, b the result of the increasing f, save minimum 100 values
 int main () 
{
Int a, b, f [3], [100], t, I, j;
for (i=0; i<100; I++) f [2] [I]=3 * 100 * 100;
For (a=0; a<10; {+)
For (b=0; b<15. B++) {
T=a + 2 * a * b * b;
for (i=0; T> F [2] [I] & amp; & i<100; i++);//insert find insertion position
If (i<100) {
For (j=99; J> i; J -) {
F [0] [j]=f [0] [1];
F [1] [j]=f [1] [1];
F [2] [j]=f [2] [1];
}
F [0] [I]=a;
F [1] [I]=b;
F [2] [I]=t;
}
}
}
for (i=0; nullnullnullnullnullnull
  • Related