7-1 Hello world
P
H
P
\color{#FF0000}{PHP}
PHP :
Hello
"world!"
#include <bits/stdc++.h>
using namespace std;
int main()
{
puts("Hello\n");
puts("\"world!\"");
return 0;
}
7-2 程序运行时间
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b; cin >> a >> b;
int num = ((b - a) + 50) / 100;
printf("%02d:%02d:%02d", num / 3600, num % 3600 / 60, num % 60);
return 0;
}
7-3 6翻了
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s; getline(cin, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] == '6') {
int j;
for (j = i; j < s.size() && s[j] == '6'; j++) ;
j = j - i;
if (j > 9) cout << "27";
else if (j > 3) cout << '9';
else{
string t(j,'6'); cout << t;
}
i += j - 1;
} else cout << s[i];
}
return 0;
}
s.find(str,pos) : s (目标字符串),pos (起始位置),str (s的子串),函数返回子串str第一次出现在s中的起始位置,否则返回 -1
s.replace(pos,cnt,str) : s (目标字符串),pos (起始位置),cnt (个数/长度),str (要替换的字符串)
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s; getline(cin, s);
int pos = s.find('6', 0);
while (pos != -1) {
int i;
for (i = pos; i < s.size() && s[i] == '6'; i++);
i = i - pos;
if (i > 9) s.replace(pos, i, "27");
else if (i > 3) s.replace(pos, i, "9");
pos = s.find('6', pos + 1);
}
cout << s;
return 0;
}
7-4 哲哲打游戏
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100010;
vector<int> v[MAX_N];
int book[MAX_N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int k; scanf("%d", &k);
while (k--) {
int x; scanf("%d", &x);
v[i].push_back(x);
}
}
int p = 1;
while (m--) {
int op, x;
scanf("%d%d", &op, &x);
if (!op) p = v[p][x - 1];
else if (op == 1) book[x] = p, cout << p << endl;
else p = book[x];
}
cout << p << endl;
return 0;
}
7-5 列出连通集
#include <bits/stdc++.h>
using namespace std;
vector<int> v[15];
bool vis[15];
void dfs(int x) {
cout << x << ' ', vis[x] = true;
for (auto c : v[x]) if (!vis[c]) dfs(c);
}
void bfs(int x) {
queue<int> que;
que.push(x), vis[x] = true;
while (que.size()) {
int t = que.front();
que.pop();
cout << t << ' ';
for (auto c : v[t]) if (!vis[c]) que.push(c), vis[c] = true;
}
}
int main()
{
int n, m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i < n; i++) sort(v[i].begin(), v[i].end());
for (int i = 0; i < n; i++)
if (!vis[i]) cout << "{ ", dfs(i), cout << "}\n";
memset(vis, false, sizeof vis);
for (int i = 0; i < n; i++)
if (!vis[i]) cout << "{ ", bfs(i), cout << "}\n";
return 0;
}
7-6 螺旋方阵
参考:蛇形矩阵
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n][n];
memset(a, 0, sizeof a);
int t = 1, i = 0, j = 0;
a[i][j] = t++;
while (t <= n * n) {
while (!a[i][j + 1] && j + 1 < n) a[i][++j] = t++;
while (!a[i + 1][j] && i + 1 < n) a[++i][j] = t++;
while (!a[i][j - 1] && j - 1 >= 0) a[i][--j] = t++;
while (!a[i - 1][j] && i - 1 >= 0) a[--i][j] = t++;
}
for (int i = 0; i < n; i++) {
for (auto c : a[i]) printf("%3d", c);
puts("");
}
return 0;
}
7-7 单词长度
特
殊
情
况
\color{#FF0000}{特殊情况}
特殊情况 :
输入样例1:sss.
输出样例1:3
输入样例2:.
输出样例2:
输入样例2:.sss
输出样例2:
输入样例3:ss.s
输出样例3:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
bool f = false;
while (cin >> s) {
if (s.back() == '.' && s.size() > 1) s.pop_back();
else if (s.find('.', 0) != -1) break;
cout << (f ? " " : "") << s.length(), f = true;
}
return 0;
}
7-8 图着色问题
#include <bits/stdc++.h>
using namespace std;
vector<int> v[510];
int color[510];
bool vis[510];
bool dfs(int x) {
vis[x] = true;
for (auto c : v[x]) {
if (color[x] == color[c]) return false;
if (!vis[c]) return dfs(c);
}
return true;
}
int main()
{
int n, m, k; cin >> n >> m >> k;
while (m--) {
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cin >> m;
while (m--) {
memset(vis, false, sizeof vis);
bool flag = true;
set<int> se;
for (int i = 1; i <= n; i++) cin >> color[i], se.insert(color[i]);
for (int i = 1; i <= n; i++) if (!vis[i] && flag) flag = dfs(i);
if (flag && se.size() == k) puts("Yes");
else puts("No");
}
return 0;
}
7-9 福到了
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c;
int n;
bool flag = true;
cin >> c >> n;
string a[n];
cin.get();
for (auto &s : a) getline(cin, s);
for (int i = 0; i < n; i++) if (a[i] != a[n - i - 1]) flag = false;
if (flag) puts("bu yong dao le");
for (int i = n - 1; i >= 0; i--) {
for (int j = a[i].size() - 1; j >= 0; j--)
cout << (a[i][j] == ' ' ? ' ' : c);
puts("");
}
return 0;
}
|