记录了目前1-70题中WA的三道题的思路及代码,欢迎大家在评论区交流讨论。
飞机起飞速度
非常恶心的一道题,体量赶上一小个大作业了,样例通过,可能有部分极端值未能考虑到。
原为github上一个开源的java项目Boeing-737,直接扒别人项目当OJ练习题,着实离谱。
按照题目要求,依次求解就完事了。
#include <stdio.h>
#include <math.h>
const int pref[10][7] = {
{0, 0, 0, 1, 2, 3, 4},
{0, 0, 0, 1, 2, 3, 4},
{0, 0, 0, 1, 2, 3, 4},
{0, 0, 0, 1, 2, 4, -1},
{0, 0, 1, 1, 3, 4, -1},
{1, 1, 1, 2, 3, 4, -1},
{1, 1, 1, 2, 3, 4, -1},
{1, 1, 2, 3, 4, -1, -1},
{2, 2, 2, 3, 4, -1, -1},
{2, 2, 3, 3, 4, -1, -1}
};
const int v1[5][3][13] = {
{
{ -1, -1, -1, -1, -1, -1, 108, 118, 128, 138, 146, 154, 161 },
{ -1, -1, -1, -1, -1, -1, 104, 114, 122, 131, 138, 146, 154 },
{ -1, -1, -1, -1, -1, -1, 99, 108, 116, 124, 132, 140, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 110, 120, 130, 140, 147, 155, 162 },
{ -1, -1, -1, -1, -1, -1, 106, 115, 124, 132, 141, 148, 155 },
{ -1, -1, -1, -1, -1, -1, 100, 109, 117, 125, 134, 141, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 111, 222, 132, 141, 149, 156, -1 },
{ -1, -1, -1, -1, -1, -1, 107, 116, 125, 134, 141, 148, -1 },
{ -1, -1, -1, -1, -1, -1, 101, 110, 118, 127, 135, 142, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 113, 123, 133, 143, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 108, 117, 126, 133, 142, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 103, 111, 120, 128, 136, -1, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 115, 125, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 109, 119, 128, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 104, 113, 122, -1, -1, -1, -1 }
}
};
const int vr[5][3][13] = {
{
{ -1, -1, -1, -1, -1, -1, 108, 118, 128, 138, 147, 155, 163 },
{ -1, -1, -1, -1, -1, -1, 104, 114, 122, 131, 139, 147, 155 },
{ -1, -1, -1, -1, -1, -1, 99, 108, 116, 124, 132, 140, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 110, 120, 130, 140, 148, 156, 164 },
{ -1, -1, -1, -1, -1, -1, 106, 115, 124, 132, 141, 149, 156 },
{ -1, -1, -1, -1, -1, -1, 100, 109, 117, 125, 134, 141, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 111, 122, 132, 141, 150, 157, -1 },
{ -1, -1, -1, -1, -1, -1, 107, 116, 125, 134, 142, 150, -1 },
{ -1, -1, -1, -1, -1, -1, 101, 110, 118, 127, 135, 142, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 113, 123, 133, 143, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 108, 117, 126, 135, 143, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 103, 111, 120, 128, 136, -1, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 115, 125, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 109, 119, 128, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 104, 113, 122, -1, -1, -1, -1 }
}
};
const int v2[5][3][13] = {
{
{ -1, -1, -1, -1, -1, -1, 122, 130, 138, 147, 154, 161, 167 },
{ -1, -1, -1, -1, -1, -1, 118, 125, 132, 139, 146, 153, 159 },
{ -1, -1, -1, -1, -1, -1, 112, 119, 126, 132, 139, 145, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 122, 130, 138, 147, 154, 161, 167 },
{ -1, -1, -1, -1, -1, -1, 118, 125, 132, 139, 146, 153, 160 },
{ -1, -1, -1, -1, -1, -1, 112, 119, 125, 132, 139, 145, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 121, 130, 138, 147, 154, 161, -1 },
{ -1, -1, -1, -1, -1, -1, 117, 124, 131, 139, 146, 153, -1 },
{ -1, -1, -1, -1, -1, -1, 111, 118, 125, 132, 139, 145, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 121, 130, 138, 147, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 117, 124, 131, 139, 146, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 111, 118, 125, 132, 139, -1, -1 }
}, {
{ -1, -1, -1, -1, -1, -1, 121, 130, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 116, 124, 131, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, 110, 117, 125, -1, -1, -1, -1 }
}
};
const int v1reduc[3][8] = {
{0, 0, 14, 10, 8, 6, 5, 4},
{0, 0, 14, 10, 8, 6, 5, 4},
{0, 0, 12, 8, 6, 4, 3, 2}
};
int Xvspd[3] = {0};
void invalid(void) {
printf("Flight not possible!\n");
}
int calPref(int elev, double pres, double temp) {
int psal = (int)fmin(9, fmax(0, ceil((elev + 1000 * (29.92 - pres)) / 1000)));
int tmpc = (int)fmin(7, fmax(1, floor(temp / 10))) - 1;
int Xpref = pref[psal][tmpc];
return Xpref;
}
void calXvspd(int Xpref, int wgt, int flp) {
int wtit = (int) ceil(wgt / 5000);
Xvspd[0] = v1[Xpref][flp][wtit];
Xvspd[1] = vr[Xpref][flp][wtit];
Xvspd[2] = v2[Xpref][flp][wtit];
}
void altXv1spd(int rwy, int flp) {
int ar1t = (int)fmin(7, fmax(0, floor(rwy / 2000)));
Xvspd[0] -= v1reduc[flp][ar1t];
}
int main() {
double temp, pres;
int elev, rwy, wgt, flp, wet;
scanf("%lf %lf %d %d %d %d %d", &temp, &pres,
&elev, &rwy, &wgt, &flp, &wet);
if ((flp == 1 || flp == 5 || flp == 15) &&
(41413 <= wgt && wgt <= 65000) &&
(wet == 0 || wet ==1) &&
(rwy > 6900)){
switch (flp) {
case 1: flp = 0; break;
case 5: flp = 1; break;
case 15: flp = 2; break;
}
int Xpref = calPref(elev, pres, temp);
if (Xpref != 1) {
calXvspd(Xpref, wgt, flp);
if ((Xvspd[0] != -1) && (Xvspd[1] != -1) && (Xvspd[2] != -1)) {
if (wet == 1) altXv1spd(rwy, flp);
printf("V1=%dkts Vr=%dkts V2=%dkts",
Xvspd[0], Xvspd[1], Xvspd[2]);
} else invalid();
} else invalid();
} else invalid();
return 0;
}
字符串替换
按理来讲只需找到子字符串位置,而后原字符串向后平移并复制即可。
起先怀疑输入缓冲区问题,但尝试fgets, fflush, getchar均未能AC,目前猜测为后台样例问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void strReplace(char *str, char *olds, char *news) {
int lenOld = strlen(olds), lenNew = strlen(news);
char *find = str;
while (*str) {
find = strstr(find, olds);
if (find == NULL) break;
memmove(find + lenNew, find + lenOld, strlen(find + lenOld) + 1);
memcpy(find, news, lenNew);
find += lenNew;
}
}
int main() {
char *str = (char*) calloc(1001, 1);
char *olds = (char*) calloc(1001, 1);
char *news = (char*) calloc(1001, 1);
char *ch = NULL;
ch = str;
while(((*ch = getchar()) != '\n') && (ch - str) <= 1000) ++ch;
*ch = '\0';
ch = olds;
while(((*ch = getchar()) != '\n') && (ch - olds) <= 1000) ++ch;
*ch = '\0';
ch = news;
while(((*ch = getchar()) != '\n') && (ch - news) <= 1000) ++ch;
*ch = '\0';
strReplace(str, olds, news);
puts(str);
return 0;
}
GPS通讯协议
这题还好,主要考察常规的字符串处理,但题目描述属于是经典的不知道从哪直接复制粘贴过来甚至连格式都懒得改的胡言乱语。
可能有部分极端情况未考虑。
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool isGPRMC(char str[]) {
return strstr(str, "GPRMC");
}
bool isEnd(char str[]) {
return strstr(str, "END");
}
int hexToDec(char str[]) {
int num = 0;
if ('A' <= str[0] && str[0] <= 'F') num += (str[0] - 'A' + 10) * 16;
else if ('0' <= str[0] && str[0] <= '9') num += (str[0] - '0') * 16;
if ('A' <= str[1] && str[1] <= 'F') num += (str[1] - 'A' + 10);
else if ('0' <= str[1] && str[1] <= '9') num += (str[1] - '0');
return num;
}
int strToDec(char str[]) {
int num = 0;
if ('0' <= str[0] && str[0] <= '9') num += (str[0] - '0') * 10;
if ('0' <= str[1] && str[1] <= '9') num += (str[1] - '0');
return num;
}
bool isValid(char str[]) {
int sum = str[1], curr;
for (curr = 2; str[curr] != '*'; ++curr) sum ^= str[curr];
return sum == hexToDec(str + curr + 1);
}
void UTCToISO(char str[]) {
int hour = (strToDec(str) + 8) % 24;
int minute = strToDec(str + 2);
int second = strToDec(str + 4);
printf("%d:%d:%d\n", hour, minute, second);
}
int main() {
char str[1000];
while (1) {
memset(str, 0, 1000);
fgets(str, 999, stdin);
if (isEnd(str)) break;
if (!isGPRMC(str)) continue;
if (!isValid(str)) {
printf("error\n");
continue;
}
UTCToISO(str + 7);
}
return 0;
}