首页 > 其他分享 >实验3

实验3

时间:2023-11-04 15:45:41浏览次数:34  
标签:int long char 实验 func ans include

实验任务1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define N 80
void print_text(int line , int co1 ,char text[]);
void print_spaces(int n);
void print_blank_lines(int n);
int main()
{
 int line,co1,i;
 char text[N] ="hi,November~";
 srand(time(0));
 for(i = 1;i<=10;i++){
  line = rand()%25;
  co1 = rand()%80;
  print_text(line,co1,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);
}

 

实验任务2

#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;
}

#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;
}

实验任务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){
 long long int ans;
 if(n==1)
 ans=1;
 else
 ans=2*(func(n-1)+1)-1;
 return ans;
}

 实验任务4

4.1

#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 x=1;
 int y=1;
 int z=1;
 int i,j,q,ans;
 ans=0;
 if(n<m)
 ans=0;
 else if(n>=m)
{
 for(i=1;i<=n;i++)
 {
  x=x*i;
 }
  for(j=1;j<=m;j++)
 {
  y=y*j;
 }
  for(q=1;q<=(n-m);q++)
 {
  z=z*q;
 }
 ans=x*1.0/(y*z);
}
 return ans;
 
}

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

 

 

实验任务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);
long long func(int n);
int main()
{
 unsigned int n;
 while(scanf("%u",&n)!=EOF){
  hanoi(n,'A','B','C');
  printf("一共移动了%d次\n",func(n));
 }
 system("pause");
 return 0;
}
long long func(int n){
 long long int ans;
 if(n==1)
 ans=1;
 else
 ans=2*(func(n-1)+1)-1;
 return ans;
}
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);
  moveplate(n,from,to);
  hanoi(n-1,temp,from,to);
 }
}
void moveplate(unsigned int n,char from,char to)
{
 printf("%u:%c--->%c\n",n,from,to);
}
 

 实验任务6

#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 ans,answ=0;
    ans=0;
    while(s*1.0/10!=0)
    {
    if((s%10)%2==1)
    {
        ans=ans*10+s%10;
    }
    s=s/10; 
    }
    while(ans*1.0/10!=0)
    {
        answ=answ*10+ans%10;
        ans=ans/10;
    }
    return answ;
}

 

标签:int,long,char,实验,func,ans,include
From: https://www.cnblogs.com/PomPomDyx/p/17802239.html

相关文章

  • 数据采集与融合技术实验四
    作业①:要求:使用Selenium框架+MySQL数据库存储技术路线爬取“沪深A股”、“上证A股”、“深证A股”3个板块的股票数据信息。主要代码url_list=['http://quote.eastmoney.com/center/gridlist.html#hs_a_board','http://quote.eastmoney.com/center/gridlist......
  • 实验3 C语言函数应用编程
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn......
  • 实验3 类与数组、指针
    实验任务1Point.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()co......
  • 实验三
    test1#include<stdio.h>#include<time.h>#include<stdlib.h>#include<windows.h>#defineN80voidtest_(intline,intcol,chartest[]);voidspace(intn);voidblank(intn);intmain(){intline,col,i;chartest[N]=&......
  • 实验三
     ......
  • 软件设计实验7:单例模式
    实验7:单例模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解单例模式的动机,掌握该模式的结构;2、能够利用单列模式解决实际问题。 [实验任务一]:学号的单一仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。实验要求:1. 画出对应的类图;  2......
  • 软件设计实验6:原型模式
     实验6:原型模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解原型模式的动机,掌握该模式的结构;2、能够利用原型模式解决实际问题。 [实验任务一]:向量的原型用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量......
  • 实验三
    #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()......
  • 实验三
    任务一task1.cpp1#include<iostream>2#include"point.hpp"3#include<vector>4usingstd::vector;5usingstd::cin;6voidoutput(constvector<Point>&v){7for(auto&t:v)8t.show();9}10voidtest()......
  • 实验3—C语言函数应用编程
    1、实验任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_b......