#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个元素,输出一个空格*/}}
#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"intleap(int a)/*自定义函数leap用来指定年份是否为闰年*/{
if(a %4==0&& a %100!=0|| a %400==0)/*闰年判定条件*/return1;/*是闰年返回1*/elsereturn0;/*不是闰年返回0*/}intnumber(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个月份天数*/elsefor(i =0; i < m -1; i++)
sum += a[i];/*不是闰年,累加数组a钱m-1个月份天数*/
sum += d;/*将前面累加的结果加上日期,求出总天数*/return sum;/*将计算的天数返回*/}voidmain(){
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"voidmain(){
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"voidmain(){
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"intleap(int a)/*自定义函数leap用来指定年份是否为闰年*/{
if(a %4==0&& a %100!=0|| a %400==0)/*闰年判定条件*/return1;/*不是闰年返回1*/elsereturn0;/*不是闰年返回0*/}intnumber(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个月份天数*/elsefor(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;/*将计算的天数返回*/}voidmain(){
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);elseprintf("%d:%d:%d 晒网\n", year, month, day);}
#include"stdafx.h"#include"stdio.h"voidmain(){
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"voidmain(){
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"voidmain(){
int year;/*定义基本整型变量year*/printf("请输入年份:\n");scanf("%d",&year);/*从键盘输入表示年份的整数*/if((year %4==0&& year %100!=0)|| year %400==0)/*判断闰年条件*/printf("%d 是闰年\n", year);/*满足条件的输出是闰年*/elseprintf("%d 不是闰年\n", year);/*否则输出不是闰年*/}
14
#include"stdafx.h"#include"stdio.h"voidmain(){
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"voidmain(){
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");}
#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);/*将每次可兑换的方案输出*/return0;}
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>intmain(){
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();return0;}
25
#include"stdafx.h"#include"stdio.h"#include<math.h>intmain(){
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();return0;}
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);/*输出各种颜色球的数量*/}}return0;}
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,继续下次循环*/}return0;}
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");}elsecontinue;}}}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);/*输出阶梯数*/return0;}
31
#include"stdafx.h"#include"stdio.h"voidmain(){
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"intmain(){
char cString[100];/*定义保存字符串的数组*/int iIndex, iWord=1;/*iWord表示单词的个数*/char cBlank;/*表示空格*/gets(cString);/*输入字符串*/if(cString[0]=='\0')/*判断如果字符串为空的情况*/{
printf("There is no char!\n");}elseif(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号候选人的票数*/}elseif(a[i]==2){
v2++;/*统计2号候选人的票数*/}elseif(a[i]==3){
v3++;/*统计三号候选人的票数*/}else{
v0++;/*统计无效票数*/}}printf("The Result:\n");printf("candidate1:%d\ncandidate2:%d\ncandidate3:%d\nonuser:%d\n",v1,v2,v3,v0);/*将统计的结果输出*/return0;}
34
#include"stdafx.h"#include"stdio.h"#defineMAX50/*定义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]);}return0;}
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++;}return0;}
#include"stdafx.h"#include"stdio.h"intage(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"intsub(int n)/*定义函数递归求鱼的总数*/{
if(n ==1)/*当n等于1时递归结束*/{
staticint 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);return0;}
39
#include"stdafx.h"#include"stdio.h"intgys(int x,int y)/*定义求最大公约数函数*/{
return y?gys(y,x%y):x;/*递归调用gys,利用条件语句返回最大公约数*/}intgbs(int x,int y)/*定义求最小公倍数函数*/{
return x/gys(x,y)*y;}voidyuefen(int fz,int fm)/*定义约分函数*/{
int s=gys(fz,fm);
fz/=s;
fm/=s;printf("the result is %d/%d\n",fz,fm);}voidadd(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);}voidmul(int a,int b,int c,int d)/*定义乘法函数*/{
int u1,u2;
u1=a*c;
u2=b*d;yuefen(u1,u2);}voidsub(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);}voiddiv(int a,int b,int c,int d)/*定义除法函数*/{
int u1,u2;
u1=a*d;
u2=b*c;yuefen(u1,u2);}voidmain(){
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>voidmain(){
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输出*/}
#include"stdafx.h"#include"stdio.h"#defineswap(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中的内容*/}