2012-03-07 ECNU 編程實踐考試 我的題解
晚上有同學找我要題解,我就干脆做了一下題目,希望能有些幫助。 1
/*
2
Copyright (C) 2012, coreBugZJ, all rights reserved.
3
4
2012-03-07 ECNU 編程實踐考試
5
*/
6
7
8
/************************************************************
9
四元一次方程
10
枚舉即可
11
*/
12
/*
13
#include <stdio.h>
14
#include <string.h>
15
16
#define L 1003
17
18
int solve( int n ) {
19
int w, x, y, ans = 0;
20
21
for ( w = 0; 4*w <= n; ++w ) {
22
for ( x = 0; 4*w+3*x <= n; ++x ) {
23
for ( y = 0; 4*w+3*x+2*y <= n; ++y ) {
24
++ans;
25
}
26
}
27
}
28
29
return ans;
30
}
31
32
int main() {
33
int t, n;
34
scanf( "%d", &t );
35
while ( t-- > 0 ) {
36
scanf( "%d", &n );
37
printf( "%d\n", solve(n) );
38
}
39
return 0;
40
}
41
*/
42
43
44
/************************************************************
45
Search Web Pages
46
排序即可
47
*/
48
/*
49
#include <stdio.h>
50
#include <string.h>
51
52
#define L 103
53
#define N 23
54
55
struct __Node
56
{
57
char url[ L ];
58
int vi;
59
};
60
typedef struct __Node Node;
61
62
Node node[ N ];
63
64
int main() {
65
int n, i, j;
66
Node tmp;
67
68
scanf( "%d", &n );
69
for ( i = 0; i < n; ++i ) {
70
scanf( "%s%d", node[ i ].url, &(node[ i ].vi) );
71
}
72
73
for ( i = 0; i < n; ++i ) {
74
for ( j = i+1; j < n; ++j ) {
75
if ( node[ i ].vi < node[ j ].vi ) {
76
tmp = node[ i ];
77
node[ i ] = node[ j ];
78
node[ j ] = tmp;
79
}
80
}
81
}
82
83
for ( i = 0; i < n; ++i ) {
84
if ( node[ i ].vi == node[ 0 ].vi ) {
85
puts( node[ i ].url );
86
}
87
}
88
89
return 0;
90
}
91
*/
92
93
94
/************************************************************
95
整數的質因子分解
96
素數篩法
97
*/
98
/*
99
#include <stdio.h>
100
#include <string.h>
101
102
#define N 20003
103
104
int prime[ N ], nprime;
105
106
void init() {
107
int i, j;
108
memset( prime, 0, sizeof(prime) );
109
nprime = 0;
110
for ( i = 2; i < N; ++i ) {
111
if ( 0 == prime[ i ] ) {
112
prime[ nprime++ ] = i;
113
for ( j = i+i; j < N; j+=i ) {
114
prime[ j ] = 0;
115
}
116
}
117
}
118
}
119
120
void solve( int a ) {
121
int i, p, e;
122
i = 0;
123
while ( 1 < a ) {
124
p = prime[ i ];
125
e = 0;
126
while ( a % p == 0 ) {
127
++e;
128
a /= p;
129
}
130
if ( 0 < e ) {
131
printf( "(%d,%d)", p, e );
132
}
133
++i;
134
}
135
printf( "\n" );
136
}
137
138
int main() {
139
int t, a;
140
init();
141
scanf( "%d", &t );
142
while ( t-- > 0 ) {
143
scanf( "%d", &a );
144
solve( a );
145
}
146
return 0;
147
}
148
*/
149
150
151
/************************************************************
152
行數據的排序
153
逆字典序排序
154
注意 0≤ai≤109 中 109 表示 10 的 9 次方!
155
*/
156
/*
157
程序二
158
*/
159
/*
160
#include <stdio.h>
161
#include <stdlib.h>
162
163
#define N 1003
164
#define L 53
165
166
int num[ N ][ L ];
167
168
int cmp( const void *a, const void *b ) {
169
int *x = (int*)a;
170
int *y = (int*)b;
171
int i;
172
for ( i = 0; (-1!=x[i])&&(-1!=y[i]); ++i ) {
173
if ( x[ i ] > y[ i ] ) {
174
return -1;
175
}
176
if ( x[ i ] < y[ i ] ) {
177
return 1;
178
}
179
}
180
if ( (-1 == x[i]) && (-1 == y[i]) ) {
181
return 0;
182
}
183
if ( -1 == y[ i ] ) {
184
return -1;
185
}
186
return 1;
187
}
188
189
int main() {
190
int t, n, a, i, j;
191
scanf( "%d", &t );
192
while ( t-- > 0 ) {
193
scanf( "%d", &n );
194
for ( i = 0; i < n; ++i ) {
195
a = 1;
196
for ( j = 0; -1 != a; ++j ) {
197
scanf( "%d", &a );
198
num[ i ][ j ] = a;
199
}
200
}
201
202
qsort( num, n, sizeof(num[0]), cmp );
203
204
for ( i = 0; i < n; ++i ) {
205
if ( -1 != num[ i ][ 0 ] ) {
206
printf( "%d", num[ i ][ 0 ] );
207
for ( j = 1; num[ i ][ j ] != -1; ++j ) {
208
printf( " %d", num[ i ][ j ] );
209
}
210
}
211
printf( "\n" );
212
}
213
}
214
return 0;
215
}
216
*/
217
218
/*
219
程序一,WA 了,因為 109 !!
220
*/
221
/*
222
#include <stdio.h>
223
#include <string.h>
224
#include <stdlib.h>
225
226
#define N 1003
227
#define L 53
228
#define BASE 5
229
230
char num[ N ][ L ];
231
232
int cmp( const void *a, const void *b ) {
233
return strcmp( ((const char *)b), ((const char*)a) );
234
}
235
236
int main() {
237
int t, n, a, i, j;
238
scanf( "%d", &t );
239
while ( t-- > 0 ) {
240
scanf( "%d", &n );
241
for ( i = 0; i < n; ++i ) {
242
for ( j = 0; ;++j ) {
243
scanf( "%d", &a );
244
if ( -1 != a ) {
245
num[ i ][ j ] = (char)(a+BASE);
246
}
247
else {
248
num[ i ][ j ] = '\0';
249
break;
250
}
251
}
252
}
253
254
qsort( num, n, sizeof(num[0]), cmp );
255
256
for ( i = 0; i < n; ++i ) {
257
if ( '\0' != num[ i ][ 0 ] ) {
258
printf( "%d", (int)(num[ i ][ 0 ]) - BASE );
259
for ( j = 1; num[ i ][ j ] != '\0'; ++j ) {
260
printf( " %d", (int)(num[ i ][ j ]) - BASE );
261
}
262
}
263
printf( "\n" );
264
}
265
}
266
return 0;
267
}
268
*/
269
270
271
/************************************************************
272
Phone Number
273
模擬,字符串排序
274
*/
275
/*
276
#include <stdio.h>
277
#include <string.h>
278
#include <stdlib.h>
279
280
#define N 103
281
#define L 10
282
#define LM 89
283
#define CM 256
284
285
char num[ N ][ L ];
286
int n;
287
288
char* map[ CM ];
289
290
void init() {
291
int i;
292
for ( i = 0; i < CM; ++i ) {
293
map[ i ] = "";
294
}
295
296
map[ '0' ] = "0";
297
map[ '1' ] = "1";
298
map[ '2' ] = map[ 'A' ] = map[ 'B' ] = map[ 'C' ] = "2";
299
map[ '3' ] = map[ 'D' ] = map[ 'E' ] = map[ 'F' ] = "3";
300
map[ '4' ] = map[ 'G' ] = map[ 'H' ] = map[ 'I' ] = "4";
301
map[ '5' ] = map[ 'J' ] = map[ 'K' ] = map[ 'L' ] = "5";
302
map[ '6' ] = map[ 'M' ] = map[ 'N' ] = map[ 'O' ] = "6";
303
map[ '7' ] = map[ 'P' ] = map[ 'Q' ] = map[ 'R' ] = map[ 'S' ] = "7";
304
map[ '8' ] = map[ 'T' ] = map[ 'U' ] = map[ 'V' ] = "8";
305
map[ '9' ] = map[ 'W' ] = map[ 'X' ] = map[ 'Y' ] = map[ 'Z' ] = "9";
306
}
307
308
int cmp( const void *a, const void *b ) {
309
return strcmp( (char*)a, (char*)b );
310
}
311
312
int main() {
313
int i, j;
314
char tmp[ LM ];
315
316
init();
317
318
scanf( "%d", &n );
319
for ( i = 0; i < n; ++i ) {
320
num[ i ][ 0 ] = '\0';
321
322
scanf( "%s", tmp );
323
for ( j = 0; tmp[ j ]; ++j ) {
324
strcat( num[ i ], map[ tmp[ j ] ] );
325
if ( 4 == strlen( num[ i ] ) ) {
326
strcat( num[ i ], "-" );
327
}
328
}
329
}
330
331
qsort( num, n, sizeof(num[0]), cmp );
332
333
strcpy( num[ n ], "*" );
334
j = 1;
335
for ( i = 0; i < n; ++i ) {
336
if ( 0 != strcmp( num[i], num[i+1] ) ) {
337
printf( "%s %d\n", num[i], j );
338
j = 0;
339
}
340
++j;
341
}
342
343
return 0;
344
}
345
*/
346

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

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

posted on 2012-03-08 01:12 coreBugZJ 閱讀(604) 評論(2) 編輯 收藏 引用 所屬分類: ACM 、娛樂