参考书籍:C程序设计(第五版 谭浩强著)
程序有错误的话,欢迎指正哦
例题1-1 在屏幕上输出This is a C program
#include <stdio.h>
int main(void)
{
printf("This is a C program\n");
return 0;
}
例题1-2 求两个整数的和
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
printf("a+b=%d\n",a+b);
return 0;
}
例题1-3 求两个整数的最大值
#include <stdio.h>
int main(void)
{
int a = 100;
int b = 200;
int max = 0;
if(a>b)
max = a;
else if(a<b)
max = b;
else
max = a;
printf("max=%d\n",max);
}
习题4 在屏幕上输出Hello World!
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
习题5 打印图形
#include <stdio.h>
int main(void)
{
//方法1打印 使用多条printf语句
printf("*****\n");
printf(" *****\n");
printf(" *****\n");
printf(" *****\n");
//方法2打印 使用1条printf语句
printf("\
*****\n\
*****\n\
*****\n\
*****");
return 0;
}
习题6 输入a,b,c三个值并求出最大值
主要采用排序的方式,因为本题未说a,b,c是否存在重复,这里采用排序的方式会大大的减少代码量。如果大家有发现程序的bug,欢迎留言哦。
#include <stdio.h>
int main(void)
{
int a =0, b=0, c=0, max =0;
int a1 = 0, b1 = 0, c1 = 0,tmp =0;
printf("please type a value,b value,c value\n");
scanf("%d%d%d",&a,&b,&c); //for example: 1 2 3
//scanf("%d,%d,%d",&a,&b,&c); //for example: 1,2,3
printf("a=%d, b=%d, c=%d\n",a, b, c);
a1 = a;
b1 = b;
c1 = c;
if(a1 > b1) //比较排序法
{
tmp = a1;
a1 = b1;
b1= tmp;
}
else
{}
if(b1 > c1)
{
tmp = b1;
b1 = c1;
c1= tmp;
}
else
{}
max = c1;
printf("max = %d",max);
return 0;
}
好了第一章的习题结束了,有问题可以一起讨论,一起成长。