逆序输出十个数
for (i = 0; i < 10; i++)
a[i] = i;
for (i = 9; i >= 1; i--)
printf("%d", a[i]);
斐波那契数列前20项输出
int i, f[20] = {
1,1};
for (i = 2; i < 20; i++)
f[i] = f[i-1]+f[i-2];
for (i = 0; i < 20; i++)
{
if (i % 5 == 0)printf("\n");
printf("%12d", f[i]);
}
升序输出
for (i = 0; i < 10; i++)
scanf_s("%d", &a[i]);
printf("\n");
for (i = 0; i < 9; i++)//涉及到i+1,如果不减去1,则会溢出
for (j = 0; j < 9 - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
for (i = 0; i < 10; i++)
printf("%d\t", a[i]);
矩阵行列互换
int a[2][3] = {
{
1,2,3},{
4,5,6} };
int b[3][2], i, j;
printf("数组a\n");
for (i = 0; i <= 1; i++)
{
for (j = 0; j <= 2; j++)
{
printf("%5d", a[i][j]);
b[j][i] = a[i][j];
}
printf("\n");
}
printf("数组b\n");
for (i = 0; i <= 2; i++)
{
for(j=0;j<=1;j++)
printf("%5d", b[i][j]);
printf("\n");
}
输出3*4矩阵中最大项以及下标
int a[3][4] = {
{
1,2,3,4},{
9,8,7,6},{
-10,10,-5,2} };
max = a[0][0];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
if (a[i][j] > max)
{
max = a[i][j];
row = i;
colum = j;
}
}
}
printf("max=%d\nrow=%d\ncolum=%d\n", max, row, colum);
输出字符组
int i;
for (i = 0; i < 15; i++)
{
printf("%c", c[i]);
}
printf("\n");
输入一行字符,计数单词的数目
int i, num = 0, word = 0;
char c;
gets_s(string);
for (i = 0; (c=string[i])!='\0'; i++)//括号的位置非常容易出现错误
{
if (c