1.输出数字序列 1,2,3,5,8,13,21...,输出个数由键盘输入。注意输入使用scanf输入
//a =1 b=0 //a = 1 b=1 //a= 2 b=1 // // //比如: // //输入 3输出为 // //1 //2 //3 // //输入 4 输出为 //1 //2 //3 //5 int main(int argc,const char *argv[]) { int a=1,b=0; int num; scanf("%d", &num); for (int i=0; i<num; i++) { printf("%d ", a+b); int temp = a; a = a+b; b = temp; } return 0; }
2.输出数字序列1/1 2/1,3/2,5/3,8/5,13/8,21/13...,输出个数由键盘输入。注意输入使用scanf输入
//比如: // //输入 3输出为 //a = 1 b= 1 //2/1 a=2 b =1 //3/2 a=3 b =2 //5/3 a=5 b =3 // //输入 4 输出为 //2/1 //3/2 //5/3 //8/5 int main(int argc, const char * argv[]) { int a=1; int b=1; int num; scanf("%d", &num); for (int i=0; i<num; i++) { printf("%d/%d ", a+b, a); int temp = a; a= a+b; b = temp; } return 0; }
3.流程控制(for求阶乘的和 ) 输入n(int类型),打印1!+2!+3!+4!+5!+。。。+n!的值
//输入:2 //输出: //3 // //输入:3 // //输出: //9 //6! = 6*5! = 6*5*4!; //Sn = 1! //Sn = 1! + 2*Sn int main(int argc,const char *argv[]) { int n; int sum = 0; int temp=1; scanf("%d", &n); for (int i=1; i<=n; i++) { temp *=i; sum += temp; } printf("sum = %d ", sum); return 0; }