回复 楼主 房房
题4从小到大排序
#include <stdio.h>
int main()
{
int b[5],i,j,t
;
for (i = 0; i<5; i++)
{
scanf("%d",&b[i]);
}
for (i = 0; i<4; i++)
{
for (j =i+1; j<5; j++)
{
if (b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
for (i = 0; i<5; i++)
{
printf("%d ",b[i]);
}
return 0;
}
题6:杨辉三角
#include<iostream.h>
void tri(int a[][15]);
void main()
{int i,j,a[15][15];tri (a);
cout<<"a= ";for(i=0;i<15;i++)
{for(j=0;j<=i;j++)
{cout<<a[i][j];
if(a[i][j]>=1&&a[i][j]<=9)//当输出个位数之后输出4个空格保持整齐
cout<<" ";
else if (a[i][j]>=10&&a[i][j]<=99)//当输出十位数之后输出3个空格保持整齐
cout<<" ";
else if(a[i][j]>=100&&a[i][j]<=999)//当输出百位数之后输出2个空格保持整齐
cout<<" ";
else cout<<" ";//当输出百位数之后输出1个空格保持整齐
}
cout<<endl<<" ";//每行输出结束后换行
}}
void tri(int a[15][15])
{int i,j;
for(i=0;i<15;i++)
for(j=0;j<=i;j++)
{if(j==0||j==i)//三角形第一列和对角线被赋值为
a[i][j]=1;
else a[i][j]=a[i-1][j-1]+a[i-1][j];//算出其余的数组元素
}
}
字符串逆序:
#include <stdio.h>
int main(void)
{
int i, j, temp;
char str[80];
i = 0;
while((str[i] = getchar( )) != '\n')
i++;
str[i] = 0;
for (j=0;j<i/2;j++)
{
temp=str[i-j-1];
str[i-j-1]=str[j];
str[j]=temp;
}
for(i = 0; str[i] != 0; i++)
putchar(str[i]);
}