C语言算法第五源代码以及流程图
程序流程图 内存 执行过程 数计学院计算机科学与技术 110座机电话号码6戴晨丹
1 /* example5-3.cpp */
2 #include
3 Int main
4
5 Intx,y, max;
6 printf “请输入两个整数:” ;
7 scanf “%d%d”, &x, &y ;
8 If x y
9 max x;
10 else
11 max y;
12 printf “%d和%d的较大值是:%d\n”, x, y, max ;
13 return 0;
14 5.4符号函数:
1 /* example5-4.cpp */
2 #include
3 Int main
4 5 Int x, sign;
6 print “请输入x的值:” ;
7 scanf “%d”, &x ;
8 if x 0
9 sign 1;
10 else if x 0
11 sign -1;
12 else
13 sign 0;
14 printf “x的值为:%d,符号函数的值为:%d\n”, x, sign ;
15 return 0;
16 5.5将三个整数由小到大输出
1 /* example5-4.cpp */
2 #include
3 Int main
4 5 Int x, y, z, temp;
6 print “请输入三个整数:” ;
7 scanf “%d%d”, &x, &y, &z ;
8 if x y
9 10 temp x;
11 x y;
12 y temp;
13 14 if z x
15 16 temp z;
17 z y;
18 y x;
19 x temp;
20 21 else if z y
22 23 temp y;
24 y z;
25 z temp;
26 27 printf “这三个整数由小到大依次是:%3d %3d %3d\n”, x, y, z ;
28 return 0;
29 5.6.百分制转换
1 /* example5-6.cpp */
2 #include
3 Int main 4 5 double score;
6 int temp;
7 char grade;
8 printf “请输入一个百分制成绩:” ;
9 scanf “%1f”,&score ;
10 temp int score/10;
11 switch temp
12 13 case 10:
14 case 9: grade ‘A’; break;
15 case 8: grade ‘B’; break;
16 case 7: grade ‘C’; break;
17 case 6: grade ‘D’; break;
18 default: grade ‘E’; break;
19 20 printf “百分制成绩%4.1f对应的等级制成绩为:%c\n,score, grade ;
21 return 0;
22 计算n!
1 /* example5-6.cpp */
2 #include
3 int main
4 5 int n, i 1;
6 long int result 1;
7 printf “请输入一个整数:” ;
8 scanf “%d”, &n ;
9 while i n
10 11 result result*I;
12 i++;
13 14 printf “%d的阶乘为: %d\n”, n, result ;
15 return 0;
16 水仙花数
1 Duty5-2.cpp */
2 #include
3 Int main
4 5 int x,x1, x2, x3, y;
6 printf “请输入一个三位整数:” ;
7 scanf “%d”, &x ;
8 x1 x%10;
9 y x/10;
10 x2 y%10;
11 x3 y/10;
12 if x1*x1*x1+x2*x2*x2+x3*x3*x3 x ;
13 printf “%d是水仙花数\n”, x ;
14 else
15 printf “%d不是水仙花数\n”, x ;
16 return 0;
17 situation, causing the livelihood of 100 tailings project management project and South Mining Technology in two engineering work l