考研算法38天:反序输出 【字符串的翻转】

题目

题目收获

很简单的一道题,但是还是有收获的,我发现我连scanf的字符串输入都忘记咋用了。。。。。我一开始写的

cpp 复制代码
#include <iostream>
#include <cstring> 
using namespace std;

void deserve(string &str){
	int n = str.size();
	int Size = n/2;
    for(int i=0;i<Size;i++){
        swap(str[i],str[n-1-i]);
    }
}

int main(){
    string str;
    while(scanf("%s",&str)!=EOF){
        deserve(str);
        printf("%s",str);
    }
    return 0;
}

结果发现咋搞都编译错误,查别人的博客发现别人和自己一样,最后就问了chat果然是自己记错了。。。。。。

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;

void reverse(char *str) {
    int n = strlen(str);
    int Size = n / 2;
    for (int i = 0; i < Size; i++) {
        swap(str[i], str[n - 1 - i]);
    }
}

int main() {
    char str[100];  // Assuming a maximum length of 100 characters
//输入不需要&这个符号
    while (scanf("%s", str) != EOF) {
        reverse(str);
        printf("%s ", str);
    }
    return 0;
}

好吧,哈哈哈哈哈。

AC代码

cpp 复制代码
#include <iostream>
#include <cstring> 
using namespace std;

void deserve(string &str){
	int n = str.size();
	int Size = n/2;
    for(int i=0;i<Size;i++){
        swap(str[i],str[n-1-i]);
    }
}

int main(){
    string str;
    while(cin>>str){
        deserve(str);
        cout<<str<<endl;
    }
    return 0;
}

事实证明scanf和printf是要比cin和cout快的。

相关推荐
KaMeidebaby1 小时前
卡梅德生物技术快报|小 RNA 适配体合成 + 多方法亲和力表征全流程标准化操作手册
前端·网络·数据库·人工智能·算法
是Dream呀1 小时前
基于深度学习的人类行为识别算法研究
人工智能·深度学习·算法
happyprince2 小时前
03_NVIDIA_ModelOpt-量化算法深入
人工智能·深度学习·算法
大鱼>2 小时前
AI+货物追踪:智能快递柜追踪系统
人工智能·深度学习·算法·机器学习
researcher-Jiang3 小时前
算法训练:堆 & 可并堆
算法
郝学胜-神的一滴3 小时前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
在书中成长3 小时前
HarmonyOS 小游戏《对战五子棋》开发第18篇 - 棋盘设计
算法·harmonyos
Frostnova丶3 小时前
(12)LeetCode 76. 最小覆盖子串
算法·leetcode·职场和发展
灯澜忆梦3 小时前
GO_函数_1
算法
言乐64 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构