⑴ c語言如何求迴文數
1、首先打開vc6.0,新建一個控制台項目。
⑵ C語言編程迴文數
main()
{
int x,n,i,j,a[20],c;
long sum;
scanf("%d",&n);
for (x=1;x<=n;x++)
{ sum=x*x;
for(i=1;i<20;i++)
a[i]=0;
for (i=1;i<20;i++)
{a[i]=sum%10;
sum=sum/10;
if (sum==0) break;
}
if (i%2==0) {c=0; for (j=1;j<=i/2;j++) {if(a[j]!=a[i+1-j]) c=1;}
if (c==0){printf("%d %d\n",x,x*x);}}
else {c=0; for (j=1;j<=(i-1)/2;j++) {if (a[j]!=a[i+1-j]) c=1;}
if (c==0){printf("%d %d\n",x,x*x);}}
}
}
⑶ C語言「迴文」程序代碼
首先我對你的 "並且當輸入的字元串第一個字元為#時,輸入為空時,不輸出。" 這句話比較費解, 不明白你的意思!
你把這說清楚了我再補充回答你~
我寫了個參考代碼給你參考, 首先是輸入你要判斷的字元串的個數, 然後再依次輸入所有的字元串, 最後判斷輸入的所有字元串是否是"迴文"! 因為不理解你那句話, 所以暫時沒做什麼空和什麼"#"處理.
詳細c代碼:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0, j = 0, n = 0;
int len = 0;
char *start = NULL;
char *end = NULL;
char str[STR_NUM][STR_LEN] = {0};
printf("Please input the number of string: \n");
scanf("%d", &n);
printf("Please input all the string: \n");
for (i = 0; i < n; i++)
{
scanf("%s", str[i]);
}
for (j = 0; j < n; j++)
{
start = str[j];
len = strlen(str[j]);
end = str[j] + len - 1;
while (start - end <= 0)
{
if (*start++ != *end--)
{
break;
}
}
if (start > end)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
例子,運行後:
Please input the number of string:
4
Please input all the string:
aba
112ds
madam
xyzyx
yes
no
yes
yes
Press any key to continue
補充回答:
大概明白你的意思,我會把例子貼上, 若不符合你的要求我再幫修改!
詳細代碼如下:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0;
int len = 0;
int str_count = 0;
char *start = NULL;
char *end = NULL;
char str[STR_NUM][STR_LEN] = {0};
printf("Please input all the string: \n");
while (1)
{
scanf("%s", str[str_count]);
if (str[str_count][0] == '#')
{
break;
}
else
{
str_count++;
continue;
}
}
for (i = 0; i < str_count; i++)
{
start = str[i];
len = strlen(str[i]);
end = str[i] + len - 1;
while (start - end <= 0)
{
if (*start++ != *end--)
{
break;
}
}
if (start > end)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
運行實例1:
Please input all the string:
xyzyx
adghf
#
yes
no
Press any key to continue
運行實例2:
Please input all the string:
1232ss
sakljfkla
333dafs
aba
ee3
xyzyx
dfj222
madam
111$111
slsl33
#
no
no
no
yes
no
yes
no
yes
yes
no
Press any key to continue
⑷ c語言中求1000以內的迴文數的程序
#include<stdio.h>
int main()
{
int i,n,m,count=0;
printf("所有的迴文數字如下:\n");
for(i=1;i<=1000;i++)
{
n=i;
m=0;
while(n)
{
m=m*10+n%10;
n/=10;
}
if(m==i)
{
printf("%d ",m);
count++;
}
}
printf("\n共%d個\n",count);
getchar();
return 0;
}
⑸ 用c語言寫迴文數,怎麼寫啊/急求!!
/編寫一個迴文數的程序c語言編程
#include <stdio.h>
void main()
{
int n, m=0, count=0;
printf("請輸入一個數:\n");
scanf("%d", &n);
for(n=1; n<=10000; n++)
{
while(n>0)
{
m=m*10+n%10;
n=n/10;
}
if(m==n)
{
count++;
printf("%3d", n);
}
if(count%5==0)
printf("\n");
}
}
我寫的是找出1到10000的迴文數,不過是在沒有vc++壞境下寫的,代碼還美調試,自己運行一下看看。