Manchester


私信TA

用户名:wenyajie

访问量:312845

签 名:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

等  级
排  名 1
经  验 62755
参赛次数 1
文章发表 188
年  龄 0
在职情况 学生
学  校 Xiamen University
专  业 计算机科学

  自我简介:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

解题思路:
输入字符串,逆序输出;
用栈思路(c++)自己运行可以,提交时间超限,别提交;

#include<stack>
#include<iostream>
using namespace std;
int main()
{
stack<char> s;
char c;
while((c=getchar())&&c!='\n')
{
s.push(c);
}
while(s.empty()!=true)
{
 cout<<s.top();
 s.pop();
}
return 0;
}



注意事项:
这样写运行没错,但是提交提示指针越界:(知识不够无法理解)

#include<stdio.h>
#include<string.h>
int main()
{
char a[102],c;
int j=0;
while((a[j]=getchar())&&a[j]!='\n')
  j++;
  a[j]='\0';

for(int i=strlen(a)-1;i>=0;i--)   
 printf("%c",a[i]);              
 return 0;
}

测试时,这里strlen(a)后加上-1,1,2...(codeblocks)等都能有正确结果,匪夷所思,但提交越界;


下面这样写提交正确,但是codeblocks提示:gets为危险函数,不应该被使用:

#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
gets(a);
for(int i=strlen(a)-1 ;i>=0;i--)
putchar(a[i]);
}


参考代码:

#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
gets(a);
for(int i=strlen(a)-1 ;i>=0;i--)
putchar(a[i]);
}

这个可以。

 

0.0分

10 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

求大佬解释一下为什么错了#include<stdio.h>
int main()
{
	char str[101],s;
	int i=0;
	scanf("%c",&s) ;
	while(s!='\n') 
	{
		str[i] = s;
		i++;
		scanf("%c",&s) ;
	}
	i = i-1;
	while(i>=1)
	{
		printf("%c",str[i]);
		i--;
	}
	printf("%c",str[i]);
	return 0;
}
2020-01-12 19:46:07
str的长度不超过100个字符。   
char a[100];

明显是错误的
2019-06-30 14:59:33
能问一下我的   #include <string.h>   不能编译通过怎么办?   谢谢
2019-02-01 11:21:36
为什么上面写的这个代码在Xcode里运行不了呢,除了会警告gets不安全
它还会显示:I am a student
                  error: empty command
                  error: 'I' is not a valid command.
                  error: Unrecognized command 'I'.
                  (lldb) 
为什么呢?
2019-01-28 00:58:15
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int main()
{
    int m;
    char a[1000];
    gets(a);
    m=strlen(a);
    while(m--)
        cout<<a[m];
    return 0;
}
2019-01-19 11:09:37
i=strlen(a)-1 这为什么要-1啊
2019-01-08 17:03:02
//如果用c++应该这样
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() 
{
    string str;           //Declare a string 
    getline(cin,str);  //Input a line
    reverse(str.begin(),str.end()); //make the string reverse
    cout<<str;                             //Output
    return 0;
}
2018-12-22 17:09:03
  • «
  • 1
  • »