Advertisement

上海计算机学会2020年9月月赛C++丙组T2中心对称数

阅读量:
题目背景

在超市里,有一些价格标签倒置后,数字竟不会发生改变。转置 180 度后不变的十进制数字被称为中心对称数(Strobogrammatic Numbers)。下图分别给出 0 到 9 这十个数字倒置后的样子:
WechatIMG663.png

题目描述

中心对称数 是指沿中心旋转 180 度后不变的十进制正整数。

  • 0,1,8 旋转后不变;
  • 6 旋转后为 9,9 旋转后为 6;
  • 其他数字旋转后不能构成合理的数字。

给定一个正整数 s,请判断 s 是否是中心对称数。注意有一部分输入将会非常大。

输入格式

单个整数:表示 s。

输出格式
  • 如果输入的数字是中心对称数 ,输出 Strobogrammatic number
  • 否则输出 Not a strobogrammatic number
数据范围

记 s 的十进制长度为 n,

  • 对于 50%50% 的数据,1≤n≤10;
  • 对于 100%100% 的数据,1≤n≤10000。
样例数据

输入:

18081

输出:

Strobogrammatic number

输入:

666

输出:

Not a strobogrammatic number

输入:

4287

输出:

Not a strobogrammatic number

题解

本题关键点:新定义一个数组c,记录反转后仍旧是数字的,排除反转后不合理的数字。最后再判断是否为回文数。代码如下。

复制代码
 #include <iostream>

    
 #include <string>
    
 using namespace std;
    
 int main(){
    
 	int len; 
    
 	char c[10];
    
 	string s;
    
 	cin>>s;
    
 	len=s.length();
    
 	c[0]='0';
    
 	c[1]='1';
    
 	c[8]='8';	
    
 	c[6]='9';
    
 	c[9]='6';
    
 	for(int i=0;i<len;i++){	
    
 		if(s[i]=='2' || s[i]=='3' || s[i]=='4' || s[i]=='5' || s[i]=='7'){
    
 			cout<<"Not a strobogrammatic number"<<endl;
    
 			return 0;					
    
 		}else if (!(s[i]==c[s[len-i-1]-'0'])) {
    
 			cout<<"Not a strobogrammatic number"<<endl;
    
 			return 0;
    
 		}		
    
 	}		
    
 	cout<<"Strobogrammatic number"<<endl;	
    
 	return 0;
    
 }
    
    
    
    

全部评论 (0)

还没有任何评论哟~