-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnowCoder18.cpp
More file actions
306 lines (270 loc) · 6.84 KB
/
nowCoder18.cpp
File metadata and controls
306 lines (270 loc) · 6.84 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
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
// nowCoder18.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#define _USE_MATH_DEFINES
//1.只有输出
int firstTestfun()
{
std::cout << "Hello Nowcoder!" << std::endl;
return 0;
}
//2.单组_A+B
int a_add_b_Testfun()
{
int a, b;
while (std::cin >> a >> b) { // 注意 while 处理多个 case
std::cout << a + b << std::endl;
}
return 0;
}
//3.多组_A + B_EOF形式
int multi_a_add_b_Testfun()
{
int a, b;
while (std::cin >> a >> b) { // 注意 while 处理多个 case
std::cout << a + b << std::endl;
}
return 0;
}
//4.多组_A + B_T组形式
int fixed_a_add_b_testFun() {
int a, b;
int lines;
std::cin >> lines;
for (int i = 0; i < lines; i++) {
std::cin >> a >> b;
std::cout << a + b << std::endl;
}
return 0;
}
//5.多组_A + B_零尾模式
int zero_end_a_add_b_testFun() {
int a, b;
while (std::cin >> a >> b) { // 注意 while 处理多个 case
if (a == 0 && b == 0) {
break;
}
std::cout << a + b << std::endl;
}
return 0;
}
//6.单组_一维数组
int sumArray() {
int a, b;
long sum = 0;
std::cin >> a;
for (int i = 0; i < a; i++) {
std::cin >> b;
sum += b;
}
std::cout << sum << std::endl;
return 0;
}
//7.多组_一维数组_T组形式
int multi_sumArray() {
int a, b;
int inputLines;
int lines = 0;
std::cin >> inputLines;
while (lines < inputLines) { // 注意 while 处理多个 case
std::cin >> a;
long sum = 0;
for (int i = 0; i < a; i++) {
std::cin >> b;
sum += b;
}
std::cout << sum << std::endl;
lines++;
}
return 0;
}
//8.单组_二维数组
int two_dim_sum_array() {
int a, b;
std::cin >> a >> b;
long sum = 0;
int inputValue;
for (int i = 0; i < a * b; i++) {
std::cin >> inputValue;
sum += inputValue;
}
std::cout << sum << std::endl;
return 0;
}
//9.多组_二维数组_T组形式
int multi_two_dim_sum_array()
{
int lines;
std::cin >> lines;
for (int j = 0; j < lines; j++)
{
int a, b;
std::cin >> a >> b;
long sum = 0;
int inputValue;
for (int i = 0; i < a * b; i++) {
std::cin >> inputValue;
sum += inputValue;
}
std::cout << sum << std::endl;
}
return 0;
}
//10.单组_字符串 倒置
int reverse_string() {
int input_chars;
std::cin >> input_chars;
std::string a;
std::cin >> a;
for (int i = a.size() - 1; i >= 0; i--) {
std::cout << a[i];
}
return 0;
}
//11.多组_字符串_T组形式 倒置
int multi_reverse_string() {
int lines;
std::cin >> lines;
for (int j = 0; j < lines; j++) {
int input_chars;
std::cin >> input_chars;
std::string a;
std::cin >> a;
for (int i = a.size() - 1; i >= 0; i--) {
std::cout << a[i];
}
std::cout << std::endl;
}
return 0;
}
//12.单组_二维字符数组 倒置
int reverse_two_dims_string() {
int lines, chars_count;
std::cin >> lines;
std::cin >> chars_count;
std::vector<std::string> inputValues;
inputValues.resize(lines);
for (int j = 0; j < lines; j++) {
std::cin >> inputValues[j];
}
for (int j = lines - 1; j >= 0; j--) {
for (int i = inputValues[j].size() - 1; i >= 0; i--) {
std::cout << inputValues[j][i];
}
std::cout << std::endl;
}
return 0;
}
//13.多组_带空格的字符串_T组形式
//字符串去空格
int trimAllSapace(std::string& input_str) {
if (!input_str.empty()) {
int index = 0;
while ((index = input_str.find(' ', index)) != std::string::npos) {
input_str.erase(index, 1);
}
}
return 0;
}
//13.多组_带空格的字符串_T组形式
int reverse_multi_string_trimSpaces() {
int lines, count;
std::cin >> lines;
std::vector<std::string> inputValues;
inputValues.resize(lines);
for (int j = 0; j < lines; j++) {
int count;
std::cin >> count;
std::cin >> std::ws;
std::getline(std::cin, inputValues[j]);
trimAllSapace(inputValues[j]);
}
for (int j = 0; j < lines; j++) {
for (int i = inputValues[j].length() - 1; i >= 0; i--) {
std::cout << inputValues[j][i];
}
std::cout << std::endl;
}
return 0;
}
//14.单组_保留小数位数
int setPrecisionTest()
{
double a;
while (std::cin >> a) { // 注意 while 处理多个 case
std::cout << std::fixed << std::setprecision(3) << a << std::endl;
}
return 0;
}
//15.单组_补充前导零 前面填充0
int fillzeroPre()
{
int a;
std::cin >> a;
std::ostringstream oss;
int width = 9; // 想要的总宽度
oss << std::setw(width) << std::setfill('0') << a;
std::string result = oss.str();
std::cout << result << std::endl;
return 0;
}
//16.单组_spj判断YES与NO
int use_if()
{
int a;
while (std::cin >> a) { // 注意 while 处理多个 case
if (a % 2 == 1)
{
std::cout << "YES" << std::endl;
}
else
{
std::cout << "NO" << std::endl;
}
}
return 0;
}
// 17.单组_spj判断浮点误差 计算圆形的面积
int testSetPrecision2()
{
int a;
double tmp_pi = M_PI;
while (std::cin >> a) { // 注意 while 处理多个 case
double result = tmp_pi * a * a;
std::cout << std::fixed << std::setprecision(8) << result << std::endl;
}
return 0;
}
//18.单组_spj判断数组之和 构造符合要求的数组
int constructArray() {
int a, b;
while (std::cin >> a >> b) { // 注意 while 处理多个 case
int count = a;
for (int i = 1; i < count; i++) {
int value = (b - i >= a) ? i : 1;
b = b - value;
std::cout << value << " ";
a--;
}
std::cout << b << std::endl;
}
return 0;
}
int main()
{
std::cout << "Hello World!\n";
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件