给定某年某月某日,将其转换为这一年的第几天并输出。1.c
/**
* @file 1.c
* @brief 给定某年某月某日,将其转换为这一年的第几天并输出
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
int day_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* @brief judge whether the year is a leap year
*
* @param year the year
*
* @return 1 for yes, 0 for no
*/
int is_leap(int year)
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;
}
/**
* @brief Calc the totol day of 1-x month
*
* @param month the last month
*
* @return the total days
*/
int total_month(int month)
{
int i, count = 0;
for (i = 0; i < month; i++)
count += day_per_month[i];
return count;
}
/**
* @brief Calc the total days
*
* @param year year
* @param month month
* @param day day
*
* @return total days
*/
int calc(int year, int month, int day)
{
int count = 0;
count = total_month(month) + day;
if (is_leap(year) && month > 2) //如果是闰年并且月份大于2,就在总数上加一天。
count++;
return count;
}
int main(int argc, const char *argv[])
{
int year, month, day;
printf("Please enter date like this xxxx:xx:xx: ");
scanf("%4d:%2d:%2d", &year, &month, &day);
printf("Total %d days\n", calc(year, month, day));
return 0;
}
判断输入的字符串是否为回文字符串;(提示:回文顺读倒读都一样,例如:12321,abcba)。2.c
/**
* @file 2.c
* @brief 判断输入的字符串是否为回文字符串;(提示:回文顺读倒读都一样,例如:12321,abcba)
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
/**
* @brief 判断输入的字符串是否为回文字符串;(提示:回文顺读倒读都一样,例如:12321,abcba)
*
* @param str the string
*
* @return 1 for yes, 0 for no
*/
int loop(char *str)
{
int i, j;
for (i = 0, j = strlen(str) - 1; i <= j; i++, j--)
{
if (str[i] != str[j])
return 0;
}
return 1;
}
int main(int argc, const char *argv[])
{
char str[100];
printf("Please enter a string: ");
scanf("%s", str);
if (loop(str))
printf("Yes!\n");
else
printf("No!\n");
return 0;
}
编写程序检查某一个整数中是否有重复的数字,如检查2823中存在重复数字2。3.c
/**
* @file 3.c
* @brief 编写程序检查某一个整数中是否有重复的数字,如检查2823中存在重复数字2
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
int main(int argc, const char *argv[])
{
int n, digit[10] = {0};
printf("Input a number: ");
scanf("%d", &n);
while(n)
{
if (digit[n % 10] == 1)
break;
else
digit[n % 10] = 1;
n /= 10;
}
if (n)
printf("Yes!\n");
else
printf("No!\n");
return 0;
}
将下列二位数组转置;(提示:行变列,列变行)
A[5][5]={
{1,6,2,3,7},
{2,7,3,2,2},
{3,2,5,2,6},
{3,2,6,4,2},
{4,3,7,4,8}};
4.c
/**
* @file 4.c
* @brief 将下列二位数组转置;(提示:行变列,列变行)
* @author GuoPengyu
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num[5][5] = {{1, 6, 2, 3, 7},
{2, 7, 3, 2, 2},
{3, 2, 5, 2, 6},
{3, 2, 6, 4, 2},
{4, 3, 7, 4, 8}};
int i, j, tmp;
for(i = 0; i < 4; i++){
for(j = i + 1; j < 5; j++)
{
tmp = num[i][j];
num[i][j] = num[j][i];
num[j][i] = tmp;
}
}
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
printf("%2d ", num[i][j]);
printf("\n");
}
return 0;
}
编写函数调用随机数生成函数产生0到19之间的数,在数组中存入15个互不相同的数;
随机数生成例程:template.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int i, num[10] = {0}; //给srand函数一个种子;
srand(time(NULL));
for(i = 0; i < 10; i++)
{
num[i] = rand() % 10; //产生随机数
}
for(i = 0; i < 10; i++)
{
printf("%d, ", num[i]);
}
printf("\n");
return 0;
}
5.c
/**
* @file 5.c
* @brief 编写函数调用随机数生成函数产生0到19之间的数,在数组中存入15个互不相同的数
* @author Gaojinlong
* @version 0.1
* @date 2011-09-09
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char *argv[])
{
int num[15], i, j;
srand(time(NULL));
for(i = 0; i < 15;)
{
num[i] = rand()%20;
for(j = 0; j < i; j++)
if(num[i] == num[j])
break;
if(j == i)
i++;
}
for(i = 0; i < 15; i++)
printf("%d,", num[i]);
printf("\n");
return 0;
}