tsak1
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define N 80
void print_text(int line, int col, char text[]);
void print_spaces(int n);
void print_blank_lines(int n);
int main() {
int line, col, i;
char text[N] = "hi, April~";
srand(time(0));
for(i = 1; i <= 10; ++i) {
line = rand() % 25;
col = rand() % 80;
print_text(line, col, text);
Sleep(1000);
}
return 0;
}
void print_spaces(int n) {
int i;
for(i = 1; i <= n; ++i)
printf(" ");
}
void print_blank_lines(int n) {
int i;
for(i = 1; i <= n; ++i)
printf("\n");
}
void print_text(int line, int col, char text[]) {
print_blank_lines(line-1);
print_spaces(col-1);
printf("%s", text);
}
程序实现功能为每隔1s在第line行第col列打印文本“hi,April~”,共生成十段文本(由主函数中循环控制),每段文本输出由print_text函数实现,其中line和col由随机数生成,print_spaces函数和print_blank_lines函数起定位作用,分别负责打印n个空格和n行空白行。
task2-1:
点击查看代码
#include <stdio.h>
long long fac(int n);
int main() {
int i, n;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i)
printf("%d! = %lld\n", i, fac(i));
return 0;
}
long long fac(int n) {
static long long p = 1;
p = p * n;
return p;
}
tsak2-2
点击查看代码
#include <stdio.h>
int func(int, int);
int main() {
int k = 4, m = 1, p1, p2;
p1 = func(k, m);
p2 = func(k, m);
printf("%d, %d\n", p1, p2);
return 0;
}
int func(int a, int b) {
static int m = 0, i = 2;
i += m + 1;
m = i + a + b;
return m;
}
理论上分析结果为8与17,与实验运行结果相一致,局部static变量的特点是它位于静态存贮区,在函数调用结束后,它的值仍然存在,并可以影响到下一次调用的过程。
tsak3
点击查看代码
#include <stdio.h>
#include<stdlib.h>
long long func(int n);
int main() {
int n;
long long f;
while (scanf("%d", &n) != EOF) {
f = func(n);
printf("n = %d, f = %lld\n", n, f);
}
system("pasue");
return 0;
}
long long func(int n)
{
long long f;
if(n==1)
{
f=1;
}
else
{
f=2*func(n-1)+1;
}
return f;
}
task4-1
点击查看代码
#include <stdio.h>
#include<stdlib.h>
int func(int n, int m);
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
system("pause");
return 0;
}
int func(int n,int m)
{
int i,s=1,k=1;
if(m>n)
{
return 0;
}
else if(m==0||n==0)
{
return 1;
}
else
{
for(i=1;i<=m;i++)
{
s*=n-i+1;
k*=i;
}
return s/k;
}
task4-2
点击查看代码
#include <stdio.h>
int func(int n, int m);
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
return 0;
}
int func(int n,int m){
int i;
if(m>n){
return 0;
}
else if(m==0||n==0){
return 1;
}
else{
i=func(n-1,m)+func(n-1,m-1);
return i;
}
}
tsak5
点击查看代码
#include<stdio.h>
#include<stdlib.h>
void hanoi(int n,char from,char temp,char to);
void moveplate(int n,char from,char to);
int sum(int n);
int main()
{
int n,k;
char A,B,C;
while(scanf("%d",&n)!=EOF)
{
hanoi(n,'A','B','C');
k=sum(n);
printf("一共移动了%d次\n",k);
system("pause");
}
return 0;
}
void moveplate(int n,char from,char to)
{
printf("%d: %c--> %c\n",n,from,to);
}
void hanoi(int n,char from,char temp,char to)
{
if(n==1){
moveplate(n,from,to);
}
if(n>1){
hanoi(n-1,from,to,temp);
moveplate(n,from,to);
hanoi(n-1,temp,from,to);
}
}
int sum(int n){
if(n==1){
return 1;
}
if(n>1){
return 2*sum(n-1)+1;
}
}
task6
点击查看代码
#include <stdio.h>
#include <math.h>
long func(long s);
int main() {
long s, t;
printf("Enter a number: ");
while (scanf("%ld", &s) != EOF) {
t = func(s);
printf("new number is: %ld\n\n", t);
printf("Enter a number:");
}
return 0;
}
long func(long s){
long ans;
long digit,t=1;
ans=0;
while(s!=0){
digit=s%10;
if(digit%2){
ans+=t*digit;
t*=10;
}
s/=10;
}
return ans;
}