A - cAPS lOCK
如果字符串全部是大写或者除首字母外都大写,那么改变整个串的大小写,大写转小写,小写转大写。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[1000];
int main()
{
while(gets(a)){
int i;
for(i = 1; a[i]; ++i){
if(a[i] >= 'a' && a[i] <= 'z'){
break;
}
}
if(a[i] == 0){
for(i = 0; a[i]; ++i){
if(a[i] >= 'A' && a[i] <= 'Z'){
printf("%c",a[i] + 32);
}
else{
printf("%c",a[i] - 32);
}
}
puts("");
}
else{
puts(a);
}
}
return 0;
}
B - Opposites Attract
要求找到A和B的和为0的所有组合数。直接hash统计。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
long long xhash[21];
int main()
{
int n;
while(scanf("%d", &n) != EOF){
int i, t;
memset(xhash, 0, sizeof(xhash));
for(i = 0; i < n; ++i){
scanf("%d", &t);
xhash[t + 10]++;
}
long long ans = 0;
for(i = 0; i < 10; ++i){
ans = ans + xhash[i] * xhash[20 - i];
}
ans = ans + xhash[10] * (xhash[10] - 1) / 2;
printf("%I64d\n", ans);
}
return 0;
}
C - The World is a Theatre
从n个男生中选取至少4个男生,从m个女生中至少选取1个女生,选取的总人数为t的所有组合数。暴力。
#include <iostream>
#include <cstdio>
using namespace std;
#define LL long long
LL com(LL n, LL r){// return C(n, r)
if( n-r > r ) r = n-r; // C(n, r) = C(n, n-r)
LL i, j, s = 1;
for( i=0, j=1; i < r; ++i ){
s *= (n-i);
for( ; j <= r && s%j == 0; ++j ) s /= j;
}
return s;
}
int main(){
LL n, m, t;
while(scanf("%I64d %I64d %I64d", &n, &m, &t) != EOF){
LL i, ans;
ans = 0;
for(i = 1; t - i >= 4; ++i){
ans += com(n, t - i) * com(m, i);
}
printf("%I64d\n", ans);
}
return 0;
}
D、E、F暂时不会