首页 > 试题广场 >

反序输出

[编程题]反序输出
  • 热度指数:31678 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入任意4个字符(如:abcd), 并按反序输出(如:dcba)

输入描述:
题目可能包含多组用例,每组用例占一行,包含4个任意的字符。


输出描述:
对于每组输入,请输出一行反序后的字符串。
具体可见样例。
示例1

输入

Upin
cvYj
WJpw
cXOA

输出

nipU
jYvc
wpJW
AOXc
当成一道算法题ok?
#include<iostream>
using namespace std;
int main(){
    char a[4];
    int n = sizeof(a);
    while(gets(a)){
        for(int i=0;i<n/2;i++){
            char temp = a[n-1-i];
            a[n-1-i] = a[i];
            a[i] = temp ;
        }
        for(int i=0;i<n;i++){
            cout<<a[i];
        }
        cout<<endl;
    }
    return 0;
}

发表于 2017-07-24 18:42:43 回复(2)
main(){
    char str[4];
    for(;~scanf("%s",&str)&&printf("%c%c%c%c\n",str[3],str[2],str[1],str[0]););
}

编辑于 2018-01-11 18:42:48 回复(0)
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		while(in.hasNext()){//循环输入
			String str=in.nextLine();//字符输入
			char array[]=str.toCharArray();//字符串转换成数组
			for(int i=array.length-1;i>=0;i--){//倒序输出
				System.out.print(array[i]);
			}
			System.out.println();
		}
		in.close();
	}
}
//本题也可以尝试用栈做 
发表于 2016-04-14 16:44:27 回复(0)
void print(char *str,int n);
int main(void)
{
    int i;
    char str[10];
    while(scanf("%s",&str)!=EOF)
    {
        //for(i=3;i>=0;i--)
        //    printf("%c",str[i]);
        print(str,3);
        printf("\n");
    }
    
    return 0;
}

void print(char *str,int n)
{
    if(n>=0)
    {
        printf("%c",*(str+n));
        print(str,n-1);
    }
}

发表于 2020-04-24 11:52:45 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            System.out.println(new StringBuilder(scanner.next()).reverse().toString());
        }
    }
}


发表于 2020-03-07 21:46:41 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    string temp;
    while(cin>>temp){
        reverse(temp.begin(),temp.end());
        cout<<temp<<endl;
    }
    return 0;
}

发表于 2020-02-27 00:52:45 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main() {
	string input;
	while (cin >> input) {
		for (int i = 0; i < 4; i++) {
			cout << input[3 - i];
		}
		cout << endl;
	}
	return 0 ;
}

发表于 2020-02-13 22:41:32 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            StringBuilder sb = new StringBuilder();
            System.out.println(sb.append(sc.nextLine()).reverse());
        }
    }
}

发表于 2018-08-03 19:56:49 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Six{
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        StringBuffer sb = new StringBuffer();

        try {
            while ((str=bufferedReader.readLine())!=null){
                System.out.println(sb.append(str).reverse().toString());
                sb.replace(0,sb.length(),"");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

发表于 2017-11-16 10:38:30 回复(0)
#include <iostream>
using namespace std;
int main(){
    string a,b;
    while(cin>>a){
        for(int i=3;i>=0;i--)b+=a.at(i);    
        cout<<b<<endl;
        b="";
    }
    return 0;
}

发表于 2017-02-24 20:37:11 回复(0)

python solution:

while True:
    try:
        print(input()[::-1])


    except:
        break
发表于 2017-10-04 11:12:01 回复(2)
#include<iostream>
using namespace std;
int main()
{
	char str[4];
	while (cin>>str)
	{
		cout<<str[3]<<str[2]<<str[1]<<str[0]<<endl;
	}
	return 0;
}

发表于 2016-10-10 21:04:59 回复(6)
#include<stdio.h>
#include<string.h>
int main()
{
    char a[100];
    int i;
    while(scanf("%s",a)!=EOF)
    {
        for(i=strlen(a)-1;i>=0;i--)
            printf("%c",a[i]);
        printf("\n");
    }
    return 0;
}
发表于 2018-01-13 15:30:41 回复(0)
//先接收一下字符串,再按相反顺序遍历一下,输出
#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    string str;
    while(cin>>str){
        for(int i=3; i>=0; i--){
            cout<<str[i];
        }
        cout<<endl;
    }
    return 0;
}

发表于 2020-02-13 22:24:22 回复(0)
#include<iostream>
using namespace std;
int main()
{
    char ch[4];
    int n=4;
    while(cin>>ch)
    {
        while(n)
        {
            cout<<ch[n-1];
            n--;
        }
    }
    return 0;
}


发表于 2018-03-18 12:00:30 回复(1)
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(){
    string str;
    while(cin>>str){
        reverse(str.begin(),str.end());
        cout<<str<<endl;
    }
    return 0;
}

发表于 2017-10-20 17:25:15 回复(1)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
         Scanner sc=new Scanner(System.in);
         while(sc.hasNext()){
             StringBuilder sb=new StringBuilder(sc.nextLine());
             System.out.println(sb.reverse().toString());
             
             
             
         }
        
        
        
        
        
    }
    
}

发表于 2016-10-09 16:30:59 回复(1)
#include <stdio.h>

int main() {
    char str[4];
    char s;
    int i = 0;
    while(scanf("%c",&s)!=EOF){
        if(i<4){
            str[i] = s;
            i++;
        }
        else{
            for(int j = 3;j>=0;j--){
                printf("%c",str[j]);
            }
            printf("\n");
            i=0;
        }
    }
   
    return 0;
}
发表于 2023-02-15 17:55:30 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    cin >> str;
    swap(str[0], str[3]);
    swap(str[1], str[2]);
    cout << str << endl;
    return 0;
}

编辑于 2024-01-28 21:57:53 回复(0)
#include <cstdio>
#include <string>
using namespace std;

int main(){
    char arr[5];
    char str[5];
    while(scanf("%s",arr) != EOF){
        for(int i = 0; i < 4; ++i){
            str[3-i] = arr[i];
        }
        printf("%s\n",str);
    }
    return 0;
}

发表于 2023-03-14 19:47:57 回复(0)