数组练习1(更新中)

1 上楼梯

 

 

 

 

 

 

 

0 0
1 1
2 2
3 4
4 7 F[4]=F[1]+F[2]+F[3]
5 ?13 F[5]=F[2]+F[3]+F[4]
6 ? 24 F[6]=F[3]+F[4]+F[5]
7 ? 44

1 输入n (n<73) ,n 为台阶数
2 定义一个数组int F[73]
2 F[0]=0;F[1]=1;F[2]=2;F[3]=4
for (int i=4;i<=n;i++)
F[i]=F[i-3]+F[i-2]+F[i-1];
F[i]=F[i-3]+F[i-2]+F[i-1];
F[5]=F[2]+F[3]+F[4]
cout<<F[n];

void s1()
{
    //上楼梯
    int n;
    cin>>n;
    unsigned long long F[75]= {0,1,2,4};
    for(int i=4; i<=n; i++)
      F[i]=F[i-3]+F[i-2]+F[i-1];
    cout<<F[n];
} 

2 分糖

 

 

 

 

(1) 初始化:赋初值 1->3 2->5 3->7 ...... 99->? 199, 100-> ?201
1 2 3 4 ... 99 100
3 5 7 9 ... 199 201
规律 3+2*(i-1) i: 1......100
a[0] ...... a[99] 初始化,循环赋值 a[i]=3+2*i; i:0......99
(2) 第一轮分糖(数组下标:0..99)
a[i]=a[i]/2;
a[i+1]+=a[i]; //i 0......98
if i==3 a[3]=a[3]+a[2] //i:0...99

// a[99]=a[99]/2
(3) 输出 数组a

void s2()
{
    //分糖
  int a[100];
  for(int i=0; i<=99; i++)
    a[i]=3+2*i;
  for(int i=0; i<=98; i++)
  {
    a[i]=a[i]/2;
    a[i+1]+=a[i];
  }
  //a[99]=a[99]/2;
  //a[0]+=a[99];
  for(int i=0; i<=99; i++)
    cout<<a[i]<<endl;
}

3 放花炮

 

 

 

 

a[100*100]={0};
输入b[] 时间间隔
for (int i=0;i<人数;i++)
{
for (j=0;j<炮的数字*b[i];j+b[i])
{
a[i]=1;
}
}
sum =0; //sum 为炮响的次数
if a[i]==1 sum=sum+1;

void f4()
{
    int n;//人数 
    int A[n];  //每个人点炮的时间间隔 
    int b;//炮数 
    int B[10000]={0};//时间轴 
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>A[i];
    }
    cin>>b;
    // 3
    // 1 2 3
    // 4
    for (int i=0;i<n;i++)
    {
        //按照人循环 
        for (int j=0;j<b*A[i];j+=A[i])
        {
            //按照时间 间隔循环
            B[j] =1;
        }
    }
    //求和 
    int sum=0;
    for (int i=0;i<10000;i++) 
    sum=sum+B[i];
    cout<<sum;    
 } 

 4 开关灯1

 

 

 

 

#include <cstring>
void s4()
{
    //开关灯1
    freopen("light1.in","r",stdin);
    freopen("light1.out","w",stdout);
    /*
    int a,b;
    cin>>a>>b;
    cout<<a<<" "<<b<<endl;
    */
    int f[101]; //
    memset(f,0,sizeof(f));//全部数组元素赋值为0
    for(int  i=1; i<=100; i++)
    {
        if(i%2==0) 
            f[i]=!f[i];
    }
     for(int  i=1; i<=100; i++)
     {
         if(i%3==0)
         {
             f[i]=!f[i];
         }
     }
     for(int  i=1; i<=100; i++)
     {
         cout<<f[i]<<" ";
     }
     cout<<endl;
    fclose(stdin);//关闭重定向输入    
    fclose(stdout);//关闭重定向输出
} 

 

 

