注解:
1. eps定义的格式
2. 判断是否等于零,需要在+-eps之间(负的别忘了
方法1:递归(大概能到第46项,并且自定义代码段里面有
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
unsigned long long fibonacci(int n)
{
if(n==0)
{
return 0;
}else if (n==1)
{
return 1;
}else if(n==2)
{
return 1;
}else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
int main()
{
int n;
scanf("%d",&n);
printf("%llu",fibonacci(n));
return 0;
}
方法2:打表
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int i;
unsigned long long a[100];
a[0]=0;
a[1]=1;
for(i=2;i<100;i++){
a[i]=a[i-1]+a[i-2];
}
int n;
scanf("%d",&n);
printf("%llu",a[n]);
return 0;
}
方法3:循环交换(课上的,还挺好用:)
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int n;
scanf("%d",&n);
unsigned long long a=0,b=1;
int i;
for(i=2;i<=n;i++){
b=a+b;
a=b-a;
}
printf("%llu",b);
return 0;
}
成绩转换,别忘了还可能是100(case10的时候)
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int grade;
scanf("%d",&grade);
switch (grade/10) {
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("Fail\n");
break;
}
return 0;
}
四则运算:
1. 浮点数的时候注意判断是否为零要用eps
2. 注意最后一个break和return 1 的位置
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
#define eps 1e10-9
int main()
{
char op;
double a,b,r;
scanf("%lf %c %lf",&a,&op,&b);
switch (op) {
case '+':
r=a+b;
break;
case '-':
r=a-b;
break;
case '*':
r=a*b;
break;
case '/':
if(fabs(b)>=eps){
r=a/b;
break;
}
default:
printf("invalid expression:%lf %c %lf\n",a,op,b);
return 1;
}
printf("%lf %c %lf = %lf\n",a,op,b,r);
return 0;
}
输入成绩并判断是否合法
注:
scanf==1或者0,表示scanf的返回值进行判断
用while getchar!='\n‘来清空一行缓冲区
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int a;
printf("input a score 0~100:");
while(scanf("%d",&a)==0||a>100||a<0){
printf("invalid input: %d\n",a);
printf("input a score 0~100:");
while(getchar()!='\n');
}
printf("your score is %d",a);
return 0;
}
存款计算
注解:由于是after每年之后你有多少钱,所以再乘以利息的时候应该是(s+m)*(1+r)
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int i, yr, money_year;
double rate, s;
printf("\n每年购买金额(>=0): ");
scanf("%d",&money_year);
printf("\n购买年限: ");
scanf("%d",&yr);
printf("\n年利率(0~0.2): ");
scanf("%lf",&rate);
s=0;
for(i=1;i<=yr;i++){
s = (money_year+s)*(1+rate);
printf("after %4d years, u have:%15.2lf$\n",i,s);
}
return 0;
}
数据求和题
注解:
1. x等于0的时候,自认为不可以被整除
2. 用continue可以进行排除法
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
#define DOWNCASE( c ) ( ((c) >= 'A' && (c) <= 'Z') ? ((c) + 0x20) : (c) )
int main()
{
int a, b, c, d, x, y, sum_x = 0, sum_y = 0;
a=1; b=100; // 根据实际需要进行初始化
c=1; d=200;
while(scanf("%d%d", &x, &y) != EOF) {
if(x<a || x>b || y<c || y>d)
continue;
if(x !=0 && y%x == 0)
continue;
if(y !=0 && x%y == 0)
continue;
sum_x += x; sum_y += y;
}
printf("sum_x = %d, sum_y = %d", sum_x, sum_y);
return 0;
}