zoukankan      html  css  js  c++  java
  • C代码

      1 //----------------------------1
      2 #include<stdio.h>
      3 void main()//斐波那契数列
      4 {
      5     int fib[12],k;
      6     fib[0]=1;
      7     fib[1]=1;
      8     for(k=2;k<12;k++)
      9         fib[k]=fib[k-1]+fib[k-2];//数组,无数组应该是栈吧
     10     for(k=0;k<12;k++)
     11         printf("%d",fib[k]);
     12     printf("\n");
     13 }
     14 //------------------------2//从小到大排列
     15 #include<stdio.h>
     16 #define N 10
     17 void main()
     18 {
     19     int data[N];
     20     int i,j,temp;
     21     printf("Please input N(10) numbers:\n");
     22     for(i=0;i<N;i++)
     23         scanf("%d",&data[i]);
     24     for(i=1;i<N;i++)
     25         for(j=0;j<N-i;j++)
     26             if(data[j]>data[j+1])
     27             {
     28                 temp=data[j];
     29                 data[j]=data[j+1];
     30                 data[j+1]=temp;
     31             }
     32             printf("the result of sort:\n");
     33             for(i=0;i<N;i++)
     34                 printf("%d",data[i]);
     35             printf("\n");
     36 }
     37 //-----------------------3//较简单的矩阵转置
     38 #include<stdio.h>
     39 #define M 8
     40 #define N 8
     41 void main()
     42 {
     43     int i,j,a[M][N],b[M][N];
     44     int row,col;
     45     printf("Input row,col:\n");//row代表行,col代表列
     46     scanf("%d%d",&row,&col);
     47     printf("Please input array:\n");//3行4列
     48     for(i=0;i<row;i++)
     49         for(j=0;j<col;j++)
     50             scanf("%d",&a[i][j]);
     51     for(i=0;i<row;i++)//双重循环实现矩阵的转置
     52         for(j=0;j<col;j++)
     53             b[j][i]=a[i][j];
     54     printf("\n output transpose array:\n");
     55     for(i=0;i<col;i++)
     56         {
     57             for(j=0;j<row;j++)
     58                 printf("%d ",b[i][j]);
     59             printf("\n");//行输出后,实现换行
     60     }
     61 }
     62 //------------------------4杨辉三角
     63 #include<stdio.h>
     64 #define N 6
     65 void main()
     66 {
     67     int i,j,a[N][N];
     68     for(i=0;i<N;i++)
     69     {
     70         a[i][i]=1;
     71         a[i][0]=1;
     72     }
     73     for(i=2;i<N;i++)
     74         for(j=1;j<i;j++)
     75             a[i][j]=a[i-1][j]+a[i-1][j-1];
     76     for(i=0;i<N;i++)
     77     {
     78         for(j=0;j<=i;j++)
     79             printf("%d ",a[i][j]);
     80         printf("\n");
     81     }
     82 }
     83 //--------------------------5
     84 #include<stdio.h>
     85 #define N 100
     86 void main()
     87 {
     88     char a[N];
     89     int i,c1,c2,c3,c4;
     90     c1=c2=c3=c4=0;
     91     printf("输入任意一个字符串:\n");
     92     gets(a);//全部输入a[N]中,gets(a)
     93     for(i=0;a[i]!='\0';i++)
     94     {
     95         if(a[i]>='A'&&a[i]<='Z')
     96             c1++;
     97         else
     98             if(a[i]>='a'&&a[i]<='z')
     99                 c2++;
    100             else
    101                 if(a[i]>='0'&&a[i]<='9')
    102                     c3++;
    103                 else
    104                     c4++;
    105     }
    106     printf("c1=%d,c2=%d,c3=%d,c4=%d\n",c1,c2,c3,c4);
    107 }
    108 //---------------------------6
    109 #include<stdio.h>
    110 #include<string.h>
    111 #define N 100
    112 void main()
    113 {
    114     char str[N];
    115     int i,len;
    116     printf("输入任意一个字符串:\n");
    117     gets(str);
    118     len=strlen(str);
    119     for(i=len-1;i>=0;i--)
    120         putchar(str[i]);
    121     printf("\n");
    122 }
    123 //----------------7给同学做的作业,丝毫不精致
    124 #include<iostream>
    125 #define N 100
    126 using namespace std;
    127 int fun(int a[4][4],int b[N])
    128 {
    129     int sum=0,k=0,count=0;
    130     for(int i=0;i<4;i++)
    131     {
    132         for(int j=0;j<4;j++)
    133             if(a[i][j]==0)
    134                 {
    135                     for(j=j;j<4;j++)
    136                     if(a[i][j]<0)
    137                         {b[k++]=a[i][j];
    138                     count++;
    139                     }
    140             }
    141     }
    142     for(int i=0;i<count;i++)
    143         sum+=b[i];
    144     return sum;
    145 }
    146 int main()
    147 {
    148     int a[4][4]={{-2,0,-3,-1},{-8,2,0,-4},{0,3,-3,-12},{21,0,-13,3}},b[N];
    149     cout<<"the sum is:"<<endl;
    150     cout<<fun(a,b)<<endl;
    151 }
  • 相关阅读:
    MySQL-子查询,派生表,通用表达式
    MySQL-插入数据(INSERT)
    IDEA中如何使用debug调试项目 一步一步详细教程
    Java相对路径/绝对路径总结
    jsp九个内置对象、四个域对象及Servlet的三大域对象
    浅析MVC模式与三层架构的区别
    三层架构详解
    Java集合中List,Set以及Map等集合体系详解
    POJ3233 [C
    HDU 2829 [Lawrence] DP斜率优化
  • 原文地址:https://www.cnblogs.com/herizai/p/3127721.html
Copyright © 2011-2022 走看看