void s5()
{
    //开关灯2
    bool a[10000];  
    long long n;  
    cin>>n;  
    for(long long i=1;i<=n;i++)  
    {
         a[i]=false;  
    }
    for(long long i=1;i<=n;i++)  
    {
        for(long long j=i;j<=n;j+=i) 
        {
            a[j]=!a[j];  
        }
    }
    int ans=0; 
    for(long long i=1;i<=n;i++)  
    {
         if(a[i])
         {
              ans++; 
        }
    }
    cout<<ans;  
    cout << endl;  
}
#include<cmath>
void s5_1()
{
    unsigned long long n;  
    cin>>n;  
    cout<<int(sqrt(n))<<endl;  //sqrt 数字的平方根
}

 

 

void s8()
{
    //统计各数据个数
    int n1=0,sum=0;
    int a[21]={0};
    /* 
    while (cin>>n1)
    {
        // Ctrl+Z  输入结束 
        a[n1]++;
        sum++;
    } 
    */
    for (int i=0;i<100000;i++)
    {
        cin>>n1;
        if (n1>20)  break;
        a[n1]++;
        sum++;
    }
    cout<<sum<<endl;        
    for (int i=0;i<=20;i++)
    {
        cout<<a[i];
    } 
}

 

void s6()
{
    //冒泡排序

    /*
2,6,4,32,12,9,55,26,37,73,
2,4,6,12,9,32,26,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
2,4,6,9,12,26,32,37,55,73,
*/

    freopen("sort.in","r",stdin);
    freopen("sort.out","w",stdout);
    //用冒泡法对n个数排序(由小到大) O(n2)
    int a[10005],n;//在没赋初值的情况下,数组a里的各元素值未知
    cin>>n;
    for(int i=1; i<=n; i++)//从下标1即a[1]开始,a[0]不参与运算
    {
        cin>>a[i];
    }    
    for(int j=1; j<=n-1; j++) //大循环共n-1次
    {
        for(int i=1; i<=n-j; i++) //每个小循环的步数逐次递减
        {
             if(a[i]>a[i+1])//比较两数,如前面数大于后面数,则小数上浮,大数下沉
             {
                 swap(a[i],a[i+1]);
             }        
        }    
    }
    for(int i=1; i<=n; i++)
    {
        cout<<a[i]<<" ";
    }
    fclose(stdin);//关闭重定向输入    
    fclose(stdout);//关闭重定向输出
} 

 

void s6_1()
{
    //改进的冒泡排序 
    freopen("sort.in","r",stdin);
    freopen("sort.out","w",stdout);
    int n;
    cin>>n;
    int a[n+1];//可动态定义数组大小,但不能同时赋值,如int a[n+1]={0};
    //时间复杂度 O(nlogN)
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    int k=2,LastSwap;//k靠近数组左端的位置,LastSwap为最后交换的位置
    while(k<n)//当左端a[k]的位置小于右端a[n]的位置,即还有数要比较
    {
        LastSwap=n;            //先设定这一轮扫描最后交换位置为n
        for(int j=n; j>=k; j--)//从后向前扫描到k(从前向后扫描也一样)
        {
            if(a[j]<a[j-1])    //如果后面的数比前面的数小
            {
                swap(a[j],a[j-1]); //交换a[j]和a[j-1]的值
                LastSwap=j;        //记录最后的变更位置
            }
        }
        k=LastSwap;            //下一轮只需比较到上一轮的LastSwap
    }    
    for(int i=1; i<=n; i++)
    {
         cout<<a[i]<<" ";
    }
    fclose(stdin);//关闭重定向输入    
    fclose(stdout);//关闭重定向输出
}

 分数的精确值

 

 

 

void s10()
{
    //求分数的精确值
    int shang1[101];
    int m,n;
    cin>>m>>n;//m/n 0<m<n<=100
    cout<<m<<"/"<<n<<"=0.";
    for (int i=1;i<=100;i++)
    {
        m=m*10;
        shang1[i]=m/n;
        m=m%n;
    }
    for (int i=1;i<=100;i++)
    {
        cout<<shang1[i];
    }
    cout<<endl;     
}

 求分数精确值2

