C语言代码编程200个例题(上)

1

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
    int i, j, n, m;										 /*定义变量i,j,n,m*/
    int a[16] =
    {
    
    
        0
    };											 /*定义数组a,元素初始值为0*/
    printf("please input the decimalism number(0~32767):\n");	 /*输出双引号内普通字符*/
    scanf("%d", &n);									 /*输入n的值*/
    for (m = 0; m < 15; m++)							 /*for循环从0到14,最高为符号位,本题始终为0*/
    {
    
    
        i = n % 2;									 /*取2的余数*/
        j = n / 2;								 	 /*取被2整除的结果*/
        n = j;										 /*将余数每次的余数存入数组a中*/
        a[m] = i;
    }
    for (m = 15; m >= 0; m--)
    {
    
    
        printf("%d", a[m]);								 /*for循环,将数组中的16个元素从后往前输出*/
        if (m % 4 == 0)
            printf(" ");								 /*每输出4个元素,输出一个空格*/
    }
}

2

#include "stdafx.h"
#include "stdio.h"
int main()
{
    
    
    printf("* * * * *\n");
    printf("*       *\n");
    printf("*       *\n");
    printf("*       *\n");
    printf("* * * * *\n");
}

3

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int a, b, sum;								/*声明变量*/
	a=123;									/*为变量赋初值*/
	b=789;									/*为变量赋初值*/
	sum=a+b;								/*求和运算*/
	printf("sum is  %d\n",sum);					/*输出结果*/
}

4

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int a, b, c, t;									/*定义四个基本整型变量a,b,c,t*/
	printf("please input a,b,c:\n");						/*双引号内普通字符原样输出并换行*/
	scanf("%d%d%d", &a, &b, &c);					/*输入任意三个数*/
	if (a > b)										/*如果a大于b,借助中间变量t实现a,b值互换*/
	{
    
    
		t = a;
		a = b;
		b = t;
	}
	if (a > c)										/*如果a大于c,借助中间变量t实现a,c值互换*/
	{
    
    
		t = a;
		a = c;
		c = t;
	}
	if (b > c)										/*如果b大于c,借助中间变量t实现b,c值互换*/
	{
    
    
		t = b;
		b = c;
		c = t;
	}
	printf("the order of the number is:\n");
	printf("%d,%d,%d", a, b, c);						/*输出函数将a,b,c的值顺序输出*/
}

5

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int day,x1,x2;								/*定义day,x1,x2三个变量为基本整型*/
	day=9;
	x2=1;
	while(day>0)
	{
    
    
		x1=(x2+1)*2;							/*第一天的桃子数是第二天桃子数加1后的2倍*/
		x2=x1;
		day--;								/*因为从后向前推天数递减*/
	}
	printf("the total is %d\n",x1);					/*输出桃子的总数*/
}

6

#include "stdafx.h"
#include "stdio.h"

int leap(int a)										/*自定义函数leap用来指定年份是否为闰年*/
{
    
    
    if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)				/*闰年判定条件*/
        return 1;									/*是闰年返回1*/
    else
        return 0;									/*不是闰年返回0*/
}

int number(int year, int m, int d) /*自定义函数number计算输入日期为该年第几天*/
{
    
    
    int sum = 0, i, a[12] =
    {
    
    
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };											/*数组a存放平年每月的天数*/
    int b[12] =
    {
    
    
        31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };											/*数组b存放闰年每月的天数*/
    if (leap(year) == 1)								/*判断是否为闰年*/
        for (i = 0; i < m - 1; i++)
            sum += b[i];								/*是闰年,累加数组b前m-1个月份天数*/
    else
        for (i = 0; i < m - 1; i++)
            sum += a[i];								/*不是闰年,累加数组a钱m-1个月份天数*/
    sum += d;										/*将前面累加的结果加上日期,求出总天数*/
    return sum;										/*将计算的天数返回*/
}

void main()
{
    
    
    int year, month, day, n;								/*定义变量为基本整型*/
    printf("请输入年月日\n");
    scanf("%d%d%d", &year, &month, &day);				/*输入年月日*/
    n = number(year, month, day);							/*调用函数number*/
    printf("第%d天\n", n);
}


7

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int a, b, c;
    for (a = 1; a <= 3; a++)								/*穷举a的所有可能*/
        for (b = 1; b <= 3; b++)							/*穷举b的所有可能*/
            for (c = 1; c <= 3; c++)						/*穷举c的所有可能*/
                if (a != 1 && c != 1 && c != 3 && a != b && a != c && b != c)
												/*如果表达式为真,则输出结果,否则继续下次循环*/
    {
    
    
        printf("%c 将嫁给 A\n", 'X' + a - 1);
        printf("%c 将嫁给 B\n", 'X' + b - 1);
        printf("%c 将嫁给 C\n", 'X' + c - 1);
    }
}

