How to make the strings upside down

来源:互联网 发布:mvc php路由文件加载 编辑:程序博客网 时间:2024/06/05 19:03

Problem description  The children often play a game like this.When one child say some words like "abc",the other must say "cba" as soon as possible.When we take them in the computer,it will give the correct answer quickly. But in China,there is a big problem when we play the game.The ideogram covers different bits from English letter.That is to say, we will consider that how to give a new string which is read from back to front.Meanwhile,don't change any ideograms.
Input  Standard input will contain multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow, each string is on a separate line and consists of at most 1,000 letters or ideograms.
Output  The output will contain a new string which is read from back to front.
Sample Input

1Acm 程序设计大赛
Sample Output
赛大计设序程 mcA

【注】

这个题目的关键当然在于输出汉字,倒序输出没有问题。

利用汉字编码为负数的特点判断为汉字的倒序连续输出两个字符即可。

#include <cstring>#include <cstdio>int main(){char s[2048];int n;scanf("%d\n", &n);while (n--)    {        gets(s);        int m = strlen(s);        for (int i = m - 1; i >= 0; i--)        {            if(s[i] < 0 && s[i-1] < 0)            {                printf("%c%c",s[i-1],s[i]);                i-=1;            }            else                printf("%c",s[i]);        }        printf("\n");    }return 0;}

【Learn】:

1.汉字输出%c%c且编码为负

2.cout 不能与printf 混用,否则提交时会出现PE

3.strrev函数可以用于一般的倒序

4.scanf("%d\n",&n);与scanf("%d",&n);getchar;空格与换行

//感谢LuoXun老师!



//【函数示例】#include<stdio.h>#include<string.h>int main(){  char str1[] = "abcxyz";// 若改为 char *str1 = "abcxyz";,程序在运行时会崩溃  char *ret1 = strrev(str1);  printf("The origin string of str1 is: %s\n", str1);  printf("The reverse string of str1 is: %s\n", ret1);  return 0;}//运行结果://The origin string of str1 is: zyxcba//The reverse string of str1 is: zyxcba

The end.



0 1
原创粉丝点击