401 - Palindromes(回文串水题)

VJ上交题地址:https://vjudge.net/problem/UVA-401

Description

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.

 

A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I" are their own reverses, and "3" and "E" are each others' reverses.

A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.

Of course,"A","T", "O", and "Y" are all their own reverses.

A list of all valid characters and their reverses is as follows.

CharacterReverseCharacterReverseCharacterReverse
AAMMYY
B N Z5
C OO11
D P 2S
E3Q 3E
F R 4 
G S25Z
HHTT6 
IIUU7 
JLVV88
K WW9 
LJXX  

Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.

Input

Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.

Output

For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.

STRINGCRITERIA
" -- is not a palindrome."if the string is not a palindrome and is not a mirrored string
" -- is a regular palindrome."if the string is a palindrome and is not a mirrored string
" -- is a mirrored string."if the string is not a palindrome and is a mirrored string
" -- is a mirrored palindrome."if the string is a palindrome and is a mirrored string

Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.

In addition, after each output line, you must print an empty line.

Sample Input

NOTAPALINDROME 
ISAPALINILAPASI 
2A3MEAS 
ATOYOTA

Sample Output

NOTAPALINDROME -- is not a palindrome.
 
ISAPALINILAPASI -- is a regular palindrome.
 
2A3MEAS -- is a mirrored string.
 
ATOYOTA -- is a mirrored palindrome.

Hint

use the C++'s class of string will be convenient, but not a must

题意:

给定一个字符串,看这个字符串是不是回文,是不是镜像,是不是镜像回文。

题目很水,?,注意一下格式就可以。

#include<iostream>
#include<cstring>
char a[1010];
int main()
{
  while (gets(a))
  {
    int tip1=0;//是不是回文。
    int tip2=0;//是不是镜像。
    int len=strlen(a);
    for (int i=0;i<len/2+1;i++)
    {
      if (a[i]==a[len-i-1])
      {
        if (a[i]=='A'||a[i]=='H'||a[i]=='I'||a[i]=='M'||a[i]=='O'||a[i]=='T'||a[i]=='U'||a[i]=='V'||a[i]=='W'||a[i]=='X'||a[i]=='Y'||a[i]=='1'||a[i]=='8')
        {
        continue;
        }
        else
        {
        tip2=1;
        }
      }
      else
      {
        tip1=1;
        if ((a[i]=='E'&&a[len-i-1]=='3')||(a[i]=='3'&&a[len-i-1]=='E')||(a[i]=='J'&&a[len-i-1]=='L')||(a[i]=='L'&&a[len-i-1]=='J')||(a[i]=='S'&&a[len-i-1]=='2')||(a[i]=='2'&&a[len-i-1]=='S')||(a[i]=='Z'&&a[len-i-1]=='5')||(a[i]=='5'&&a[len-i-1]=='Z'))
        {
          continue;
        }
        else
        {
          tip2=1;
        }
      }
    }
    printf("%s",a);
    if (tip1==0&&tip2==0) printf(" -- is a mirrored palindrome.\n");
    else if (tip1==0&&tip2==1) printf(" -- is a regular palindrome.\n");
    else if (tip1==1&&tip2==0) printf(" -- is a mirrored string.\n");
    else printf(" -- is not a palindrome.\n");
    printf("\n");
  }
  return 0;
}

 

转载于:https://www.cnblogs.com/bendandedaima/p/8762848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目描述: 给定一个整数 $N$,求出大于 $N$ 的最小的既是质数又是回文数的数。 回文数指的是正着读和倒着读都一样的数字,例如 12321 就是一个回文数。 输入格式: 输入共 1 行,包含一个整数 $N$。 输出格式: 输出共 1 行,包含一个整数,表示题目所求的数。 数据范围: $1≤N≤10^7$ 样例: 输入: 31 输出: 101 解题思路: 从 $N$ 开始遍历,判断每一个数是否既是质数又是回文数。如果找到了这样的数,直接输出即可。 判断是否为质数可以用较为简单的暴力算法,枚举 $2$ 到 $\sqrt{x}$ 之间的所有数,看是否存在约数。 判断是否为回文数可以将该数转化为字符串,然后比较正序字符串和倒序字符串是否相等即可。 注意,本题所求的数可能非常大,需要使用 long long 类型存储,并且需要使用快速幂算法来快速计算幂次。同时,因为奇数位的回文数一定不是 11 的倍数,因此可以只枚举奇数位的回文数。 ### 回答2: 题目要求找出范围在2到N(包括2和N)之间的回文质数。所谓回文质数是指既是质数又是回文数的数。质数是指除了1和自身以外没有其他因数的正整数。 首先,我们先定义两个函数:一个是用来判断一个数是否为质数的函数is_prime,另一个是用来判断一个数是否为回文数的函数is_palindrome。 is_prime函数的实现方法如下:从2到该数的平方根进行遍历,判断是否存在该数的因数,如果存在则返回False,代表不是质数,如果遍历结束都没有找到因数,则返回True,代表是质数。 is_palindrome函数的实现方法如下:将该数字转化为字符串,并判断该字符串与其翻转后的字符串是否相等,如果相等则返回True,代表是回文数,否则返回False,代表不是回文数。 接下来,我们在范围从2到N进行遍历,对每个数字都进行is_prime和is_palindrome的判断,如果都满足条件,则将该数字输出。 下面是代码实现的伪代码: ``` function is_prime(num): if num < 2: return False for i in range(2, int(num**0.5)+1): if num % i == 0: return False return True function is_palindrome(num): num_str = str(num) if num_str == num_str[::-1]: return True return False function prime_palindromes(N): for num in range(2, N+1): if is_prime(num) and is_palindrome(num): print(num) ``` 以上是本题的解题思路和伪代码实现,希望能对你有所帮助。 ### 回答3: 题目要求找出所有小于等于N的回文质数。 回文数是指正读反读都相同的数,例如121、12321都是回文数。质数是只能被1和自身整除的数,例如2、3、5、7都是质数。 首先,我们可以编写一个函数来判断一个数是否为质数。函数的输入是一个正整数n,判断n是否能被小于n的所有数整除,如果能则返回False,否则返回True。 接下来,我们可以编写一个函数来判断一个数是否为回文数。函数的输入是一个正整数n,将n转换成字符串并反转,然后与原字符串进行比较,如果相同则返回True,否则返回False。 在主函数中,我们可以遍历1到N之间的所有数,对于每个数,首先判断是否为回文数,如果不是则跳过;然后判断是否为质数,如果是则输出该数。 最后,我们可以将上述步骤封装成一个循环,将N从2逐渐增加,直到N超过题目要求的上限。 以下是代码实现: def is_prime(n): for i in range(2, n): if n % i == 0: return False return True def is_palindrome(n): s = str(n) if s == s[::-1]: return True return False N = int(input()) for n in range(2, N + 1): if is_palindrome(n) and is_prime(n): print(n) 希望能够帮助你解答问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值