8

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int cock, hen, chick;										/*定义变量为基本整型*/
    for (cock = 0; cock <= 20; cock++)								/*公鸡范围在0到20之间*/
        for (hen = 0; hen <= 33; hen++)							/*母鸡范围在0到33之间*/
            for (chick = 3; chick <= 99; chick++)						/*小鸡范围在3到99之间*/
                if (5 *cock + 3 * hen + chick / 3 == 100) 				/*判断钱数是否等于100*/
                    if (cock + hen + chick == 100) 				/*判断购买的鸡数是否等于100*/
                        if (chick % 3 == 0) 						/*判断小鸡数是否能被3整除*/
                            printf("公鸡:%d 母鸡:%d 小鸡:%d\n", cock, hen,chick);
}

9

#include "stdafx.h"
#include "stdio.h"
int leap(int a) 								/*自定义函数leap用来指定年份是否为闰年*/
{
    
    
    if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)		/*闰年判定条件*/
        return 1;							/*不是闰年返回1*/
    else
        return 0;							/*不是闰年返回0*/
}

int number(int year, int m, int d)					/*自定义函数number计算输入日期距2011年1月1日共有多少天*/
{
    
    
    int sum = 0, i, j, k, a[12] =
    {
    
    
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };								/*数组a存放平年每月的天数*/
    int b[12] =
    {
    
    
        31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };							 	/*数组b存放闰年每月的天数*/
    if (leap(year) == 1)					/*判断是否为闰年*/
        for (i = 0; i < m - 1; i++)
            sum += b[i];					/*是闰年,累加数组b前m-1个月份天数*/
    else
        for (i = 0; i < m - 1; i++)
            sum += a[i];					/*不是闰年,累加数组a钱m-1个月份天数*/
    for (j = 2011; j < year; j++)
        if (leap(j) == 1)
            sum += 366;					/*2011年到输入的年份是闰年的加366*/
    else
        sum += 365;						/*2011年到输入的年份不是闰年的加365*/
    sum += d; 							/*将前面累加的结果加上日期,求出总天数*/
    return sum; 							/*将计算的天数返回*/
}

void main()
{
    
    
    int year, month, day, n;
    printf("请输入年月日\n");
    scanf("%d%d%d", &year, &month, &day); 	/*输入年月日*/
    n = number(year, month, day); 			/*调用函数number*/
    if ((n % 5) < 4 && (n % 5) > 0)			/*当余数是1或2或3时说明在打渔否则在晒网*/
        printf("%d:%d:%d 打渔\n", year, month, day);
    else
        printf("%d:%d:%d 晒网\n", year, month, day);
}

10

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
	float i,h=100,s=100;							/*定义变量i,h,s分别为单精度型并为h和s赋初值100*/
	for(i=1;i<=9;i++)							/*for语句,i的范围从1到9表示小球从第二次落地到第十次落地*/
	{
    
    
		h=h/2;									/*每落地一次弹起高度变为原来一半*/
		s+=h*2;								/*累积的高度和加上下一次落地后弹起与下落 的高度*/
	}
	printf("总长度是:%f\n",s);				/*将高度和输出*/
	printf("第十次落地后弹起的高度是:%f",h/2);			/*输出第十次落地后弹起的高度*/
	printf("\n");
}

11

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int x[7], y[7], s, i;
    s = 2520 / 6; 										/*求出平均每个人要分多少个苹果*/
    for (i = 2; i <= 6; i++)
     /*求从老二到老六得到哥哥分来的苹果却未分给弟弟时的苹果数*/
        y[i] = s *(9-i) / (8-i);
    y[1] = x[1] = (s - y[6] / 3) *8 / 7;
     /*老大得到老六分来的苹果却未分给弟弟时的苹果数*/
    for (i = 2; i <= 6; i++)
        x[i] = y[i] - y[i - 1] / (10-i);				/*求原来每人得到的苹果数*/
    for (i = 1; i <= 6; i++)
        printf("x[%d]=%d\n", i, x[i]);					/*将最终结果输出*/
}

12

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
	int n;					
	float sum1,sum2;							/*sum1和sum2应为单精度型,否则结果将不准确*/
	for(n=11;;n++)									
	{
    
    
		sum1=(n+9)/10.0;									
		sum2=(9*n+171)/100.0;
		if(sum1!=(int)sum1)continue;			/*sum1和sum2应为整数,否则结束本次循环继续下次判断*/
		if(sum2!=(int)sum2)continue;
		if(sum1==sum2) break;					/*当sum1等于sum2时,跳出循环*/
	}
	printf("共有%d个学生\n将糖果分成了%d份",(int)(n/sum1),n);	
	/*输出学生数及分成的份数*/
	printf("\n");
}

13

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int year;											 /*定义基本整型变量year*/
    printf("请输入年份:\n");
    scanf("%d", &year);									 /*从键盘输入表示年份的整数*/
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)		 /*判断闰年条件*/
        printf("%d 是闰年\n", year);						 /*满足条件的输出是闰年*/
    else
        printf("%d 不是闰年\n", year);						 /*否则输出不是闰年*/
}

14

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int a, b, c, d, e;
    for (a = 0; a <= 1; a++)								/*对a、b、c、d、e穷举贴黑纸和白纸的所有可能*/
        for (b = 0; b <= 1; b++)
            for (c = 0; c <= 1; c++)
                for (d = 0; d <= 1; d++)
                    for (e = 0; e <= 1; e++)
                        if ((a && b + c + d + e == 3 || !a && b + c + d + e !=
                            3) && (b && a + c + d + e == 0 || !b && a + c + d +
                            e != 0) && (c && a + b + d + e == 1 || !c && a + b
                            + d + e != 1) && (d && a + b + c + e == 4 || !d &&
                            a + b + c + e != 4))							/*根据体列出相应条件*/
						{
    
    
							printf("0-黑纸,1-白纸\n");
							printf("a is %d\nb is %d\nc is %d\nd is %d\ne is %d\n", a, b, c, d, e);			/*将最终结果输出*/
						}
}

15

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int i, j, k, n;												/*定义变量为基本整型*/
    for (i = 100; i < 1000; i++)								/*对100~1000内的数进行穷举*/
    {
    
    
        j = i % 10; 											/*分离出个位上的数*/
        k = i / 10 % 10; 										/*分离出十位上的数*/
        n = i / 100; 											/*分离出百位上的数*/
        if (j *j * j + k * k * k + n * n * n == i) 				/*判断各位上的立方和是否等于其本身*/
            printf("%5d", i); 									/*将水仙花数输出*/
    }
	printf("\n");
}

16

#include "stdafx.h"
#include "stdio.h"
void main()
{
    
    
    float a1 = 3, b1 = 1, c1 = 4, d1 = 5;							 /*定义变量为单精度型*/
    float day;											 /*定义天数为单精度型*/
    day = 1 / (1 / a1 + 1 / b1 + 1 / c1 + 1 / d1);
        												 /*计算四渠同时注水多久可以注满*/
    printf("需要 %f 天!\n", day);								 /*将计算出的天数输出*/
} 

17

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
    int a,b,c,sum;										/*定义变量*/
	float ave;
    printf("请输入三个学生的分数:\n");					/*输出提示信息*/
    scanf("%d%d%d",&a,&b,&c);							/*输入三个学生的成绩*/
    sum=a+b+c;											/*求总成绩*/
	ave=sum/3.0;										/*求平均成绩*/
    printf("总成绩=%4d\t, 平均成绩=%5.2f\n",sum,ave);	/*输出总成绩和平均成绩*/
}

18

#include "stdafx.h"
#include "stdio.h"

int main()
{
    
    
		char c;									/*定义变量*/
		printf("请输入一个字符:\n");					/*显示提示信息*/
		scanf("%c",&c);								/*要求输入一个字符*/
		if(c>=65&&c<=90)							/*表达式1的取值范围*/
		{
    
    
			printf("输入的字符是大写字母\n");
		}
		else if(c>=97&&c<=122)						/*表达式2的取值范围*/
		{
    
    
			printf("输入的字符是小写字母\n");
		}
		else if(c>=48&&c<=57)						/*表达式3的取值范围*/
		{
    
    
			printf("输入的是数字\n");
		}
		else										/*输入其他范围*/
		{
    
    
			printf("输入的是特殊符号\n");
		}
		return 0;
} 

19

#include "stdafx.h"
#include "stdio.h"
#include<stdlib.h>

int main()
{
    
    
    int button;							/*定义变量*/
    system("cls");						/*清屏*/
    printf("**********************\n");	/*输出普通字符*/
    printf("*  可选择的按键:     *\n");
    printf("*  1. 巧克力         *\n");
    printf("*  2. 蛋糕           *\n");
    printf("*  3. 可口可乐       *\n");
    printf("**********************\n");
    printf("从1~3中选择按键:\n");			/*输出提示信息*/
    scanf("%d",&button);					/*输入数据*/
    switch(button)							/*根据button决定输出结果*/
    {
    
    
    case 1:
        printf("你选择了巧克力");
        break;
    case 2:
        printf("你选择了蛋糕");
        break;
    case 3:
        printf("你选择了可口可乐");
        break;
    default:
        printf("\n 输入错误 !\n");			/*其它情况*/
        break;
    }
     printf("\n");
     return 0;
}

20

#include "stdafx.h"
#include "stdio.h"
void main()
{
    
    
    float x, m1, m2, m;
    char y, z;
    scanf("%f,%c,%c", &x, &y, &z); 					/*输入选择油的千克数、种类及服务*/
    switch (y)										/*选择汽油种类*/
    {
    
    
        case 'a':
            m1 = 5.75;
            break;
        case 'b':
            m1 = 6.00;
            break;
        case 'c':
            m1 = 7.15;
            break;
    }
    switch (z)										/*选择服务种类*/	
    {
    
    
        case 'a':									/*不需要提供服务*/
            m2 = 0;
            break;
        case 'm':
            m2 = 0.05;
            break;
        case 'e':
            m2 = 0.1;
            break;
    }
    m = x * m1 - x * m1 * m2; 						/*计算应付的钱数*/
    printf("汽油种类是:%c\n", y);
    printf("服务等级是:%c\n", z);
    printf("用户应付金额是:%.3f\n", m);
}

21

#include "stdafx.h"
#include "stdio.h"

void main()
{
    
    
	float a,b;
	char c;
	printf("请输入运算格式:a+(-,*,/)b \n");
	scanf("%f%c%f",&a,&c,&b);
	switch(c)
	{
    
    
	case '+':printf("%f\n",a+b);break;
	case '-':printf("%f\n",a-b);break;
	case '*':printf("%f\n",a*b);break;
	case '/':
		if(!b)
			printf("除数不能是零\n");
		else
			printf("%f\n",a/b);
		break;
	default:printf("输入有误!\n");
		
	}
}

22

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int i,j,k;													/*定义i,j,k为基本整型*/
		for(i=0;i<=10;i++)										/*i是一角钱兑换个数,所以范围从1到10*/
			for(j=0;j<=5;j++)									/*j是两角钱兑换个数,所以范围从0到5*/
				for(k=0;k<=2;k++)								/*k是五角钱兑换个数,所以范围从0到2*/
					if(i+j*2+k*5==10)							/*三种钱数相加是否等于十*/
						printf("yi jiao%d,liang jiao%d,wu jiao%d\n",i,j,k);/*将每次可兑换的方案输出*/
	return 0;
}

23

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int i, j; 								/*定义i,j两个变量为基本整型*/
	for (i = 1; i <= 9; i++)					/*for循环i为乘法口诀表中的行数*/
	{
    
    
		for (j = 1; j <= i; j++)				/*乘法口诀表中的另一个因子,取值范围受一个因子i的影响*/
		printf("%d*%d=%d ", i, j, i *j);		/*输出i,j及i*j的值*/
		printf("\n"); 						/*打完每行值后换行*/
	}
}

24

#include "stdafx.h"
#include "stdio.h"
#include<math.h>

int main()
{
    
    
    double y;
    int x, m;
    for (y = 1; y >=  - 1; y -= 0.1)						/*0到π,π到2π分别绘制21个点*/
    {
    
    
        m = acos(y) *10; 							/*求出对应的横坐标位置*/
        for (x = 1; x < m; x++)
            printf(" ");							/*画*前画空格数*/
        printf("*"); 								/*画**/
        for (; x < 62-m; x++)						/*画出对称面的**/
            printf(" ");
        printf("*\n");
    }
    getchar();
    return 0;
}

25

#include "stdafx.h"
#include "stdio.h"
#include<math.h>

int main()
{
    
    
    double y;
    int x, m;
    for (y = 1; y >=  - 1; y -= 0.1)						/*0到π,π到2π分别绘制21个点*/
    {
    
    
        m = acos(y) *10; 							/*求出对应的横坐标位置*/
        for (x = 1; x < m; x++)
            printf(" ");							/*画*前画空格数*/
        printf("*"); 								/*画**/
        for (; x < 62-m; x++)						/*画出对称面的**/
            printf(" ");
        printf("*\n");
    }
    getchar();
    return 0;
}

26

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int a, b, c, x, sum;
	for (x = 1; x <= 5; x++)									/*x的取值范围从1到5*/
	{
    
    
		if (10605 % (28-5 * x) == 0)							/*满足条件的x值即为所求*/
		{
    
    
			sum = 10605 / (28-5 * x);						/*计算出邮票总数*/
			a = 2 * sum / 10;								/*计算a集邮册中的邮票数*/
			b = 5 * sum / 7;								/*计算b集邮册中的邮票数*/
			c = 303;										/*c集邮册中的有票数*/
			printf("total is %d\n", sum);						/*输出邮票的总数*/
			printf("A:%d\n", a);							/*输出A集邮册中的邮票数*/
			printf("B:%d\n", b);							/*输出B集邮册中的邮票数*/
			printf("C:%d\n", c);								/*输出C集邮册中的邮票数*/
		}
	}
}

27

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int i, j, count;
	puts("the result is:\n");
	printf("time  red ball  white ball  black ball\n");
	count = 1;
	for (i = 0; i <= 3; i++)										/*红球数量范围0到3之间*/
	{
    
    
		for (j = 0; j <= 3; j++)									/*白球的数量范围0到3之间*/
		{
    
    
			if ((8-i - j) <= 6) 									/*判断要取黑色球的数量是否在6个以内*/
				printf("%3d%8d%9d%10d\n", count++, i, j, 8-i - j); 	/*输出各种颜色球的数量*/
		}
	}
	return 0;
}

28

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int n = 1, m, sum, i; 								/*定义变量为基本整形*/
	while (1)
	{
    
    
		m = n; 										/*m存储一楼灯的数量*/
		sum = 0;
		for (i = 1; i < 8; i++)
		{
    
    
			m = m * 2;								/*每层楼灯的数量是上一层的2倍*/
			sum += m; 								/*计算出除一楼外灯的总数*/
		}
		sum += n; 									/*加上一楼灯的数量*/
		if (sum == 765)								/*判断灯的总数量是否达到765*/
		{
    
    
			printf("the first floor has %d\n", n); 			/*输出一楼灯的数量*/
			printf("the eight floor has %d\n", m); 			/*输出八楼灯的数量*/
			break; 									/*跳出循环*/
		}
		n++; 										/*灯的数量加1,继续下次循环*/
	}
	return 0;
}

29

#include "stdafx.h"
#include "stdio.h"
#include <math.h>
#include <stdlib.h>
main()
{
    
    
	int i, j, n = 0;										/*定义变量为基本整型*/
	system("cls");
	printf("10-100之间的素数:\n");
	for (i = 10; i <= 100; i++)
	{
    
    
		for (j = 2; j <= sqrt(i); j++)
		{
    
    
			if (i % j == 0) 							/*判断是否能被整除*/
				break; 								/*如果能被整除,就不需要接着判断,跳出循环*/
			else
			{
    
    
				if (j > sqrt(i) - 1) 
				{
    
    
					printf("%d,", i);
					n++;								/*记录次数*/
					if (n % 5 == 0) 					/*5个一换行*/
						printf("\n");
				}
				else
					continue;
			}
		}
	}
	printf("\n");
}

30

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int i;													/*定义基本整型变量i*/
	for (i = 100; i < 1000; i++)								/*for循环求一百到一千内的所有三位数*/
		/*根据题意写出对应的条件*/
		if (i % 2 == 1 && i % 3 == 2 && i % 5 == 4 && i % 6 == 5 && i % 7 == 0)
			printf("the number of the stairs is %d\n", i);		/*输出阶梯数*/
	return 0;
}

31

#include "stdafx.h"
#include "stdio.h"
void main()
{
    
    
	int i;												/*定义整型变量*/
	float total=0;										/*定义实型变量,并初始化*/
	for(i=0;i<5;i++)									/*循环*/
		total=(total+1000)/(1+0.025);						/*累计存款额*/
	printf("must save %5.2f at first. \n",total); 			/*输出存款额*/
}

32

#include "stdafx.h"
#include "stdio.h"
int main()
{
    
    
	char cString[100];									/*定义保存字符串的数组*/
	int iIndex, iWord=1;								/*iWord表示单词的个数*/
	char cBlank;										/*表示空格*/
	gets(cString);										/*输入字符串*/
	if(cString[0]=='\0')									/*判断如果字符串为空的情况*/
	{
    
    
		printf("There is no char!\n");
	}
	else if(cString[0]==' ')								/*判断第一个字符为空格的情况*/
	{
    
    
		printf("First char just is a blank!\n");
	}
	else
	{
    
    
		for(iIndex=0;cString[iIndex]!='\0';iIndex++)		/*循环判断每一个字符*/
		{
    
    
			cBlank=cString[iIndex];						/*得到数组中的字符元素*/
			if(cBlank==' ')								/*判断是不是空格*/
			{
    
    
				iWord++;								/*如果是则加1*/
			}
		}
		printf("%d\n",iWord);
	}
}

33

#include "stdafx.h"
#include "stdio.h"
main()
{
    
    
	int i, v0 = 0, v1 = 0, v2 = 0, v3 = 0, n, a[50];
	printf("please input the number of electorate:\n");
	scanf("%d", &n); 											/*输入参加选举的人数*/
	printf("please input 1or2or3\n");
	for (i = 0; i < n; i++)
	{
    
    
		scanf("%d", &a[i]);										/*输入每个人所选的人*/
	}
	for (i = 0; i < n; i++)
	{
    
    
		if (a[i] == 1)
		{
    
    
			v1++;											/*统计1号候选人的票数*/
		}
		else if (a[i] == 2)
		{
    
    
			v2++;											/*统计2号候选人的票数*/
		}
		else if (a[i] == 3)
		{
    
    
			v3++;											/*统计三号候选人的票数*/
		}
		else
		{
    
    
			v0++;											/*统计无效票数*/
		}
	}
	printf("The Result:\n");
	printf("candidate1:%d\ncandidate2:%d\ncandidate3:%d\nonuser:%d\n",v1,v2,v3,v0); 	/*将统计的结果输出*/
	return 0;
}

34

#include "stdafx.h"
#include "stdio.h"
#define MAX 50												/*定义MAX为常量50*/
main()
{
    
    
	int i,num;													/*定义变量i,num为基本整型*/
	int Chinese[MAX],Math[MAX],English[MAX];				/*定义数组为基本整型*/
	long StudentID[MAX];										/*定义StudentID为长整形*/
	float average[MAX];
	printf("please input the number of students");
	scanf("%d",&num);										/*输入学生数*/
	printf("Please input a StudentID and three scores:\n");
	printf("    StudentID  Chinese  Math    English\n");
	for( i=0; i<num; i++ )										/*根据输入的学生数量控制循环次数*/
	{
    
    
		printf("No.%d>",i+1);
		scanf("%ld%d%d%d",&StudentID[i],&Chinese[i],&Math[i],&English[i]);
		/*依次输入学号及语文,数学,英语成绩*/
		average[i] = (float)(Chinese[i]+Math[i]+English[i])/3;		/*计算出平均成绩*/
	}
	puts("\nStudentNum    Chinese   Math   English  Average");
	for( i=0; i<num; i++ )										/*for循环将每个学生的成绩信息输出*/
	{
    
    
		printf("%8ld %8d %8d %8d %8.2f\n",StudentID[i],Chinese[i],Math[i],English[i],average[i]);
	}
	return 0;
}

35

#include "stdafx.h"
#include "stdio.h"
#include <string.h>
main()
{
    
    
	int i, j = 1, n;
	float a[100], b[100], sum = 0;
	printf("\nEnter the number of players:\n");
	scanf("%d", &n); 										/*从键盘中输入选手的人数*/
	for (i = 1; i <= n; i++)
	{
    
    
		printf("now player %d\n", i);
		printf("please input score:\n");
		for (; j < 5 *n + 1; j++)
		{
    
    
			scanf("%f", &a[j]); 							/*输入5个裁判每个裁判所给的分数*/
			sum += a[j]; 									/*求出总份数*/
			if (j % 5 == 0)									/*一位选手有5个裁判给打分*/
			{
    
    
				break;
			}
		}
		b[i] = sum; 										/*将每个选手的总分存到数组b中*/
		sum = 0; 										/*将总分重新置0*/
		j++; 											/*j自加*/
	}
	j = 1;
	printf("player     judgeA  judgeB  judgeC  judgeD  judgeE  total\n");
	for (i = 1; i <= n; i++)
	{
    
    
		printf("player %d", i); 								/*输出几号选手*/
		for (; j < 5 *n + 1; j++)
		{
    
    
			printf("%8.1f", a[j]); 							/*输出裁判给每个选手对应的分数*/
			if (j % 5 == 0)
			{
    
    
				break;
			}
		}
		printf("%8.1f\n", b[i]); 								/*输出每个选手所得的总成绩*/
		j++;
	}
	return 0;
}

36

#include "stdafx.h"
#include <string.h>
main()
{
    
    
  int i,j,x=1,y=3,a[6][6]={
    
    0};				/*因为数组下标要用1到5,所以数组长度是6*/
  for(i=1;i<=25;i++)
  {
    
    
    a[x][y] =i;							/*将1到25所有数存到存到数组相应位置*/
    if(x==1&&y==5)
    {
    
    
      x=x+1;							/*当上一个数是第1行第五列时,下一个数放在它的下一行*/
      continue;							/*结束本次循环*/
    }
    if(x==1)							/*当上一个数是是第1行时,则下一个数行数是5*/
      x=5;
    else
      x--;								/*否则行数减1*/
    if(y==5)							/*当上一个数列数是第5列时,则下一个数列数是1*/
      y=1;
    else
      y++;							/*否则列数加1*/
    if(a[x][y]!=0)						/*判断经过上面步骤确定的位置上是否有非零数*/
    {
    
    
      x=x+2;							/*表达式为真则行数加2列数减1*/
      y=y-1;
    }
  }
  for(i=1;i<=5;i++)						/*将二维数组输出*/
  {
    
    
    for(j=1;j<=5;j++)
      printf("%4d",a[i][j]);
      printf("\n");						/*每输出一行回车*/
  }
}  

