从头开始学算法:考研机试题练习(C/C++)--入门模拟
来源:互联网 发布:手机铃声放大软件 编辑:程序博客网 时间:2024/05/01 00:12
从头开始学算法:考研机试题练习(C/C++)–入门模拟
最近重学c语言,刷的是胡凡写的《算法笔记》,这本书的题主要是面向考研机试和一般算法考试的,零基础入门,还不错,在此记录学习过程。
本文主要讲解一些简单的模拟题。
#include <stdio.h>#include <string.h>/*3n+1猜想*/void callatz_fun(){ int n; scanf("%d", &n); int step = 0; while (n != 1) { if (n % 2 == 0) { n = n / 2; } else { n = (3 * n + 1) / 2; } step++; } printf("%d", step);}/*求总分最高的挖掘机学校*/void wajueji_fun(){ int num, sum_score[100001] = { 0 }, highest_school=0 , school, score; scanf("%d", &num); while (num--) { scanf("%d %d", &school, &score); sum_score[school] += score; if (sum_score[school] >sum_score[highest_school]) { highest_school = school; } } printf("%d %d", highest_school, sum_score[highest_school]);}/*二分查找*/void binary_seach_fun(){ int i, n, a[200], find, find_index=-1; scanf("%d", &n); for(i=0;i<n;i++){ scanf("%d", &a[i]); } scanf("%d", &find); int low = 0, high = n - 1,mid; while (low < high) { mid = (low + high) / 2; if (a[mid] == find) { find_index = mid; break; } //从小到大排序 else if (a[mid] > find) { high = mid; } else { low = mid; } } if (find_index == -1 && a[low] == find) find_index = low;}/*查找*/void seach_fun(){ int i, n, a[200], find, find_index = -1; scanf("%d", &n); for (i = 0; i<n; i++) { scanf("%d", &a[i]); } scanf("%d", &find); for (i = 0; i<n; i++) { if (a[i] == find) { find_index = i; break; } } printf("%d", find_index);}/*画正方形*/void draw_square(){ int i, j, n; char shape; scanf("%d %c", &n, &shape); int row = n / 2 + n % 2; for (i = 0; i < row; i++) { printf("%c", shape); for (j = 1; j < n - 1; j++) { if (i == 0 || i == row - 1) printf("%c", shape); else printf(" "); } printf("%c", shape); printf("\n"); }}/*是否为闰年*/bool is_leap_year(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; return false;}/*计算日期差*/void date_difference(){ int mouth_days[13] = {0, 31, 28, 31, 30, 31 ,30 ,31, 31, 30, 31, 30, 31 }; int from_day, from_month, from_year, to_day, to_month, to_year, difference_days=0; char from_date[9], to_date[9], str_temp[5]; scanf("%s", from_date); scanf("%s", to_date); strncpy(str_temp, from_date, 4); sscanf(str_temp, "%d", &from_year); str_temp[2] = '\0'; strncpy(str_temp, from_date+4, 2); sscanf(str_temp, "%d", &from_month); strncpy(str_temp, from_date+6, 2); sscanf(str_temp, "%d", &from_day); //printf("%d %d %d\n", from_year, from_month, from_day); strncpy(str_temp, to_date, 4); sscanf(str_temp, "%d", &to_year); str_temp[2] = '\0'; strncpy(str_temp, to_date + 4, 2); sscanf(str_temp, "%d", &to_month); strncpy(str_temp, to_date + 6, 2); sscanf(str_temp, "%d", &to_day); //将to_day变为与from_day相同 difference_days += to_day - from_day; //将to_month变为与from_month相同 if (is_leap_year(to_year)) mouth_days[2] = 29; if (to_month > from_month) { for (int i = from_month; i < to_month; i++) { difference_days += mouth_days[i]; } } else { for (int i = to_month; i < from_month; i++) { difference_days -= mouth_days[i]; } } //将to_year变为from_year for (int i = from_year; i < to_year; i++) { if (is_leap_year(i)) { difference_days += 366; } else { difference_days += 365; } } //补上减去那些没有算的闰月 //起始年的月高于2月的该年不能当闰年算 if (from_month > 2 && is_leap_year(from_year)) { difference_days--; } //终止年的月高于2月,最后一年少算一天 if (to_month > 2 && is_leap_year(to_year)) { difference_days++; } printf("%d", difference_days);}//A、B的D进制和void scale_change(){ int a, b, d; char out[31]; scanf("%d %d %d", &a, &b, &d); int sum = a + b; int n=31; while (sum) { n--; out[n] = '0' + (sum % d); sum = sum / d; } char result[31]; strncpy(result, out + n, 31 - n); result[31 - n] = '\0'; printf("%s", result);}/*回文串*/void is_plalindrome(){ char sentence[256]; scanf("%s", sentence); int first = 0, last = strlen(sentence) - 1; while (first <= last) { if (sentence[first] == sentence[last]) { first++; last--; }else{ break; } } if (first > last) { printf("YES"); } else { printf("NO"); }}/*说反话*/void ironic_fun(){ char in_str[81],sentence[40][20]; gets_s(in_str); int i, num = -1,start=0; for (i = 0; i < strlen(in_str); i++) { if (in_str[i] != ' ') continue; else { num++; strncpy(sentence[num], in_str + start, i - start); sentence[num][i - start] = '\0'; start = i + 1; } } num++; strncpy(sentence[num], in_str + start, strlen(in_str) - start); sentence[num][i - start] = '\0'; for (i = num; i >= 0; i--) { printf("%s ", sentence[i]); } printf("\b");}int main(){ //callatz_fun(); //wajueji_fun(); //seach_fun(); //draw_square(); //date_difference(); //scale_change(); //is_plalindrome(); ironic_fun(); return 0;}
阅读全文
0 0
- 从头开始学算法:考研机试题练习(C/C++)--入门模拟
- 从头开始学算法:考研机试题练习(C/C++)–算法初步
- 从头开始学算法:考研机试题练习(C/C++)--基础知识
- 从头开始学算法:考研机试题练习(C/C++)–数学问题
- 从头开始学算法:考研机试题练习(C/C++)–STL使用
- 从头开始学算法:考研机试题练习(C/C++)–简单数据结构
- 从头开始学算法:考研机试题练习(C/C++)–搜索
- 从头开始学C
- 从头开始学C语言
- 从头开始学c ----基础1
- 从头开始学c ----基础2
- 从头开始学算法
- CCF201312--模拟练习试题参考答案(C++)
- 从头学C#-01
- 从头开始学习C
- 从头开始学C++,给自己鼓励下!!
- 从头再学C语言:终于开始动动脑子了。
- 2012年 浙工大考研计算机专业课试题C++(学硕)
- [杂记] bsp与apk
- 生存期
- HDU 6191 Query on A Tree 可持久化trie + dfs建树 || 启发式合并trie
- Android中数值计算的精度
- springMVC
- 从头开始学算法:考研机试题练习(C/C++)--入门模拟
- QT文字绘制
- 使用PullToRefresh遇到的问题
- PHP laravel系列之中间件
- 解决的s2010空间丑陋,解决manifest xp不显示控件的方法
- C++访问控制
- 深入理解Java HashMap(JDK1.8)
- Map之ConcurrentHashMap
- 异常处理,finally,与自定义异常