7-1 就不告诉你 (15分)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
string str = to_string(a * b);
reverse(str.begin(), str.end());
cout << str;
return 0;
}
错误:
解析->前导0的问题
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
string str = to_string(a * b);
reverse(str.begin(), str.end());
while (str[0] == '0')
{
str.erase(0, 1);
}
cout << str;
return 0;
}
7-2 打折 (5分)
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%.2f", a * b * 0.1);
return 0;
}
7-3 不同子串 (10分)
#include <iostream>
#include <set>
#include <cstring>
using namespace std;
int main()
{
string s;
getline(cin, s);
set<string> ans;
for (int i = 0; i <= s.size() - 1; i++)
{
for (int j = 0; j <= s.size() - 1; j++)
{
string str = s.substr(i, j);
ans.insert(str);
}
}
printf("%d\n", ans.size());
return 0;
}
7-4 Left-pad (20分)
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int cnt;
char c;
string s;
cin >> cnt >> c;
getchar();
getline(cin, s);
int len = s.length();
if (len >= cnt)
{
cout << s.substr(s.size() - cnt);
}
else
{
for (int i = 0; i < cnt - len; i++)
{
cout << c;
}
cout << s;
}
return 0;
}
7-5 I Love GPLT (5分)
#include <iostream>
using namespace std;
int main()
{
cout << "I" << endl
<< " " << endl
<< "L" << endl
<< "o" << endl
<< "v" << endl
<< "e" << endl
<< " " << endl
<< "G" << endl
<< "P" << endl
<< "L" << endl
<< "T";
return 0;
}
7-6 大炮打蚊子 (15分)
5 6
00#00#
000###
00#000
000000
00#000
2
1 2
1 4
#include <iostream>
using namespace std;
int a[700][700]; //标记蚊子生命值 2满血 1半血 0死亡
int main()
{
int M, N;
cin >> M >> N;
int k, x, y;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
char c;
cin >> c;
if (c == '#')
{
a[i][j] = 2;
}
else
{
a[i][j] = 0;
}
}
}
cin >> k; //炮弹的数量
while (k--)
{
int sum = 0;
cin >> x >> y; //炮弹的位置
//直接命中
if (a[x][y] >= 1)
{
sum++;
a[x][y] = 0;
}
//上
if ((x - 1) >= 0)
{
if (a[x - 1][y] == 2)
{
a[x - 1][y]--;
}
else if (a[x - 1][y] == 1)
{
a[x - 1][y] = 0;
sum++;
}
}
//下
if ((x + 1) != M)
{
if (a[x + 1][y] == 2)
{
a[x + 1][y]--;
}
else if (a[x + 1][y] == 1)
{
a[x + 1][y] = 0;
sum++;
}
}
//左
if ((y - 1) >= 0)
{
if (a[x][y - 1] == 2)
{
a[x][y - 1]--;
}
else if (a[x][y - 1] == 1)
{
a[x][y - 1] = 0;
sum++;
}
}
//右
if ((y + 1) != N)
{
if (a[x][y + 1] == 2)
{
a[x][y + 1]--;
}
else if (a[x][y + 1] == 1)
{
a[x][y + 1] = 0;
sum++;
}
}
cout << sum << endl;
}
return 0;
}
7-7 装箱问题 (20分)
8
60 70 80 90 30 40 10 20
#include <iostream>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
int a[n], box[n], c[n]; //a数组即要存放的物品,box数组即为大小100的箱子,c数组即每个物品对象的编号
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) //编号i从1开始,用for循环设定每个箱子初始值为100
{
box[i] = 100;
}
for (int i = 0; i < n; i++) //先依次拿出物品即a[i]来存放
{
for (int j = 1;; j++) //箱子的编号从1开始,不设循环次数
{
if ((box[j] >= a[i])) //从箱子1开始判断大小是否比物品大,如果大即可存放,否则j++,即再寻找下一个箱子比较
{
c[i] = j; //用数组b[i]记录每个物品a[i]的箱子编号
box[j] = box[j] - a[i]; //存放后这个箱子的空间即减去这个物品a[i]
break; //存放后即退出这个箱子循环,继续执行主循环拿下一个物品继续存放
}
}
}
for (int i = 0; i < n; i++) //输出每物品和对应箱子的编号
{
printf("%d %d\n", a[i], c[i]);
}
int max = 0; //用下面循环语句找出箱子最大的编号,即用的箱子数
for (int i = 1; i < n; i++)
{
if (c[i] > c[max])
{
max = i;
}
}
printf("%d\n", c[max]);
return 0;
}
简化:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n, d, s[1000] = {100}, c = 0, j;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &d);
for (j = 0; j <= c; j++)
{
if (s[j] >= d)
{
printf("%d %d\n", d, j + 1);
s[j] -= d;
break;
}
}
if (j > c)
{
s[++c] = 100 - d;
printf("%d %d\n", d, c + 1);
}
}
printf("%d", c + 1);
}
7-8 装睡 (10分)
4
Amy 15 70
Tom 14 60
Joe 18 50
Zoe 21 71
#include <iostream>
using namespace std;
int main()
{
int n, heart, impluse;
string name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> name >> heart >> impluse;
if (heart < 15 || heart > 20 || impluse < 50 || impluse > 70)
{
cout << name << endl;
}
}
return 0;
}
7-9 全球变暖 (25分)
7
.......
.##....
.##....
....##.
..####.
...###.
.......
7-10 图着色问题 (25分)
6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
4
1 2 3 3 1 2
4 5 6 6 4 5
1 2 3 4 5 6
2 3 4 2 3 4