编写函数实现以下功能:从键盘输入三个人的年龄,返回三个人的最大年龄。1.c
/**
* @file 1.c
* @brief find the max number
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief find the max number of a,b,c
*
* @param a number a
* @param b number b
* @param c number c
*
* @return max
*/
int get_max(int a, int b, int c)
{
if (a < b) //a is the biggest in a and b
a = b;
if (a > c) //if a > c a is the biggest in a, b, c
return a;
else
return c;
}
int main(int argc, const char *argv[])
{
int a, b, c;
printf("Enter the age of three people: ");
scanf("%d %d %d", &a, &b, &c);
printf("The max is %d\n", get_max(a, b, c));
return 0;
}
计算用1、2、3、4这四个数可以组成多少互不相同且无重复的三位数。2.c
/**
* @file 2.c
* @brief calc the different nums within 100-999 with 1-4
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i, j, k;
int count = 0;
for (i = 1; i <= 4; i++)
for (j = 1; j <= 4; j++)
for (k = 1; k <= 4; k++)
if (i != j && i != k && j != k)
{
count++;
printf("%d%d%d\n", i, j, k);
}
printf("\ncount = %d\n", count);
return 0;
}
从键盘输入两个数,计算这两个数的最大公约数。3.c
/**
* @file 3.c
* @brief calc the GCD of two numbers
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief
* 此函数用遍历法求最大公约数,最大公约数只能小于或等于两个数中最小的那个,
* 由此遍历至1,在这中间,若首次遇到既能整除a,又能整除b的数,则该数为最大公约数。
* @param a
* @param b
*
* @return
*/
int gcd(int a, int b)
{
int i;
for (i = a < b ? a : b; i > 0; i--)
{
if (a % i == 0 && b % i == 0)
return i;
}
}
int main(int argc, const char *argv[])
{
int i, j;
printf("Please enter two integer: ");
scanf("%d %d", &i, &j);
printf("The gcd of %d and %d is %d\n", i, j, gcd(i, j));
return 0;
}
用终端模拟一个时钟,用终端打印时间,指定开始时间为23:59:50,显示格式为xx:xx:xx。(提示:sleep(1)用来延时一秒)4.c
/**
* @file 4.c
* @brief Display a virtual clock in terminal
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief
* 在一个死循环中令second一直自加,然后进行判断,当second加到60后,将其清零,minute进行自加,minute加到60后,将其清零,hour自加,hour加到24后,将其清零。
* 在输出时,定义好格式‘b’为退格,%02d意为输出整数,不足两位以0补齐。
* fflush(stdout)为清楚缓冲区,由于printf函数以行为缓冲,当不想输出换行符时可这样输出。
* @return
*/
int main(int argc, const char *argv[])
{
int hour = 23, minute = 59, second = 50;
while(1)
{
second++;
if (second == 60)
{
second = 0;
minute++;
if (minute == 60)
{
minute = 0;
hour++;
if (hour == 24)
hour = 0;
}
}
printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d", hour, minute, second);
fflush(stdout);
sleep(1);
}
return 0;
}