西安电子大学计算机考研复试机试(2019)+ 算法笔记(入门一)+简单模拟/查找/图形输出/日期处理

2019-04-13 16:39发布

/* n 偶数 砍半 奇数 3n+1 砍半 n = 1 */ #include #include using namespace std; int main(){ int n,count=0; while(cin>>n){ while(n!=1){ if(n%2!=0){ n = 3*n+1; } n/=2; count++; } cout<
#include #include using namespace std; /*这道题要注意数组定义的位置*/ const int maxn = 100010; int a[maxn] = {0}; int main(){ int n,p,q = 0,max; cout<<"n:"<>n){ for(int i=0;i
/* cin>>n; cin>>a[n]; cin>>x; 输出:x的位置 */ #include #include using namespace std; const int maxn=200; int a[maxn] = {}; int main(){ int n,x; while(cin>>n){ for(int i=0;i>a[i]; } cin>>x; int k; for(k=0;k 这道题还是数组定义位置的问题,一开始如下定义,int n , a[n];编译可以通过,但是exe闪退了,应该是数组的大小是变量的关系。所以定义方式采用如上代码中的形式即可。或者在cin>>n;之后定义int a[n];也可以。
/* 题目中有一个四舍五入的要求,行数是列数的一半(四舍五入) */ #include #include using namespace std; int main(){ int n,row; char a; while(cin>>n>>a){ // 直接在此处将奇数加一再对半就ok了,即完成了四舍五入操作 if(n%2!=0){ row = (n+1)/2; } else{ row = n/2; } for(int i=1;i<=row;i++){ if(i==1||i==row){ for(int j=0;j
/* 哈哈哈!日期计算网站:http://bjtime.cn/riqi/ */ /* 题目中有一个四舍五入的要求,行数是列数的一半(四舍五入) */ #include #include using namespace std; // 用二维数组表示平年闰年每一个月的天数 int a[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; // 计数器记天数 int count = 0; //0--平年 1--闰年 int leap = 0; //判断是否闰年 bool isLeapYear(int year){ return (year%400==0)||(year%100!=0&&year%4==0); } // 该年的某月某日据年初的天数 int dis(int year,int month,int day){ int days = 0; if(isLeapYear(year)){ leap = 1; } for(int i = month-1;i>=0;i--){ days += a[i][leap]; } days += day; return days; } int main(){ int date1,year1,month1,day1; int date2,year2,month2,day2; while(cin>>date1>>date2){ // 判断两个date的大小,date1比date2早 if(date1>date2){ int temp = date1; date1 = date2; date2 = temp; } year1=date1/10000;month1=date1%10000/100;day1=date1%100; year2=date2/10000;month2=date2%10000/100;day2=date2%100; cout<<"date1:"< 为了验证答案的正确性呢,我写了好多条注释!这道题答案上使用日期累加计算的,我直接用年月日相加减求和计算的,基本一致啦!看起来我的代码比答案繁琐写。参考答案的点:二维数组,将平年闰年的每个月份的天数存储起来,这种方法好简单而且很清晰。像我大一那会就写了各种switch还有循环之类的,看起来贼乱!
十进制转换为任意进制 #include #include #include using namespace std; /*进制转换*/ int main(){ // 十进制转化为任意进制 int num ,D ,trans = 0; int i = 0; while(cin>>num>>D){ while(num!=0){ trans += num % D * pow(10,i++); num/=D; } cout<  
判断是否是回文串,如“12321”,“123321”都是。正序反序一样的字符串即回文串 #include #include #include using namespace std; const int maxn = 255; char a[maxn]; /*判断是否是回文串*/ bool judge(char a[]){ // cout<
不明白答案上为啥要开二维数组,那么繁杂! #include #include #include using namespace std; const int maxn = 255; char a[maxn]; char b[maxn]; /*将字符串倒序输出*/ int main(){ while(gets(a)){ for(int i=0;i