void s11()
{
    //求分数的精确值
    int shang1[101]={0};//保存商 
    int yushu[101]={0};//保存余数 
    int m,n,beg,end,j=0;
    int flag=0;    //无线循环小数 
    cin>>m>>n;//m/n 0<m<n<=100
    //cout<<m<<"/"<<n<<"=0.";
    for (int i=1;i<=100;i++) //i: 商的位数
    {
        yushu[i]=m;          //余数    
        m=m*10;              //余数扩大10位
        shang1[i]=m/n;       //
        m=m%n;               //求余数
             
        if (m==0)            //整除了
          {beg=1;end=i;flag=1;break;}
        for(j=i-1;j>=1;j--)    
        {
            if(yushu[j]==yushu[i])
            {
                beg=j;
                end=i;
                break;
            }
        }
        if(j>0)                                    //找到了相同的余数,循环节就出现了
        break;        
    }
    if(flag)
    {
        //结果为有限循环小数
        cout<<"begin:"<<beg<<"    "<<"end:"<<end<<endl;
        cout<<"The result of M/N is a finite fraction of 0.";
        for(j=beg;j<=end;j++)          
          cout<<shang1[j];
    }
    else
    {
        //结果为无限循环小数        
        cout<<"The result of M/N is an infinite loop fraction of 0.";
        for(j=1;j<end;j++)            
            cout<<shang1[j];        
        cout<<endl;       
        cout<<"Its loop section starts from the bit and the loop section is: "<<beg<<" To "<<end-1<<endl;
        for(j=beg;j<end;j++)            
            cout<<shang1[j];        
    }
    cout<<endl;      
 } 
 

 

 

#define MAX 100
 void s11_1()
 {
     //From https://blog.csdn.net/qq_43800467/article/details/100061720
    int m,n,i=0,j,beg,end;
    int flag=0;                                        //表示结果是否是无限循环小数
    int a[MAX]={0},b[MAX]={0};                                //分别存放相除过程中的商和余数
    //printf("Please input m,n :");
    cout<<"Please input m,n :";
    //scanf("%d%d",&m,&n);
    cin>>m>>n;
    b[i]=m;
    a[i++]=m*10/n;
    while(i<MAX)
    {
        b[i]=b[i-1]*10%n;
        if(b[i]==0)                                    //整除了
        {beg=0;end=i;flag=1;break;}
        a[i]=b[i]*10/n;
        for(j=i-1;j>=0;j--)                            //判断前面有无此余数,有则说明循环节出现了
            if(b[j]==b[i])
            {    beg=j;
                end=i;
                break;
            }
        if(j>=0)                                    //找到了相同的余数,循环节就出现了
        break;
        i++;
    }

  /*输出结果*/
    if(flag)                                        //结果为有限循环小数
    {
        //printf("The result of M/N is a finite fraction of 0.");
        cout<<"begin:"<<beg<<"    "<<"end:"<<end<<endl;
        cout<<"The result of M/N is a finite fraction of 0.";
        for(j=beg;j<end;j++)
          //printf("%d",a[j]);
          cout<<a[j];
    }
    else                                            //结果为无限循环小数
    {
        //printf("The result of M/N is an infinite loop fraction of 0.");
        cout<<"The result of M/N is an infinite loop fraction of 0.";
        for(j=0;j<end;j++)
            //printf("%d",a[j]);
            cout<<a[j];
        //printf("...\n");
        cout<<endl;
        //printf("Its loop section starts from the %d bit and the loop section is: ",beg+1);
        cout<<"Its loop section starts from the bit and the loop section is: "<<beg+1<<endl;
        for(j=beg;j<end;j++)
            //printf("%d",a[j]);
            cout<<a[j];
    }

 }
  

 

执行分析

 

posted @ 2023-04-10 09:47  jhtchina  阅读(19)  评论(0)    收藏  举报