对于三位数来说,只要百位和个位相同就是回文数,根据此思路,写程序如下:
#include
int
main()
{
for(int
i=100;i
#include <stdioh>
#include <stringh>
#include <ctypeh>
int ispalindrome(char str) {
int s=0,e=strlen(str)-1,m=(s+e)/2;
while(s<=m&&str[s]==str[e])
{
s++;
e--;
}
if(s>m)
return 1;
return 0;
}
int main(void) {
char buffer[1024];
gets(buffer);
printf("[%s] is ", buffer);
if (ispalindrome(buffer)) puts("a palindrome");
else puts("not a palindrome");
return 0;
}
前两天刚好帮人写了个程序, 两个解决方案, 一个把数字转换成字符串,然后比较; 另一个则是直接数字判断, 不受位数的限制(基本上是unsigned long的上限,即最大值4294967295 (10位))
#include <stdioh>
#include <stdlibh>
#include <stringh>
#define MAXDIGITS 10
int IsPalindromeNumber(const unsigned long num);
int IsPalindromeString(const char szstr[], const unsigned int szlen);
int main(int argc, char argv[])
{
unsigned long l;
char sznum[MAXDIGITS] = {'\0'};
int sum;
int flag = 0;
printf("Printing reverse number from 1 to 99999\n");
sum = 0;
for (l=1; l < 100000; l++)
{
sprintf(sznum, "%lu\0", l);
//flag = IsPalindromeString(sznum, strlen(sznum));
flag = IsPalindromeNumber(l);
if (flag) {
printf("%d\n", l);
sum++;
}
}
printf("Sum = %d\n", sum);
return 0;
}
int IsPalindromeNumber(const unsigned long num)
{
unsigned long rev_num, tmp_num;
unsigned long num_dup;
if (num < 10)
return 0;
num_dup = num;
rev_num = 0;
while (num_dup !=0 )
{
tmp_num = num_dup % 10;
num_dup = num_dup / 10;
rev_num = rev_num 10 + tmp_num;
}
return ((rev_num == num)1:0);
}
int IsPalindromeString(const char szstr[], const unsigned int szlen)
{
int retcode = 1; / return code, 1 means TRUE /
int i, j;
if (szlen <= 1)
return 0;
for (i=0,j=szlen-1; i<j; i++,j--)
{
if (szstr[i] != szstr[j])
{
retcode = 0;
break;
}
}
return retcode;
}
IsPalindromeNumber -- 直接数字转换
IsPalindromeString -- 转换成字符串, 然后判断。
#include <stdioh>
#include <stdlibh>
#include<stringh>
int main()
{
int low;
int high;
long num;
int i;
char tmp[8];
int sts;
memset(tmp,0,sizeof(tmp));
for(i=1;i<=256;i++){
num = ii;
sprintf(tmp,"%ld",num);
if(strlen(tmp) < 2){
continue;
}
sts = 0;
low = 0;
high = strlen(tmp)-1;
while(high>=low){
if(tmp[high]==tmp[low]){
low = low +1;
high = high -1;
}else{
sts = 1;
break;
}
}
if(sts == 0){
printf("the x = %3d,xx=%6ld\n",i,num);
}
}
return 0;
}
char buf[50], bufch;
int i = 0, n = 0;
while((bufch = getchar()) != '@')
{
buf[n] = bufch;
n++;
}
for(; i < n/2; i++)
{
if(buf[i] != buf[n - 1 - i])
return 0;
}
return 1;
以上就是关于c语言写程序,求100到9000之内的回文数。(用双重循环)全部的内容,包括:c语言写程序,求100到9000之内的回文数。(用双重循环)、c语言回文补充程序、C语言怎么编写一个程序输出所有5位数字的回文(急需用)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)