考研算法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快的。

相关推荐
郝学胜-神的一滴2 分钟前
Qt 高级编程 045:坐标体系深度实战
开发语言·c++·windows·python·qt·程序人生
码匠许师傅11 分钟前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式
a187927218311 小时前
【算法】动态规划第四篇:背包收官——min 哨兵、计数世界与组合排列分水岭
算法·leetcode·动态规划·dp·01背包·算法讲解·决策合并
2601_962297481 小时前
在python3中、下列输出变量a的正确写法是_2020超星大数据Python免费答案
数据结构·python·算法·编程·字符串操作
辰烨chenye2 小时前
LeetCode Hot 100 题解 · 子串篇
算法·leetcode·职场和发展
辰烨chenye2 小时前
LeetCode Hot 100 题解 · 堆篇
java·算法·leetcode
手写码匠2 小时前
华为云Flexus+DeepSeek征文|DeepSeek 应用监控告警体系实战:从“用户投诉才知道“到“故障前就发现“
人工智能·深度学习·算法·aigc
shehuiyuelaiyuehao3 小时前
算法29,前缀和,除自身以外的数组的乘积
java·数据结构·算法
智购科技自动售货机厂家3 小时前
2026自动售货机设备清洁效果自动验证:从图像比对到评分算法的工程实践~YH
人工智能·算法·计算机视觉
爱吃巧克力的程序媛3 小时前
main.cpp注册 C++ 类到 QML 元对象系统
c++·qt