概述
C(C++)编译器
判断给定的字符串在不在集合中,用字符串比较函数就行了。
代码:
/*************************************************************************
> File Name: a.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: Thu 09 Apr 2015 08:35:46 AM CST
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
const double esp = 1e-5;
#define N 100010
int cnt = 8;
char str[N];
const char *mp[] = {"void", "byte", "char", "int", "long", "float",
"double", "__int64"};
int main(int argc, char *argv[])
{
while (gets(str) != NULL && strcmp(str, "end") != 0) {
int flag = 0;
for (int i = 0; i < 8; ++i) {
if (strcmp(str, mp[i]) == 0) {
flag = 1;
break;
}
}
printf("%sn", flag ? "Yes" : "No");
}
return 0;
}
约瑟夫环
最后一个退出的人是星期几。注意到每个人都要退出,所以将人数取模就行了。
/*************************************************************************
> File Name: b.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: Thu 09 Apr 2015 09:07:45 AM CST
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
const double esp = 1e-5;
const char *mp[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"};
int main(int argc, char *argv[])
{
int n = 0;
while (scanf("%d", &n) != EOF && n) {
n--;
printf("%sn", mp[n % 7]);
}
return 0;
}
根据给定的规则计算QQ的价值就行了。锻炼代码能力,注意细节就行了。
/*************************************************************************
> File Name: c.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: Thu 09 Apr 2015 09:37:11 AM CST
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
const double esp = 1e-5;
#define N 20
ll d1, d2;
char s1[N], s2[N];
ll values(char str[], ll d)
{
ll ans = d;
ll len = strlen(str);
for (int i = 0; i < len; ++i) {
ans += str[i] - '0';
}
int i = 1;
int cnt = 1;
while (i <= len) {
if (str[i] != str[i - 1]) {
if (cnt > 1) {
ll x = 1;
for (int j = 0; j < cnt + 1; ++j) {
x *= str[i - 1] - '0' + 1;
}
ans += x;
}
cnt = 0;
}
++cnt;
++i;
}
return ans;
}
int main(int argc, char *argv[])
{
while (scanf("%s%lld%s%lld", s1, &d1, s2, &d2) != EOF) {
ll l1 = strlen(s1);
ll l2 = strlen(s2);
ll v1 = values(s1, d1);
ll v2 = values(s2, d2);
if (l1 < l2) {
v1 = 1;
v2 = 0;
} else if (l1 > l2) {
v1 = 0;
v2 = 1;
}
printf("%sn", (v1 > v2) ? "First" : ((v1 < v2) ? "Second" : "Equal"));
}
return 0;
}
行列式
一个类似行列式的东西,主对角线乘积和减去副对角线上的乘积和。注意结果可能爆int。还有2*2和1*1要特判。
有一个技巧就是使用取模,可能是负数的可以加上n。还有一个方法是将二维数组向右边复制一份,但数组要开的足够大。
#include <cstdio>
#define N 15
long long mat[N][N];
int main(void)
{
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &mat[i][j]);
}
}
long long ans = 0;
for (int i = 0; i < n; ++i) {
int x = 0;
int y = i;
long long tmp = 1;
for (int j = 0; j < n; ++j) {
int a = (x + j) % n;
int b = (y + j) % n;
tmp *= mat[a][b];
}
ans += tmp;
}
for (int i = 0; i < n; ++i) {
int x = i;
int y = n - 1;
long long tmp = 1;
for (int j = 0; j < n; ++j) {
int a = (x + j + n) % n;
int b = (y - j + n) % n;
tmp *= mat[a][b];
}
ans -= tmp;
}
if (n == 2) {
ans = mat[0][0] * mat[1][1] - mat[1][0] * mat[0][1];
} else if (n == 1) {
ans = mat[0][0];
}
printf("%lldn", ans);
}
return 0;
}
零比特填充-透明传输
这个题目用的思路和QQ那道题目一样,都是求出来连续相同的字符个数,这个要求去掉连续5个1后的零,遇到这个跳过这个字符就行了。
#include <cstdio>
#include <cstring>
#define N 1010
int len;
char str[N], tmp[N];
void check(void)
{
int cnt = 1;
int flag = 1;
for (int i = 1; i <= len; ++i) {
if (str[i] != str[i - 1]) {
if (str[i - 1] == '1') {
if (cnt > 5) {
flag = 0;
strcpy(str, "Bad luck!");
break;
}
}
cnt = 0;
}
++cnt;
}
if (flag) {
cnt = 1;
strcpy(tmp, str);
int x = 0;
for (int i = 1; i <= len; ++i) {
str[x++] = tmp[i - 1];
if (tmp[i] != tmp[i - 1]) {
if (tmp[i - 1] == '1' && cnt == 5) {
++i;
}
cnt = 0;
}
++cnt;
}
str[x] = '