/*
1.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。
*/
#include <stdio.h>
main()
{
char c;
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
printf("请您输入一行字符:\n");
//字符串接收
//c = getchar();
//while(c!='\n')
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
{
letter++;
}
else if(c ==' ')
{
space++;
}
else if(c>='0'&&c<='9')
{
num++;
}else
{
other++;
}
}
printf("letter = %d\nspace = %d\nnum = %d\nother = %d\n",letter,space,num,other);
return 0;
}
/*
2.求下列试子的值:1-1/2+1/3-1/4+……+1/99-1/100,将结果输出。
分析:提取每一项式子1.0/(i+1),当i为偶数(i%2==0)时取正号,为奇数(i%2!=0)时取负号
*/
#include <stdio.h>
main(){
double sum = 0;
int i ;
for(i = 0;i<100;i++)
{
//sum+=(1-double(1/(i++))*((-1)^(i-1))); //出错
if(i%2==0)
{
sum = sum +1.0/(i+1);
continue;//跳出当次循环,循环体内后面的代码都不会执行,循环变量改变一次继续判断
}else
{
sum = sum - 1.0/(i+1);
continue;
}
}
printf("1-1/2+1/3-1/4+……+1/99-1/100 = %f\n",sum);
}
/*
3、矩阵转置:将一个m行n列矩阵(即m×n矩阵)的每一行转置成另一个n×m矩阵的相应列
*/
#include <stdio.h>
main()
{
int array[3][4]={0};
int arrayRev[4][3]={0};
int i;
int j;
//源数组赋值
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&array[i][j]);
}
}
//输出反转前的矩阵
printf("反转前的矩阵:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%3d",array[i][j]);
}
printf("\n"); //一行数据打印完换行
}
//输出反转后的矩阵
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
arrayRev[j][i] = array[i][j];
}
}
printf("转置后:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",arrayRev[i][j]);
}
printf("\n"); //一行数据打印完换行
}
return 0;
}