463 桌面窗体重叠
样例输入
10 100 20 60
60 160 50 200
样例输出
400
#include <iostream>
using namespace std;
#include <cmath>
struct win {
int l;
int r;
int u;
int d;
}a[10];
int main() {
int x1, x2;
int y1, y2;
int w1, w2;
int h1, h2;
for (int i = 1; i <= 2; i++) {
cin >> a[i].l >> a[i].r >> a[i].u >> a[i].d;
}
x1 = a[1].l; x2 = a[2].l;
y1 = a[1].u; y2 = a[2].u;
w1 = a[1].r - a[1].l; w2 = a[2].r - a[2].l;
h1 = a[1].d - a[1].u; h2 = a[2].d - a[2].u;
int startx = min(x1, x2);
int endx = max(x1 + w1, x2 + w2);
int over_w = w1 + w2 - (endx - startx);
int starty = min(y1, y2);
int endy = max(y1 + h1, y2 + h2);
int over_h = h1 + h2- (endy - starty);
int s = over_w * over_h;
if (x1 + w1 > x2 &&
x2 + w2 > x1 &&
y1 + h1 > y2 &&
y2 + h2 > y1) {
cout << s;
} else {
cout << 0 << endl;
}
return 0;
}
求两个矩形目标区域的重叠面积,里面只用到了max,和min库函数:(matlab代码)
function ratio = overlap(Rectan_A,Rectan_B)
x1 = Rectan_A(1);
y1 = Rectan_A(2);
width1 = Rectan_A(3);
height1 = Rectan_A(4);
x2 = Rectan_B(1);
y2 = Rectan_B(2);
width2 = Rectan_B(3);
height2 = Rectan_B(4);
startx = min(x1, x2); endx = max(x1+width1, x2+width2);
over_width = width1 + width2 - (endx - startx);
starty = min(y1, y2); endy = max(y1 + height1, y2 + height2);
over_height = height1+height2-(endy-starty);
if over_width< 3.0009e-04||over_height< 3.0009e-04
rario = 0;
else
Area_over = over_width*over_height;
Area1 = width1*height1;
Area2 = width2*height2;
ratio = Area_over/(Area1+Area2-Area_over);
end
461 成绩统计1
样例输入
5
C best
C good
N 90
C poor
N 98
样例输出
3 94
//atoi函数的使用
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>
struct stu {
string a;
string b;
}m[1005];
int main() {
int n;
cin >> n;
int t = 0, q = 0;
double sum = 0;
for (int i = 1; i <= n; i++) {
cin >> m[i].a >> m[i].b;
if (m[i].a == "C") {
t++;
} else {
q++;
sum += atoi(m[i].b.c_str());
}
}
cout << t << " " << (int) (sum / q);
return 0;
}
462. 注册账号
样例输入
6
i 522633200009118006
i 51170219990111195X
i 45102119800321935X
q 20081011
q 17254862
i 511702196505046283
样例输出
2 2 18667936
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>
struct stu {
string a;
string b;
}m[10005];
int main() {
int n;
cin >> n;
int male = 0, female = 0, q = 0;
long double sum = 0;
char p[10005];
for (int i = 1; i <= n; i++) {
cin >> m[i].a >> m[i].b;
if (m[i].a == "i") {
p[i] = m[i].b[m[i].b.size() - 2];
}
}
for (int i = 1; i <= n; i++) {
if (m[i].a == "i") {
if ((p[i] - '0') & 1) {
male++;
//cout << "male: " << m[i].b << endl;
} else {
//cout << "female: " << m[i].b << endl;
female++;
}
} else {
q++;
sum += atoi(m[i].b.c_str());
}
}
cout << male << " " << female << " ";
long long h = (int) (sum / q);
if ( h < 0) {
cout << 0;
} else cout << h;
return 0;
}
446 回形方阵
样例输入
5
样例输出
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
#include<iostream>
using namespace std;
int n, i, j, k, a[10][10];
int main()
{
cin >> n;
for (k = 1; k <= (n + 1) / 2; k++)
{
for (i = k; i <= n + 1 - k; i++)
{
for (j = k; j <= n + 1 - k; j++)
a[i][j] = k;
}
}
for (i = 1; i <= n; i++)
{
for(j = 1; j < n; j++)
printf("%d ",a[i][j]);
printf("%d\n",a[i][n]);
}
return 0;
}
460 猴子选大王
样例输入
3 2
样例输出
3
数学方法
#include <iostream>
using namespace std;
int main()
{
long long n, k, f = 0;
cin >> n >> k;
for (long long i = 1; i <= n; i++)
f = (f + k) % i;
cout << f + 1 << endl;
}
数组模拟——法一
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a[1010], p = -1, n, num, i; //用p作为数组下标(猴子编号)
cin >> n >> k;
num = n;
memset(a, -1, sizeof(a));
while(num > 0) //用num记录剩余的猴子
{
for(i = 1;i <= k;i++) //循环m次
{
p = (p + 1) % n; //指针移动(类似于环形队列)
if(a[p] == 0) //该猴子已经淘汰,不算本次循环
i--;
}
a[p] = 0; //淘汰p编号的猴子
//cout<<"淘汰的是:"<<p<<endl;
num--;
}
cout << p + 1 << endl;
return 0;
}
数组模拟——法二
#include <iostream>
#include <cstring>>
using namespace std;
int main(){
int n, k, q;
cin >> n >> k >> q;
int a[n + 5];
memset(a, 0, sizeof(a));
int out = 0;
int num = 0;
for (int i = 0; out < n; i++){
if (i == n) i = 0;
if (!a[i]){
num++;
if (num == k){
num = 0;
out++;
a[i] = 1;
if (out == n){
cout << i + 1;
}
}
}
}
}
循环链表
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
node *head, *tail, *p;
int n, i, k;
cin >> n >> k;
if (n == 1)
{
cout << 1 << endl;
return 0;
}
head = tail = new node;
head->data = 1;
for (i = 2; i <= n; i++)
{
p = new node;
p->data = i;
tail->next = p;
tail = p;
}
tail->next = head;
while (n > 1)
{
for (i = 1; i < k; i++)
tail = tail->next;
node *x = tail->next;
tail->next = x->next;
delete x;
n--;
}
cout << tail->data << endl;
return 0;
}