37

#include "stdafx.h"
#include "stdio.h"
int age(int n)											/*自定义函数age*/
{
    
    
	int f;
	if(n==1)
	f=10;											/*当n等于1时,f等于10*/
	else
	f=age(n-1)+2;										/*递归调用age函数*/
	return f;											/*将f值返回*/
}
main()
{
    
    
	int i,j;											/*定义变量i,j为基本整型*/
	printf("Do you want to know whose age?please input:\n");
	scanf("%d",&i);									/*输入i的值*/
	j=age(i);											/*调用函数age求年龄*/
	printf("the age is %d",j);								/*将求出的年龄输出*/
    printf("\n");
}

38

#include "stdafx.h"
#include "stdio.h"
int sub(int n) 											/*定义函数递归求鱼的总数*/
{
    
    
	if (n == 1)										/*当n等于1时递归结束*/
	{
    
    
		static int i = 0;
		do
		{
    
    
			i++;
		}
		while (i % 5 != 0);
		return (i + 1);									/*5人平分后多出一条*/
	}
	else
	{
    
    
		int t;
		do
		{
    
    
			t = sub(n - 1);
		}
		while (t % 4 != 0);
		return (t / 4 * 5+1);
	}
}
main() 
{
    
     
	int total; 
	total=sub(5); 										/*调用递归函数*/
	printf("the total number of fish is %d\n",total); 
	return 0;
}

39

#include "stdafx.h"
#include "stdio.h"
int gys(int x,int y)							/*定义求最大公约数函数*/
{
    
    
  return y?gys(y,x%y):x;					/*递归调用gys,利用条件语句返回最大公约数*/
}
int gbs(int x,int y)							/*定义求最小公倍数函数*/
{
    
    
  return x/gys(x,y)*y;
}
void yuefen(int fz,int fm)					/*定义约分函数*/
{
    
    
  int s=gys(fz,fm);
  fz/=s;
  fm/=s;
  printf("the result is %d/%d\n",fz,fm);
}
void add(int a,int b,int c,int d)					/*定义加法函数*/
{
    
    
  int u1,u2,v=gbs(b,d),fz1,fm1;
  u1=v/b*a;
  u2=v/d*c;
  fz1=u1+u2;
  fm1=v;
  yuefen(fz1,fm1);
}
void mul(int a,int b,int c,int d)					/*定义乘法函数*/
{
    
    
  int u1,u2;
  u1=a*c;
  u2=b*d;
  yuefen(u1,u2);
}
void sub(int a,int b,int c,int d)					/*定义减法函数*/
{
    
    
  int u1,u2,v=gbs(b,d),fz1,fm1;
  u1=v/b*a;
  u2=v/d*c;
  fz1=u1-u2;
  fm1=v;
  yuefen(fz1,fm1);
}
void div(int a,int b,int c,int d)					/*定义除法函数*/
{
    
    
  int u1,u2;
  u1=a*d;
  u2=b*c;
  yuefen(u1,u2);
}
void main()
{
    
    
  char op;
  int a,b,c,d;
  scanf("%ld,%ld,%c,%ld,%ld",&a,&b,&op,&c,&d);
  switch(op)							/*根据输入的符号选择不同函数的调用*/
  {
    
    
   case '+':add(a,b,c,d);break;				/*调用加法函数*/
   case '*':mul(a,b,c,d);break;				/*调用乘法函数*/
   case '-':sub(a,b,c,d);break;					/*调用减法函数*/
   case '/':div(a,b,c,d);break;					/*调用除法函数*/
  }
}

40

