算法是程序的靈魂,給妳壹些簡潔高效代碼:
第壹題:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char s[5];
for(i=32;i<=99;i++)
{
sprintf(s,"%d",i*i);
if(s[0]==s[1]&&s[2]==s[3])
printf("%s ",s);
}
system("pause");
return 0;
}
第二題:
智力遊戲九宮格簡練代碼
#include <stdio.h>
#include <stdlib.h>
int a[]={0,1,2,5,8,7,6,3}; /*指針數組.依次存入矩陣中構成環的元素下標*/
int b[9]; /*表示3X3矩陣,b[4]為空格*/
int c[9]; /*確定1所在的位置後,對環進行調整的指針數組*/
int count=0; /*數字移動步數計數器*/
void print(void) /*按格式要求輸出矩陣*/
{
int c;
printf(" >> Step No.%2d ",count++);
for(c=0;c<9;c++)
if(c%3==2) printf("%2d ",b[c]);
else printf("%2d",b[c]);
printf("\n");
}
int main()
{
int i,j,k,t;
system("cls");
printf(" >> Please enter original order of digits 1-8: ");
for(i=0;i<8;i++)
scanf("%d",&b[a[i]]);
/*順序輸入矩陣外邊的8個數字,矩陣元素的順序由指針數組的元素a[i]控制*/
printf("The sorting process is as felow:\n");
print();
for(t=-1,j=0;j<8&&t==-1;j++) /*確定數字1所在的位置*/
if(b[a[j]]==1) t=j; /*t:記錄數字1所在的位置*/
for(j=0;j<8;j++) /*調整環的指針數組,將數字1所在的位置定為環的首*/
c[j]=a[(j+t)%8];
for(i=2;i<9;i++) /*從2開始依次調整數字的位置*/
/*i:正在處理的數字,i對應在環中應當的正確位置就是i-1*/
for(j=i-1;j<8;j++) /*從i應處的正確位置開始順序查找*/
if(b[c[j]]==i&&j!=i-1) /*若i不在正確的位置*/
{
b[4]=i; /*將i移到中心的空格中*/
b[c[j]]=0;print(); /*空出i原來所在的位置,輸出*/
for(k=j;k!=i-1;k--) /*將空格以前到i的正確位置之間的數字依次向後移動壹格*/
{
b[c[k]]=b[c[k-1]]; /*數字向後移動*/
b[c[k-1]]=0;
print();
}
b[c[k]]=i; /*將中間的數字i移入正確的位置*/
b[4]=0; /*空出中間的空格*/
print();
break;
}
else if(b[c[j]]==i) break; /*數字i在正確的位置*/
system("pause");
return 0;
}
第三--第五題太沒挑戰性,無激情。
第三題:
#include <stdio.h>
#include <stdlib.h>
int isLetter(char c)
{
if((c>='A'&&c<='Z') || (c>='a'&&c<='z'))
return 1;
else
return 0;
}
void translate(char s[])
{
int i;
for(i=0;s[i]!='\0';i++)
if(isLetter(s[i]))
{
if(s[i]>='A'&&s[i]<='Z')
s[i]=(s[i]-'A'+1)%26+'A';/* 取模方法可避免字符出界 */
else
s[i]=(s[i]-'a'+1)%26+'a';
}
}
int main()
{
char ch,s[256];
puts("Please input your string:");
gets(s);
puts("Encrypt or not?(y/n):");
ch=getchar();
puts("The result is:");
if(ch=='Y'||ch=='y')
{
translate(s);
}
puts(s);
system("pause");
return 0;
}
第四題:
#include <stdio.h>
#include <stdlib.h>
void sort(int* p,int n)
{
int i,j,k,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
k=i;
if(*(p+k)>*(p+j))k=j;
if(k!=i)
{
temp=*(p+i);
*(p+i)=*(p+k);
*(p+k)=temp;
}
}
}
int main()
{
int a[1024],i,num;
printf("Please input numbers of array:");
scanf("%d",&num);
printf("Please input %d elements of array:\n",num);
for(i=0;i<num;i++)
scanf("%d",&a[i]);
sort(a,num);
printf("The result is:\n");
for(i=0;i<num;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
第五題:
#include <stdio.h>
#include <stdlib.h>
#define MAXN 20
#define MAXM 20
int a[MAXN][MAXM];
int main()
{
int i,j,m,n,min,max;
printf("Please input rank and column of matrix:");
scanf("%d%d",&m,&n);
printf("Please input %d elements of the matrix:\n",m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
max=min=a[0][0];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i][j]<min)
{
min=a[i][j];
}
else if(a[i][j]>max)
{
max=a[i][j];
}
}
printf("The minium is: %d\nThe maxium is: %d\n",min,max);
system("pause");
return 0;
}
------------------------------------------------------------
#include<iostream>
using std::endl;
using std::cout;
int main()
{
int a,b,c,d,i,n;
for(i=32;i<100;i++)
{
n=i*i;
a=n/1000;//整數除以整數結果當然是整數了
b=(n-a*1000)/100;
c=(n%100)/10;
d=n%10;
if(a==b && c==d)
cout<<n<<" ";//還沒計算怎麽知道答案只有壹個?
}
cout<<endl;
system("pause");
return 0;
}