小白求助判断回文数为啥没法这样写!
  • 板块学术版
  • 楼主77Glorious
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/23 21:19
  • 上次更新2023/11/24 09:27:46
查看原帖
小白求助判断回文数为啥没法这样写!
956316
77Glorious楼主2023/11/23 21:19

感觉无论输入什么都不会是回文数......

#include<bits/stdc++.h>
using namespace std;
bool hw(string a){//判断是否回文数
	int zjx;//中间项	 
	if(a.length()%2==0){
		bool pd;//判断 
		zjx=a.length()/2-1;//偶数位数字中间项下标就是长度/2-1,如2 4 4 2的中间项设为第一个4 
		for(int i=0,j=a.length()-1;i<=zjx,j>zjx;i++,j--)
			if(a[i]==a[j]) pd=1; else pd=0; return 0;		
	}
	else if(a.length()%2==1){
		bool pd;
		zjx=(a.length()-1)/2;//奇数位中间项下标,如2 4 5 4 2的中间项设为5 
		for(int i=0,j=a.length()-1;i<zjx,j>zjx;i++,j--)

			if(a[i]==a[j]) pd=1; else pd=0; return 0;
	}
	return 1;
}
int main(){
	string a;
	cin>>a;
	if(hw(a)) cout<<"你是回文数";
	else cout<<"你不是回文数"; 
	
	return 0; 

}
2023/11/23 21:19
加载中...