提示:文章
文章目录
前言
前期疑问:
本文目标:
一、背景
最近
二、
2.1 坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29?tpId=37&tqId=21240&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "stdbool.h"
typedef struct {
char a;
int len;
} moveData;
bool isTargetStr(char* str) {
int len = strlen(str);
if(len < 2)
{
return false;
}
if (!isalpha(str[0])) {
return false;
}
for (int i = 1; i < len; i++) {
if (!isdigit(str[i])) {
return false;
}
}
return true;
}
void getData(char* str, char* c, int* len) {
*c = str[0];
*len = atoi(&str[1]);
}
int* calculateDst2(moveData* data, int count, int array[]) {
for (int i = 0; i < count; i++) {
if (data[i].a == 'a') {
array[0] = array[0] - data[i].len;
} else if (data[i].a == 'd') {
array[0] = array[0] + data[i].len;
} else if (data[i].a == 'w') {
array[1] = array[1] + data[i].len;
} else if (data[i].a == 's') {
array[1] = array[1] - data[i].len;
}
}
return array;
}
void calculateDst(moveData* data, int count, int (*array)[]) {
for (int i = 0; i < count; i++) {
if (data[i].a == 'a') {
*(*array) = *(*array) - data[i].len;
} else if (data[i].a == 'd') {
*(*array) = *(*array) + data[i].len;
} else if (data[i].a == 'w') {
*(*array + 1) = *(*array + 1) + data[i].len;
} else if (data[i].a == 's') {
*(*array + 1) = *(*array + 1) - data[i].len;
}
}
}
moveData data[10000];
int main() {
char str[10000];
char retStr[50] = {'\0'};
//while (gets(str) != NULL) { // 注意 while 处理多个 case
while (scanf("%s", str) != EOF) {
// 64 位输出请用 printf("%lld") to
int len = strlen(str);
char dstStr[10000] = {'\0'};
int moveDataIndex = 0;
int index = 0;
for (int i = 0; i < len ; i++) {
if (str[i] == ';') {
continue;
}
while (str[i] != ';') {
dstStr[index++] = str[i++];
}
index = 0;
if (isTargetStr(dstStr)) {
char charater;
int length;
getData(dstStr, &charater, &length);
data[moveDataIndex].a = tolower(charater);
data[moveDataIndex].len = length;
moveDataIndex++;
}
memset(dstStr, '\0', sizeof(dstStr));
}
int array[2] = {0};
calculateDst(data, moveDataIndex, &array);
snprintf(retStr, sizeof(retStr), "%d", array[0]);
int retStrIndex = strlen(retStr);
retStr[retStrIndex] = ',';
snprintf(&retStr[retStrIndex + 1], sizeof(retStr), "%d", array[1]);
printf("%s", retStr);
}
return 0;
}
下面是第一版有问题的版本
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "stdbool.h"
typedef struct {
char a;
int len;
} moveData;
bool isTargetStr(char* str) {
int len = strlen(str);
if (!isalpha(str[0])) {
return false;
}
for (int i = 1; i < len; i++) {
if (!isdigit(str[i])) {
return false;
}
}
return true;
}
void getData(char* str, char* c, int* len) {
*c = str[0];
//printf("%s\n", &str[1]);
*len = atoi(&str[1]);
}
int* calculateDst(moveData* data, int count, int array[]) {
for (int i = 0; i < count; i++) {
if (data[i].a == 'a') {
array[0] = array[0] - data[i].len;
} else if (data[i].a == 'd') {
array[0] = array[0] + data[i].len;
} else if (data[i].a == 'w') {
array[1] = array[1] + data[i].len;
} else if (data[i].a == 's') {
array[1] = array[1] - data[i].len;
}
}
return array;
}
moveData data[10000];
int main() {
char str[10000];
char retStr[50] = {'\0'};
//while (gets(str) != NULL) { // 注意 while 处理多个 case
while (scanf("%s", str) != EOF) {
// 64 位输出请用 printf("%lld") to
int len = strlen(str);
char dstStr[10000] = {'\0'};
int moveDataIndex = 0;
int index = 0;
for (int i = 0; i < len ; i++) {
if (str[i] != ';') {
dstStr[index++] = str[i];
}
else {
index = 0;
i++;
}
if (isTargetStr(dstStr)) {
char charater;
int length;
getData(dstStr, &charater, &length);
printf("%c %d", charater, length);
data[moveDataIndex++].a = tolower(charater);
data[moveDataIndex++].len = length;
}
}
int array[2] = {0};
//array = calculateDst(data, moveDataIndex, array);
calculateDst(data, moveDataIndex, array);
//printf("%d %d", array[0], array[1]);
snprintf(retStr, sizeof(retStr), "%d", array[0]);
int retStrIndex = strlen(retStr);
retStr[retStrIndex] = ',';
snprintf(&retStr[retStrIndex + 1], sizeof(retStr), "%d", array[1]);
//itoa(array[0], retStr, 10);
//retStr[1] = ',';
//retStr[2] = itoa(array[1]);
printf("%s", retStr);
}
return 0;
}
2.2
三、
3.1
总结
未完待续
标签:int,编程,retStr,len,牛客,str,array,data From: https://blog.csdn.net/2301_77560238/article/details/137037628