1、输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。
#include <stdio.h>
void main()
{
int letter, space, digit, other;
char ch;
letter = space = digit = other = 0;
while ((ch = getchar ()) != '\n')
{
if (ch>='a' && ch <= 'z' || ch>='A'&&ch<='Z')
letter++;
else if (ch>='0' && ch <='9')
digit++;
else if (ch == ' ')
space++;
else
other++;
}
printf ("字母:%d\n", letter);
printf ("空格:%d\n", space);
printf ("数字:%d\n", digit);
printf ("其它字符:%d\n", other);
}
2、求下列试子的值:1-1/2+1/3-1/4+……+1/99-1/100,将结果输出。
#include<stdio.h>
int main()
{
int sign=1; //符号变量
double demo=2.0,sum=1.0,term;
while (demo<=100)
{
sign=-sign;
term=sign/demo;
sum=sum+term;
demo=demo+1;
}
printf("%f\n",sum);
return 0;
}
3、矩阵转置:将一个m行n列矩阵(即m×n矩阵)的每一行转置成另一个n×m矩阵的相应列
例如:将2*3的矩阵转置后输出:
即将 1 2 3 转置 1 4
4 5 6 2 5
3 6
#include<stdio.h>
int main()
{
int i, j, a[2][3] = { {1,2,3},{4,5,6} },b[3][2];
printf("array a:\n");
for (i = 0; i <=1; i++)//处理a数组中的一行中各元素
{
for (j = 0; j <= 2; j++)//处理a数组中的某一列元素
{
printf("%5d", a[i][j]);
b[j][i] = a[i][j];
}
printf("\n");
}
printf("array b:\n");
for (i = 0; i<=2; i++)
{
for (j = 0; j <=1; j++)
{
printf("%5d", b[i][j]);
}
printf("\n");
}
return 0;
}