1. 相反数
#include<iostream>
#include<map>
using namespace std;
int main(){
int n, t, count = 0;
map<int,int> mp;
scanf("%d", &n);
for(int i = 0;i < n; ++i){
scanf("%d", &t);
mp[abs(t)]++;
}
for(map<int,int>::iterator it = mp.begin(); it != mp.end(); ++it)
if(it->second==2) count++;
cout << count << endl;
return 0;
}
5
1 2 3 -1 -2
2
2. 窗口
#include<iostream>
#include<map>
using namespace std;
struct Win{
int x1, y1, x2, y2, num;
}win;
map<int, Win> mp;
int main(){
int n, m;
cin >> n >> m;
for(int i = 0; i < n; ++i){
cin >> win.x1 >> win.y1 >> win.x2 >> win.y2;
win.num = i+1;
mp[n-i] = win;
}
for(int i = 0; i < m; ++i){
int x, y;
cin >> x >> y;
int winno = -1;
for(int j = 1; j <= n; ++j){
if((mp[j].x1 <= x) && (x <= mp[j].x2) && (mp[j].y1 <= y) && (y <= mp[j].y2)){
winno = mp[j].num;
win = mp[j];
for(int k = j; k > 1; --k)
mp[k] = mp[k-1];
mp[1] = win;
break;
}
}
(winno == -1) ? cout << "IGNORED" << endl : cout << winno << endl;
}
return 0;
}
3 4
0 0 4 4
1 1 5 5
2 2 6 6
1 1
0 0
4 4
0 5
2
1
1
IGNORED
3. 命令行选项
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int N = 256;
char delimiter[] = " ";
void spilt(vector<string>& v, char *s, char *t)
{
char *sp;
sp = strtok(s, t);
while(sp) {
v.push_back(sp);
sp = strtok(NULL, t);
}
}
void mygetline(char *pc)
{
char c;
while((c=getchar()) != '\n' && c !=EOF)
*pc++ = c;
*pc = '\0';
}
int main()
{
string format;
char s[N+1];
int n;
cin >> format >> n;
getchar();
for(int i=1; i<=n; i++) {
vector<string> sv;
map<string, string> m;
mygetline(s);
spilt(sv, s, delimiter);
for(int j=1; j<(int)sv.size(); j++) {
if(sv[j].size() == 2 && sv[j][0] == '-') {
int pos = format.find(sv[j][1]);
if(pos == -1)
break;
if(m.find(sv[j]) == m.end())
m[sv[j]] = "";
if(format[pos+1] == ':' && j+1 < (int)sv.size()) {
m[sv[j]] = sv[j+1];
j++;
}
} else
break;
}
cout << "Case " << i << ":";
for(map<string,string>::iterator iter=m.begin(); iter!=m.end(); iter++) {
cout << " " << iter->first;
if(iter->second != "")
cout << " " << iter->second;
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int N = 256;
char delimiter[] = " ";
void spilt(vector<string>& v, char *s, char *t)
{
char *sp;
sp = strtok(s, t);
while(sp) {
v.push_back(sp);
sp = strtok(NULL, t);
}
}
void mygetline(char *pc)
{
char c;
while((c=getchar()) != '\n' && c !=EOF)
*pc++ = c;
*pc = '\0';
}
int main()
{
string format;
char s[N+1];
int n;
cin >> format >> n;
getchar();
for(int i=1; i<=n; i++) {
vector<string> sv;
map<string, string> m;
mygetline(s);
spilt(sv, s, delimiter);
for(int j=1; j<(int)sv.size(); j++) {
if(sv[j].size() == 2 && sv[j][0] == '-') {
int pos = format.find(sv[j][1]);
if(pos == -1)
break;
if(m.find(sv[j]) == m.end())
m[sv[j]] = "";
if(format[pos+1] == ':' && j+1 < (int)sv.size()) {
m[sv[j]] = sv[j+1];
j++;
}
} else
break;
}
cout << "Case " << i << ":";
for(map<string,string>::iterator iter=m.begin(); iter!=m.end(); iter++) {
cout << " " << iter->first;
if(iter->second != "")
cout << " " << iter->second;
}
cout << endl;
}
return 0;
}
albw:x
4
ls -a -l -a documents -b
ls
ls -w 10 -x -w 15
ls -a -b -c -d -e -l
Case 1: -a -l
Case 2:
Case 3: -w 15 -x
Case 4: -a -b
4. 无线网络
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100 + 100;
struct {
long long x, y;
} coord[N+1];
struct node {
long long x, y;
int step;
};
bool visited[N+1];
int bfs(int n, int begin, int end, long long r)
{
memset(visited, false, sizeof(visited));
node start, front, v;
start.x = coord[begin].x;
start.y = coord[begin].y;
start.step = 0;
queue<node> q;
q.push(start);
visited[begin] = true;
while(!q.empty()) {
front = q.front();
q.pop();
if(front.x == coord[end].x && front.y == coord[end].y)
return front.step - 1;
for(int i=0; i<n; i++) {
if(visited[i])
continue;
if((front.x - coord[i].x) * (front.x - coord[i].x) + (front.y - coord[i].y) * (front.y - coord[i].y) > r * r)
continue;
else {
visited[i] = true;
v.x = coord[i].x;
v.y = coord[i].y;
v.step = front.step + 1;
q.push(v);
}
}
}
return 0;
}
int main()
{
int n, m, k;
long long r;
cin >> n >> m >> k >> r;
for(int i=0; i<n+m; i++)
cin >> coord[i].x >> coord[i].y;
int ans = bfs(n + m, 0, 1, r);
cout << ans << endl;
return 0;
}
5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0
2
10 1 1 2
0 0
3 1
-2 0
-2 2
-2 4
-2 6
0 6
2 6
2 4
2 2
2 0
1
10 1 1 2
0 0
3 1
-2 0
-2 2
-2 4
-2 6
0 6
2 6
2 4
2 2
3 0
8
6 3 2 50000000
0 0
50000000 100000000
100000000 100000000
100000000 0
100000000 50000000
50000000 0
-100000000 50000000
0 50000000
0 100000000
2
5. 任务调度