专业课真题复习(2018)

1.题目描述:
输出所有的“水仙花数”,所谓“水仙花数”是指一个三维数,其各位数字的立方和等于该数本身。例如,153是一水仙花数,因为153=111+555+333

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    
    
	int b,s,g;
	int i;
	for(i=100;i<=999;i++)
	{
    
    
		b=i/100;
		s=(i-b*100)/10;
		g=i%10;
		if((b*b*b+s*s*s+g*g*g)==i)
		{
    
    
			printf("%d\n",i);
		}
	}
	return 0;
}

2.题目描述:
用选择法对10个整数排序

#include<stdio.h>
int main(void)
{
    
    
	int arr[10]={
    
    10,9,8,7,6,5,4,3,2,1};
	int i,j;
	int min,temp;
	
	for(i=0;i<10;i++)
	{
    
    
		min=i;
		for(j=i+1;j<10;j++)
		{
    
    
			if(arr[min]>arr[j])
			{
    
    
				min=j;
			}
		}
		if(min!=i)
		{
    
    
			temp=arr[min];
			arr[min]=arr[i];
			arr[i]=temp;
		}
	}
	printf("排序后的数组为:\n");
	for(i=0;i<10;i++)
	{
    
    
		printf("%d",arr[i]);
		
	}
	return 0;
}

注意:每次找到一个最小的应该和i交换,不是和j交换。。

3.题目描述:
编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
	int english=0;
	int space=0;
	int number=0;
	int other=0;
	
void count( char *str)
{
    
    
	
    int i;
	for(i=0;i<strlen(str);i++)
	{
    
    
		if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
		{
    
    
			english++;
		}else if(str[i]==' ')
		{
    
    
			space++;
		}else if(str[i]>='0'&&str[i]<='9')
		{
    
    
			number++;
		}else{
    
    
			other++;
		}
	 } 
	   

 } 
int main(void)
{
    
    
	char str[100];
	gets(str);
	count(str);
	 printf("英文字母的个数为%d\n",english);
	 printf("空格的个数为%d\n",space);
	 printf("数字的个数为%d\n",number);
     printf("其他字符的个数为%d\n",other);	 
	return 0;
}

注意:仔细审题,在主函数输出数据。

4.题目描述:
有两个磁盘文件“A"和”B“,各存放一行字母,现要求编写程序实现,把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件"C"中去。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    
    
	FILE *fa,*fb,*fc;
	int i,j;
	char a[100];
	char b[100];
	char temp;
	if((fa=fopen("A.txt","r"))==NULL)
	{
    
    
		printf("can not open this file.");
		exit(0);
	}
	fgets(a,100,fa);	
	fclose(fa);
	if((fb=fopen("B.txt","r"))==NULL)
	{
    
    
		printf("can not open this file.");
	    exit(0);
	}
	fgets(b,100,fb);
	fclose(fb);
	strcat(a,b);
	for(i=0;i<strlen(a)-1;i++)
	{
    
    
		for(j=0;j<strlen(a)-1-i;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    
				temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
	fclose(fb);
	if((fc=fopen("C.txt","w"))==NULL)
	{
    
    
		printf("can not open this file.");
	    exit(0);
	}
	fputs(a,fc);
	fclose(fc);
	return 0;
}

注意:
1.打开文件要关闭
2. strcmp()字符串比较函数
3. strcat()字符串拼接函数
4. strcpy()字符串拷贝函数,后面的字符串会覆盖掉前边的字符串,这不是拼接函数。

猜你喜欢

转载自blog.csdn.net/qq_44867340/article/details/121863936