#include "stdafx.h"
#include<stdio.h>
#include<string.h>
void main()
{
    
    
    char a[100], b[100], c[200],  *p;
    int i = 0, j = 0, k = 0;
    printf("please input string a:\n");
    scanf("%s", a); 									/*输入字符串1放入a数组中*/
    printf("please input string b:\n");
    scanf("%s", b); 									/*输入字符串2放入b数组中*/
    while (a[i] != '\0' && b[j] != '\0')
    {
    
    
        if (a[i] < b[j])									/*判断a中字符是否小于b中字符*/
        {
    
    
            c[k] = a[i]; 								/*如果小于,将a中字符放到数组c中*/
            i++; 									/*i自加*/
        }
        else
        {
    
    
            c[k] = b[j]; 								/*如不小于,将b中字符放到c中*/
            j++; 									/*j自加*/
        }
        k++; 										/*k自加*/
    }
    c[k] = '\0'; 										/*将两个字符串合并到c中后加结束符*/
    if (a[i] == '\0')									/*判断a中字符是否全都复制到c中*/
        p = b + j;									/*p指向数组b中未复制到c的位置*/
    else
        p = a + i;									/*p指向数组a中未复制到c的位置*/
    strcat(c, p); 										/*将p指向位置开始的字符串连接到c中*/
    puts(c); 										/*将c输出*/
}

41

#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include <windows.h>
char* insert (char s[],  char  t[],  int i)					/*自定义函数insert*/
{
    
    
  char string[100];								/*定义数组string作为中间变量*/
  if  ( i < 0||i > (int)strlen (s) )							/*当i超出输入字符串的长度将输出error*/
  {
    
    
    printf ( "error!!\n");
    exit (1);
  }
  if  (!strlen (s))					
    strcpy (s,  t);					/*若s数组长度为0,则直接将t数组内容复制到s中*/
    else   if (strlen (t))				/*若长度不为空,执行以下语句*/
       {
    
    
         strncpy (string,s,i);			/*将s数组中的前i个字符复制到string中*/
         string[i]='\0';				
         strcat (string,t) ;				/*将t中字符串连接到string*/
         strcat (string,(s+i));			/*将s中剩余字符串连接到string*/
         strcpy (s,string);				/*将string中字符串复制到s中*/
         return s;					/*返回值为s*/
       }
}
void main ()
{
    
    
  char str1[100],str2[100];				/*定义str1,str2两个字符型数组*/
  int position;						/*定义变量position为基本整型*/
  printf("please input str1:\n");
  gets(str1);						/*gets函数获得第一个字符串*/
  printf("please input str2:\n");
  gets(str2);						/*gets函数获得第二个字符串*/
  printf("please input position:\n");
  scanf("%d",&position);				/*输入字符串二插入字符串一的位置*/
  insert(str1,str2,position);				/*调用insert函数*/
  puts(str1);						/*输出最终得到的字符串*/
}  

42

#include "stdafx.h"
#include "stdio.h"
float average(float array[],int n)							/*自定义求平均身高函数*/
{
    
    
	int i;
	float aver,sum=0;
	for(i=0;i<n;i++)
	sum+=array[i];									/*用for语句实现sum累加求和*/
	aver=sum/n;										/*总和除以人数求出平均值*/
	return(aver);										/*返回平均值*/
}
int main()
{
    
    
	float average(float array[],int n);						/*函数声明*/
	float height[100],aver;
	int i,n;
	printf("请输入学生的数量:\n");
	scanf("%d",&n);									/*输入学生数量*/
	printf("请输入学生们的身高:\n");
	for(i=0;i<n;i++)
	scanf("%f",&height[i]);								/*逐个输入学生的身高*/
	printf("\n");
	aver=average(height,n);								/*调用average函数求出平均身高*/
	printf("学生的平均身高为:%6.2f\n",aver);			/*将平均身高输出*/
	return 0;
}

43

#include "stdafx.h"
#include "stdio.h"
#define swap(a,b) {
      
      int c;c=a;a=b;b=c;}/*定义一个带参的宏swap*/
main()
{
    
    
int i,j,a[10],b[10];/*定义数组及变量为基本整型*/
printf("please input array a:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);/*输入一组数据存到数组a中*/
printf("please input array b:\n");
for(j=0;j<10;j++)
scanf("%d",&b[j]);/*输入一组数据存到数组b中*/
printf("\nthe array a is:\n");
for(i=0;i<10;i++)
printf("%d,",a[i]);/*输出数组a中的内容*/
printf("\nthe array b is:\n");
for(j=0;j<10;j++)
printf("%d,",b[j]);/*输出数组b中的内容*/
for(i=0;i<10;i++)
swap(a[i],b[i]);/*实现数组a与数组b对应值互换*/
printf("\nNow the array a is:\n");
for(i=0;i<10;i++)
printf("%d,",a[i]);/*输出互换后数组a中的内容*/
printf("\nNow the array b is:\n");
for(j=0;j<10;j++)
printf("%d,",b[j]);/*输出互换后数组b中的内容*/
}

猜你喜欢

转载自blog.csdn.net/fjj2397194209/article/details/131350374