实验1
task1.1.c
#include "stdafx.h" // 打印一个字符小人 #include <stdio.h> #include <stdlib.h> int main() { printf(" O \n"); printf("<H>\n"); printf("I I\n"); printf(" O \n"); printf("<H>\n"); printf("I I\n"); system("pause"); return 0; }
task1.2.c
#include "stdafx.h" // 打印一个字符小人 #include <stdio.h> #include <stdlib.h> int main() { printf(" O O \n"); printf("<H> <H>\n"); printf("I I I I\n"); system("pause"); return 0; }
task2.c
#include "stdafx.h" // 1+2+3+...+n (设n的取值在100以内) //用数学归纳法(求和公式计算) #include <stdio.h> #include <stdlib.h> int main() { int n ,sum; //从键盘输入一个十进制整数n scanf("%d",&n); //待补足代码 sum = n*(n+1)/2; //打印输出求和结果 printf("sum = %d\n",sum); system("pause"); return 0; }
task3.c
调换a和b的数值
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { int a, b, t; a = 3; b = 4; printf("a = %d, b = %d\n", a, b); t = a; a = b; b = t; printf("a = %d, b = %d\n", a, b); system("pause"); return 0; }
task4.c
(1)
(2)
倒序,颠倒顺序
task5.c
// ....1.ll.c.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> # int _tmain(int argc, _TCHAR* argv[]) { float a, b, c; scanf("%f%f%f", &a, &b,&c); if((a+b)>c && a>(b-c)) printf("能构成三角形\n"); else printf("不能构成三角形\n"); system("pause"); return 0; }
task6.c
// ....1.ll.c.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <math.h> int _tmain(int argc, _TCHAR* argv[]) { int year; year = (1000000000)/(365*3600); if(((year*10)%10) < 5) printf("year = %d\n", year); else printf("year = %d\n", year + 1); system("pause"); return 0; }
task7.c
(1)原版前后两次生成的“随机数”是一样的
// ....1.ll.c.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <math.h> int _tmain(int argc, _TCHAR* argv[]) { int n; n= (rand()%(60-100+1)+60); printf("n = %d\n", n); system("pause"); return 0; }
(2)更改后不同次所生成的随机数是不同的
// ....1.ll.c.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int _tmain(int argc, _TCHAR* argv[]) { int n; srand((unsigned)time(NULL)); n= (rand()%(60-100+1)+60); printf("n = %d\n", n); system("pause"); return 0; }
task8.c
(1)
// ....1.ll.c.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <math.h> int _tmain(int argc, _TCHAR* argv[]) { char ans1, ans2; printf("每次课前认真预习,课后及时复习了没?(输入Y或y表示有,输入N或n表示没有) :"); ans1 = getchar(); getchar(); printf("\n动手敲代码实践了没?(输入Y或y表示有,输入N或n表示没有) :"); ans2 = getchar(); if((ans1 == 'y' || 'Y') && (ans2 == 'y' || 'Y')) printf("\n罗马不是一天建成的,继续保持哦:)\n" ); else printf("\n罗马不是一天毁灭的,我们来建设吧\n"); system("pause"); return 0; }
(2)
如果去掉了单独的“getchar()”一行,那么我们就只能对“getchar()”进行一次赋值,即无法对ans2再进行单独赋值。此时ans1永远等于ans2。
而如果这样的话,最终输出的结果就只由第一个问题决定,与第二个问题无关,不符合实验的目的与要求。