您的位置:首页 > 其它

郑州大学第八届校赛热身赛题解

2014-12-07 19:48 204 查看
Problem A: xq and Crystal Mine
打印菱形,应该c语言的书上有,把空格换成*号。
代码:
#include <queue>
#include <string>
#include <cstdio>

using namespace std;

int main(void)
{
int n;
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
while (scanf("%d", &n) != EOF) {
for (int i = 0; i <= n / 2; ++i) {
for (int j = 0; j < n / 2 - i; ++j) {
printf("*");
}
for (int j = 0; j < 2 * i + 1; ++j) {
printf("D");
}
for (int j = 0; j < n / 2 - i; ++j) {
printf("*");
}
printf("\n");
}
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < i + 1; ++j) {
printf("*");
}
for (int j = 0; j < n - 2 * (i + 1); ++j) {
printf("D");
}
for (int j = 0; j < i + 1; ++j) {
printf("*");
}
printf("\n");
}
}

return 0;
}


Problem B Simple Numbers Conversion
进制转换,2进制和4进制转换类似2进制和16进制的转换,2个2进制位对应一个四进制位。只有两种情况。
我这里写的是任意进制的转换,具体解释看着个文章:http://blog.csdn.net/gwq5210/article/details/40856431
不过貌似java里的BigInteger类可以直接转换,java大法好。
代码:
#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 long long ll;

const double esp = 1e-5;

#define N 10010

int r, s;		//将r进制的数变成s进制
int lnum, lans;		//对应数组的长度
char str
;		//以r进制表示的数的字符串
int num
;		//以r进制表示的数的整数
char ans
;		//保存以s进制的最后结果
char mp[] = "0123456789ABCDEF";

int main(int argc, char *argv[])
{
int t;

//	freopen("test.in", "r", stdin);
//	freopen("test.out", "w", stdout);
scanf("%d", &t);
while (t--) {
scanf("%s%d%d", str, &r, &s);
lnum = strlen(str);
//将字符变成对应的整数
for (int i = 0; i < lnum; ++i) {
num[i] = strchr(mp, str[i]) - mp;
}
int pos = 0;
lans = 0;
clr(ans, 0);
//每次迭代不移动数组,节约时间
while (pos < lnum) {
int rem = 0;
for (int i = pos; i < lnum; ++i) {
num[i] += rem * r;
rem = num[i] % s;
num[i] /= s;
}
ans[lans++] = mp[rem];
//去掉商的前导0
while (pos < lnum && num[pos] == 0) {
++pos;
}
}
//得到的是逆序的,要逆序输出
for (int i = lans - 1; i >= 0; --i) {
printf("%c", ans[i]);
}
printf("\n");
}

return 0;
}


Problem C 数字换位
数据量比较小,直接暴力所有情况就行了。我这里用的是c++,用c是可以用strcat将两个字符串连接起来。或者将字符串复制一份放在后边,用strncmp函数比较指定的长度。
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[])
{
int t;
string s, l, r;
//	freopen("test.in", "r", stdin);
//	freopen("out", "w", stdout);
cin >> t;
while (t--) {
cin >> s;
int i = 1;
int ans = 0;
int len = s.length();
do {
l = s.substr(0, i);
r = s.substr(i, len - i);
++i;
if (r + l > s) {
++ans;
}
} while (i < len);
cout << ans << endl;
}

return 0;
}


Problem D: Solve The Problem
给定n个字符串,判断有多少个字符串包含有指定的单词,每次要读入一行,c++使用getline,c可以使用gets或fgets。注意这里要求大小写不敏感。
数据量比较小,不用数据结构,直接暴力。还有注意的是如果一行中出现了指定的单词多次,那么答案也只能加1次,要看清题目的描述。
我写的比较长,另一种思路是,先把所有字符变成小写,c语言里有个tolower函数,可以将字符变成小写,或者自己手写个转换,然后,使用strncmp函数比较,代码应该会短很多。
我的代码:
#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 long long ll;

const double esp = 1e-5;

int mplen[] = {3, 7, 5, 10};
char mp[][20] = {"ELI", "Arrakis", "ELVIS", "Barrelfish"};

bool check(char c1, char c2)
{
if (c1 == c2) {
return true;
} else {
if (c2 >= 'a' && c2 <= 'z') {
return c1 == c2 - 'a' + 'A';
} else {
return c1 == c2 - 'A' + 'a';
}
}
}

int main(int argc, char *argv[])
{
int n;
while (cin >> n) {
int ans = 0;
getchar();
for (int i = 0; i < n; ++i) {
string s;
getline(cin, s);
int len = s.length();
int flag = 0;
for (int j = 0; j < len; ++j) {
for (int k = 0; k < 4; ++k) {
flag = 1;
for (int l = 0; l < mplen[k]; ++l) {
if (j + l >= len || !check(s[j + l], mp[k][l])) {
flag = 0;
break;
}
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
break;
}
}
ans += flag;
}
cout << ans << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: