本人计划考研:报考学校北京工业大学--计算机
专业课编号985:教材为C语言程序设计案例教程和严蔚敏的数据结构那本
我知道 本书是没有答案的
下面的全都是 自己写的 并在电脑上运行通过的 准确率 应该再 99%以上
现在开始复习C语言,计划先将课后部分写代码的题目写一遍
对了,数据结构练习oj题目在: http://20111564.blog.51cto.com/6048168/1565753
不定期跟新最迟一周搞定
-----2014/10/17
==============第二章 C语言的基本控制结构==============
//==========4.从键盘输入一个年份,判断该年是否为闰年
//==========5.从键盘输入一个年份和月份,输出这个月份的天数
//==========都比较简单
#include<stdio.h>
//求月份的天数
int dayNum(int year, int mouth)
{
int flag = 0;
if(( year % 100 != 0 && year % 4 == 0 ) || ( year % 400 == 0))
flag = 1; // 闰月
if(mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7
|| mouth == 8 || mouth == 10 || mouth == 12)
return 31;
else if(mouth == 2 && flag)
return 29;
else if(mouth == 2 && !flag)
return 28;
else
return 30;
}
int main()
{
int year,mouth;
scanf("输入任意一个年份:");
while (scanf("%d%d",&year,&mouth) == 2)
{
if(year >= 0 && mouth > 0 && mouth <= 12)
{
if(( year % 100 != 0 && year % 4 == 0 ) || ( year % 400 == 0) )
printf("%d是闰月\t天数是%d\n",year,dayNum(year,mouth));
else
printf("%d不是闰月\t天数是%d\n",year,dayNum(year,mouth));
}
else
printf("超范围,重新输入\n");
}
return 0;
}
//==========7.输入一行英文句子,将每个单词的首字符改成大写字母并输出
//==========10. 并求出倒数第二个单词的字符个数
#include<stdio.h>
#include<string.h>
int main()
{
char ch[100];
scanf("请输入一行英文句子:");
gets(ch);
int num = 0, count, flag = 1; //num 记录倒数第二个单词的长度
//count 记录当前扫描单词长度
//flag 标记首个单词的首个字母是否被访问
for(int i = 0; i < strlen(ch); i++)
{
if(ch[0] >= 'a' && ch[0] <= 'z' && flag)
{
putchar(ch[0] - 32);
count = 1; //首个单词首个字母被访问,初始化count
flag = 0;
}
else if(i != 0 && ch[i - 1] == ' ' && ch[i] >= 'a' && ch[i] <= 'z')
{
num = count; //符合每个单词的首字母要求时,则将上一个单词长度赋值给num
printf("%c",ch[i] - 32);
count = 1;
}
else
{
putchar(ch[i]);
count++;
}
}
if(count == 0) //输入一个单词
{
printf("\n没有倒数第二个单词\n");
}
else
printf("\n倒数第二个单词的长度为: %d\n",num - 1);
return 0;
}
//==========练习题:格式输出
//==========输入一个月份
//========== please input mouth num: 10
//========== SUM MON TUE WED THU FIR STA
//========== 1 2 3 4
//========== 5 6 7 8 9 10 11
//========== 12 13 14 15 16 17 18
//========== 19 20 21 22 23 24 25
//========== 26 27 28 29 30 31
//========== Press any key to continue
//==========按照周格式输出此月份(2014年的)
//==========已知2014/1/1是星期三
#include<stdio.h>
#include<string.h>
int main()
{
int day[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("please input mouth num:\t");
int i, mouth, sumDay = 0,count = 0;
scanf("%d",&mouth);
for(i = 1; i < mouth; i++)
sumDay += day[i];
int firstWeek = (sumDay % 7 + 3) % 7; //2014年1月1日是星期三,要将算出来的日期后移三个
printf("SUM\tMON\tTUE\tWED\tTHU\tFIR\tSTA\n");
for(int j = 0; j < firstWeek; j++) //输出第一周的前面几个空格
{
count++;
printf("\t");
}
for(i = 1; i <= day[mouth]; i++)
{
printf("%d\t",i);
count++;
if(count % 7 == 0)
printf("\n");
}
printf("\n");
return 0;
}
//============================第二章结束==========================
//======明天第三章
================第三章 计算机算法初步================
//========== 1. 输出100以内的所有素数
#include<stdio.h>
int main()
{
int count = 0;
for(int i = 2; i <= 100; i++)
{
int flag = 1;
for(int j = 2; (j * j) <= i; j++)
if(i != 2 && i % j == 0)
flag = 0;
if(flag == 1 || i == 2)
{
printf("%d\t",i);
count++;
if(count % 5 == 0)
printf("\n");
}
}
printf("\n");
return 0;
}
//========== 2.输入一个正整数,输出这个整数的所有因子
#include<stdio.h>
int main()
{
int n;
printf("please input a number:");
scanf("%d",&n);
for(int i = 1; i <=n; i++)
if(n % i ==0)
printf("%d\t",i);
printf("\n");
return 0;
}
//========== 3.用穷举法实现韩信点兵问题
//========== 问题:一队士兵,从1至5报数,最后一人报1
//========== 从1至6报数,最后一人报5
//========== 从1至7报数,最后一人报4
//========== 从1至11报数,最后一人报10
//========== 总共有多少士兵?
#include<stdio.h>
int main()
{
int flag = 1;
for(int i = 1; i < 100000; i++)
{
//不符合条件的标记
if((i % 5 != 1) || (i % 6 != 5) || (i % 7 != 4) || (i % 11 != 10))
flag = 0;
if(flag)
printf("%d\n",i);
flag = 1;
}
return 0;
}
//========== 4.水仙花数
#include<stdio.h>
int main()
{
int a, b, c;
for(int i = 100; i < 1000; i++)
{
a = i / 100;
b = ( i - a * 100 ) / 10;
c = i % 10;
if (i == a*a*a + b*b*b + c*c*c)
printf("%d\t",i);
}
printf("\n");
return 0;
}
//========== 5.输出 1*2*3 + 3*4*5 + 99*100*101
#include<stdio.h>
int main()
{
int n = 1, sum = 0;
while(n <= 99)
{
sum += n * (n + 1) * (n + 2);
n += 2;
}
printf("%d\n",sum);
return 0;
}
//========== 6.输入a,n,计算输出a+aa+aaa+...+aa..a(n个a)的值
#include<stdio.h>
int main()
{
int a, n, sum = 0;
scanf("%d%d",&a,&n);
for(int i = 1; i <=n; i++)
{
int j = 1,count = a;
while(j < i)
{
count = count * 10 + a;
j++;
}
sum += count;
}
printf("%d\n",sum);
return 0;
}
//========== 7.猴子得到一堆桃,当天吃了一半之后,多吃了一个
//========== 以后每天猴子都吃了剩余的一半桃子之后,又多吃一个
//========== 在第10天,只剩下1个桃子,最初由几个桃子?
#include<stdio.h>
int main()
{
int num = 1;
int i = 0;
while(i < 10)
{
printf("%d\n",num);
num = num * 2 + 1;
i++;
}
printf("====================%d\n",num);
return 0;
}
================第四章 数据的组织结构================
//========== 4.产生100个随机数,将其中的技术改成相应的负数,偶数未出不变,并输出
//========== 5.产生100个随机数,输入一个数字key,判断是否存在于随机数中,存在输出位置
//==========
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((unsigned)time(NULL));//系统时间作为时间种子
int num;
int count = 1, flag = 0, key;
scanf("%d",&key);
for(int i = 0; i < 100; i++)
{
num = rand() % 100;
if(num % 2 != 0)
num *= (-1);
if(key == num)
{
flag = 1;
}
if(!flag)
count++;
printf("%d\n",num);
}
if(flag)
printf("%d在随机数的第%d个出现%\n",key,count);
else
printf("%d没有在随机数中出现\n",key);
return 0;
}
//========== 6.随机生成120名职工的生日,并统计每个月员工过生日的职工人数
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int main()
{
srand(unsigned(time(NULL)));
int mouth[13];
int people[120];
int i;
memset(mouth,0,sizeof(int)*4*13);
for(i = 0; i < 120; i++) //初始化月份
people[i] = rand() % 12 + 1;
for(i = 0; i < 120; i++) //计算每个员工在哪个月内
mouth[people[i]]++; //注意小技巧
for(i = 1; i < 13; i++)
printf("%d月份的人数有%d个\n",i,mouth[i]);
return 0;
}
//========== 7.编写一个程序,输入一个文本行,其中包含多个单词,计算其中最长的单词长度
//========== 这个跟第二章的10题基本就一样
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
gets(str);
int max = 0, count = 0, length;
length = strlen(str);
for(int i = 0; i <= length; i++)
{
if(str[i] != ' ' && str[i] != '\0')
count++;
else
{
if(max < count)
{
max = count;
count = 0;
}
}
}
printf("单词最长的长度为%d\n",max);
return 0;
}
//========== 8. 判断通过键盘输入的字符串是否一个合法的标示符
//========== 首字母必须是下划线或者字母,后面可以跟字母,数字,或者下划线
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
scanf("%s",str);
int length = strlen(str);
int flag = 1;
if(str[0] == '_' || str[0] >= 'a' && str[0] <= 'z'
|| str[0] >= 'A' && str[0] <= 'Z')
{
for(int i = 1; i < length; i++)
if(str[i] >= 'a' && str[i] <= 'z'
|| str[i] >= 'A' && str[i] <= 'Z'
|| str[i] >= '0' && str[i] <= '9' || str[i] == '_')
continue;
else
flag = 0;
}
else
{
flag = 0;
}
if(flag == 1)
printf("input OK!\n");
else
printf("input error!\n");
}
//========== 9.用二维数组创建并输出下列矩阵
//========== 1 2 3 0 0 0
//========== 2 1 2 3 0 0
//========== 3 2 1 2 3 0
//========== 0 3 2 1 2 3
//========== 0 0 3 2 1 2
//========== 0 0 0 3 2 1
//========== 如果发现这是个对称矩阵的话就什么都好办了
#include<stdio.h>
int main()
{
int a[10][10],i,j;
for(i = 0; i < 6; i++)
{
int count = 1;
for(j = i; j < 6; j++)
{
if(count <= 3)
{
a[i][j] = a[j][i] = j - i + 1;
count++;
}
else
a[i][j] = a[j][i] = 0;
}
}
for(i = 0; i < 6; i++)
{
for(j = 0; j < 6; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
//========== 10,某班35名学生,4门课程
//========== (1)输入四门课程的考试成绩
//========== (2)计算每位同学的平均成绩
//========== (3)按照平均成绩的高低进行排名
//========== 成绩什么的就不输入了,随机产生
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define N 35
struct Student
{
int stuId;
int course[4];
int averageScore;
};
//初始化学生信息
void InitStudentInfo(Student student[])
{
srand(unsigned(time(NULL)));
for(int i = 0; i < N; i++)
{
student[i].stuId = 201400 + i;
for(int j = 0; j < 4; j++)
{
student[i].course[j] = rand() % 60 + 40;
}
student[i].averageScore = 0;
}
}
//求每位同学的平均分
void AverageScore(Student student[])
{
for(int i = 0; i < N; i++)
{
student[i].averageScore = student[i].course[0] + student[i].course[1]
+ student[i].course[2] + student[i].course[3];
}
}
//按照平均成绩排名,使用最搓的冒泡
void Sort(Student student[])
{
Student temp;
for(int i = 0; i < N - 1; i++)
{
for(int j = i + 1; j < N; j++)
{
if(student[i].averageScore < student[j].averageScore)
{
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
}
//输出学生所有信息
void Disp(Student student[])
{
printf("stuId\tcourse1\tcourse2\tcourse3\tcourse4\taverageScore\n");
for(int i = 0; i < N; i++)
{
printf("%d\t",student[i].stuId);
for(int j = 0; j < 4; j++)
printf("%d\t",student[i].course[j]);
printf("%d\n",student[i].averageScore);
}
}
int main()
{
Student student[35];
InitStudentInfo(student);
AverageScore(student);
Sort(student);
Disp(student);
return 0;
}
//========== 自测题
//========== 三.1. 输入含有n个数值的整数数列和整数m
//========== 挑出前m个最小值(非常简单)
#include<stdio.h>
//选择排序
void selectSort(int data[], int n)
{
int minValue, temp;
for(int i = 0; i < n; i++)
{
minValue = i;
for(int j = i + 1; j < n; j++)
if(data[j] < data[minValue])
minValue = j;
if(minValue != i)
{
temp = data[i];
data[i] = data[minValue];
data[minValue] = temp;
}
}
}
int main()
{
int data[100];
int i, n, m;
printf("please Enter Two integers: n and m \n");
scanf("%d%d",&n,&m);
for(i = 0; i < n; i++)
scanf("%d",&data[i]);
selectSort(data,n);
for(i = 0; i < m; i++)
printf("%d ",data[i]);
printf("\n");
return 0;
}
//========== 自测题 2.输入字符串10/25/1990
//========== 输出Oct,25,1990
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char str[20];
char mouth[12][10] = {"Jan","Feb","Mar","Apr","May","June","July","August","Sep","Oct","Nov","Dec"};
scanf("%s",str);
int count = 0, i;
for(i = 0; str[i] != '/'; i++)
count++;
int mouthNum = 0;
for(i = 0; i < count; i++)
mouthNum += ( str[i] - '0' ) * pow(10, (count - i - 1));
printf("%s",mouth[mouthNum - 1]);
for(i = count; i < strlen(str); i++)
if(str[i] == '/')
printf(",");
else
printf("%c",str[i]);
printf("\n");
return 0;
}
转载于:https://blog.51cto.com/20111564/1564609