6.编程输出以下的杨辉三角形(输出前10行) 源程序: #include
{int i,j,a[10][10]; for(i=0;i<10;i++) {a[i][0]=1; a[i][i]=1;}
for(i=2;i<10;i++) for(j=1;j
a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<10;i++) {for(j=0;j<=i;j++) printf(\printf(\}
运行结果: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1
7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1 9 36 84 126 126 84 36 Press any key to continue
1 9 1 指针作业:
1比较n(n<10)个字符串排序输出 源程序:
#include
void sort(char array[][20],int n); void main() {char str[10][20]; int i,j,k,n;
printf(\scanf(\
printf(\for(i=0;i<=n;i++) gets(str[i]); sort(str,n);
for(i=0;i<=n;i++)
puts(str[i]);}
void sort(char array[][20],int n) {char temp[20]; int i,j,k;
for(i=1;i<=n-1;i++)
{k=i;
for(j=i+1;j<=n;j++)
if(strcmp(array[k],array[j])>0) k=j;
if(k!=i)
{strcpy(temp,array[i]); strcpy(array[i],array[k]); strcpy(array[k],temp);} }
}
运行结果: input n(n<=10):5 input 5 string: girl boy student teacher docter boy docter girl student
teacher
Press any key to continue
方法二: 源程序: #include
{char *str[N]={\int i,j,k; char *temp; for(i=0;i for(j=i+1;j temp=str[i];str[i]=str[k];str[k]=temp;} for(i=0;i printf(\} 运行结果: boy doctor girl student teacher Press any key to continue 2 用函数实现对变量的交换。 源程序; #include temp=*p1;*p1=*p2;*p2=temp;} void main() {int a=2,b=5; swap(&a,&b); printf(\运行结果; 5,2Press any key to continue 3,输入n个学生姓名,数学成绩,英语成绩,并按这两门成绩的平均分从小到大输出。 源程序: #include struct student{char name[5];float math,eng;float aver;}; void main() {struct student stu[5],temp; int i,sub,k; float aver=0; for(i=0;i<5;i++) {scanf(\stu[i].aver=(stu[i].math+stu[i].eng)/2.0;} for(i=0;i<4;i++) {sub=i; for(k=i+1;k<5;k++) if(stu[k].aver {printf(\printf(\运行结果; 输入: zs 50 90 ls 60 60 wr 80 80 zh 70 50 qr 65 65 输出: ls60.0 60.0 zh70.0 50.0 qr65.0 65.0 zs50.0 90.0 wr80.0 80.0 Press any key to continue