首页 > 其他分享 >实验3

实验3

时间:2023-10-31 19:22:37浏览次数:24  
标签:int long char 实验 func printf include

实验任务1

#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, November~";
srand(time(0)); // 以当前系统时间作为随机种子
for(i = 1; i <= 10; ++i) {
line = rand() % 25;
col = rand() % 80;
print_text(line, col, text);
Sleep(1000); // 暂停1000ms
}
return 0;
}
// 打印n个空格
void print_spaces(int n) {
int i;
for(i = 1; i <= n; ++i)
printf(" ");
}
// 打印n行空白行
void print_blank_lines(int n) {
int i;
for(i = 1; i <= n; ++i)
printf("\n");
}
// 在第line行第col列打印一段文本
void print_text(int line, int col, char text[]) {
print_blank_lines(line-1); // 打印(line-1)行空行
print_spaces(col-1); // 打印(col-1)列空格
printf("%s", text); // 在第line行、col列输出text中字符串
}

这似乎是一个有趣的文本动画,常用与游戏加载过程

 

实验任务2

// 利用局部static变量的特性,计算阶乘
#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;
printf("p = %lld\n", p);
p = p * n;
return p;
}

// 练习:局部static变量特性
#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;
}  //第二次运行i=12,m=12+5 

 

实验任务3

#include <stdio.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);
    }
return 0;
}
long long func(int n){
    if(n==0)
      return 0;
    else
      return (func(n-1)+1)*2-1;
}

 

实验任务4

迭代

#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 up=1,down=1,i,j;
    for(i=n;i>=n-m+1;i--){
        up*=i;
    }
    for(j=1;j<=m;j++){
        down*=j;
    }
    return up/down;
} 

递归

#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){
    if(n<m)
      return 0;
    else if(m==0)
      return 0; 
    else if(m==1)
      return n;
    else 
      return func(n-1,m)+func(n-1,m-1);
}

实验任务5

#include<stdio.h> 
#include<stdlib.h> 
void hanoi(unsigned int n, char from, char temp,char to);/*递归函数声明*/ 
void moveplate(unsigned int n, char from, char to); /*移动函数的声明¥*/ 
int x;
int main() {
   unsigned int n;
   while(scanf("%u",&n)==1){   /*输入盘子数目*/ 
   x=0;     
   hanoi(n, 'A','B','C'); 
   printf("一共移动了:%d次\n",x); 
   }
   return 0; 
}
void hanoi(unsigned int n, char from, char temp, char to){
    if(n==1) {
      moveplate(n, from, to); 
    }
    else{
    hanoi(n-1,from,to,temp);    /*n-1个盘子从A以 C为中转移到B上*/ 
    moveplate(n, from, to);     /*将盘子n从A移到c上*/ 
    hanoi(n-1,temp, from, to); /*将n-1个盘子从B以A为中转移到c上*/
    }
}
void moveplate(unsigned int n,char from, char to) {
    printf("%u:%c-->%c\n",n, from,to); 
    x++;
}

#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){ int i=1,j=0,k,a[20]={0},b[20]={0}; long ans=0; while(s!=0){ a[i]=s%10; //将每一位数存入数组,i越小,位数越小 s=s/10; if(a[i]%2!=0){ j++; b[j]=a[i]; //将奇数存入另一个数组,第j个数是奇数就是b【j】 } i++; } for(k=1;k<=j;k++){ ans+=a[k]*pow(10,k-1); } return ans; }

实验任务7

#include<stdio.h>
#include<math.h>
int main(){
    int a,b,i,n[20]={0},j,k,l,m,t;
    long long s=0; 
    for(i=1;i<=100;i++){
        s=0;
        j=1;
       a=i*i;
       b=i*i*i;
        while(a!=0){
            n[j]=a%10;     
            a=a/10;
            j++;
        }
        while(b!=0){
            n[j]=b%10;     
            b=b/10;
            j++;
        }
        for(m=1;m<j;m++){
           for(k=1;k<=j;k++){
              if(n[k]<n[k+1]){
                t=n[k];
                n[k]=n[k+1];
                n[k+1]=t;
                }    
            }    
        }
        for(l=1;l<j;l++){
            s=s*10+n[l];
        }
        if(s==9876543210)
          printf("%d",i);
    
    }
    
    return 0;
}

 

标签:int,long,char,实验,func,printf,include
From: https://www.cnblogs.com/cd1747850183/p/17801057.html

相关文章

  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain()......
  • 使用EMR+DLF+OSS-HDFS进行数据湖分析(阿里云实验)
    实验地址:https://developer.aliyun.com/adc/scenario/exp/f7cf565798e34710acf483ba56e6ebf6hadoopfs操作oss#上传文件hadoopfs-putlogtail.shoss://u-5stubb6d.cn-shanghai.oss-dls.aliyuncs.com/#新建目录hadoopfs-mkdiross://u-5stubb6d.cn-shanghai.oss-dls.aliyu......
  • 实验三
    tesk1代码#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,col,i;chartext[N]="hi,November~";......
  • 实验3
    任务1源码 任务1结果生成随机弹幕: 任务2源码  任务2结果 任务3源码  任务3结果  任务4源码迭代 递归 任务4结果 迭代递归 任务5源码  任务5结果  任务6源码  任务6结果  任务7源码 1#define_CRT_SECURE_NO......
  • 实验三
    1.实验任务1每间隔1秒生成随机位置的hiNovermber实验任务2实验任务3实验任务4实验任务5 实验任务6 ......
  • 实验3 C语言函数应用编程
    一,实验目的二,实验准备三,实验内容1,实验任务1task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<Windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//......
  • 实验三
      实验3.1实验3.1源码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(in......
  • 深入浅出Docker应用(阿里云实验)
    (Docker安装和配置)一、docker安装yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2yuminstall-yjqyum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repoyuminstalldocker-ce.x86_64yuminstall-ydocker-ce.x86_643:......
  • 实验3
    一、实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);?//函数声明voidprint_spaces(intn);?//函数声明voidprint_blank_lines(intn);//......
  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<math.h>3#include<stdlib.h>4#include<time.h>5#include<windows.h>6#defineN807voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidpr......