天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

二维数组的乘积计算:

for(i=0;i<N;i++)
   for(j=0;j<N;j++)
      for(k=0,c[i][j] = 0.0;k<N;k++)
         c[i][j] +=a[i][k] * b[k][j];



二维数组的分配:

#include<stdio.h>
#include<stdlib.h>

int **malloc2d(int r, int c) //二维数组分配函数
{
    int i;
    int **t = (int **)malloc(r * sizeof(int *));
    for(i=0;i<r;i++)
        t[i] = (int *)malloc(c * sizeof(int));
    return t;
}

int main()
{
    int i,j;
    int **a =malloc2d(3,3);
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            a[i][j]=i+j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
    return 0;
}

综合计算二维数组相乘的乘积。
代码如下:

#include<stdio.h>
#include<stdlib.h>

int **malloc2d(int r, int c)
{
    int i;
    int **t = (int **)malloc(r * sizeof(int *));
    for(i=0;i<r;i++)
        t[i] = (int *)malloc(c * sizeof(int));
    return t;
}

int main()
{
    int i,j,k;
    int **a =malloc2d(3,3);
    int **b =malloc2d(3,3);
    int **c =malloc2d(3,3);
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            a[i][j]=i+j;
            b[i][j]=i+j;
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
    printf("\n");
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      for(k=0,c[i][j] = 0.0;k<3;k++)
         c[i][j] +=a[i][k] * b[k][j];

    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d ",c[i][j]);
      printf("\n");
    }

    return 0;
}
    

posted @ 2012-08-19 15:40 hoshelly 阅读(199) | 评论 (0)编辑 收藏

//整数排序,利用qsort函数
#include<stdio.h>
#include<stdlib.h>
int compare(const void * a,const void * b)
{
    return *(int*)a - *(int*)b;
}
int main()
{
    int a[20],i;
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    qsort(a,10,sizeof(int),compare);
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
    return 0;
}

posted @ 2012-08-19 11:11 hoshelly 阅读(232) | 评论 (0)编辑 收藏

//字符串排序,利用qsort函数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Nmax 1000
#define Mmax 10000
char buf[Mmax]; int M=0;
int compare(const void *i, const void *j)
{
    return strcmp(*(char **)i, *(char **)j);
}

int main()
{
    int i, N;
    char *a[Nmax];
    for(N=0; N<Nmax; N++)
    {
        a[N]= &buf[M];
        if(scanf("%s", a[N]) == EOF) break;
        M+=strlen(a[N])+1;
    }

    qsort(a,N,sizeof(char*),compare); //四个参数分别代表:待排序的数组首地址,数组中待排序的元素数量,各元素占用的空间,排序函数(确定排序顺序)
    for(i=0;i<N;i++)
        printf("%s\n",a[i]);

    return 0;
}

posted @ 2012-08-19 10:49 hoshelly 阅读(394) | 评论 (0)编辑 收藏

编写一程序,确定一个给定字符串中最长的空格序列的长度。

#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N];
    int i,j,k=0,count[100]={0},max;
    printf("Input the a string: "); //输入字符串
    gets(a);
    for(i=0;a[i]!=0;i++)
    {
        while(a[i++] == ' ')
        {
            count[k]++;
            if(a[i+1]!=' ')
                k++;
        }
    }
    for(j=0;j<k;j++)
    {
        max=count[0];
        if(count[j]<count[j+1])
            max=count[j+1];
    }
    printf("%d\n",max);
    return 0;
}
    

posted @ 2012-08-18 21:59 hoshelly 阅读(197) | 评论 (0)编辑 收藏

// 水题
#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N];
    int i,j;
    printf("Input the a string: "); //输入字符串
    gets(a);
    for(i=0;a[i]!=0;i++)
    {
        while(a[i] == ' ' && a[i+1] == ' ')
        {
            for(j=i;a[j]!=0;j++)
                a[j]=a[j+1];
        }
    }
    printf("%s",a);
    return 0;
}
    

posted @ 2012-08-18 21:44 hoshelly 阅读(169) | 评论 (0)编辑 收藏

编写一个程序,输入一个字符串 a ,并且输入一组子串的序列,字符序列之间用空格隔开,打印出那些为字符串 a 的子串。(本次输入的子串不分先后,可以 a 的子串在前面,亦可非 a 的子串在前面)

代码测试通过:

#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N],b[100];
    int i,j;
    printf("Input the a string: "); //输入字符串
    gets(a);
    while((scanf("%s",b)) != EOF ) //输入要检测的子串
    {
        for(i=0;a[i]!=0;i++) //开始检测
        {
            for(j=0;b[j]!=0;j++) 
            {
                if(a[i+j]!=b[j]) //如果字符不匹配,则退出本次循环,进行第一层循环i+1
                    break;
            }
            if(b[j] == '\0') //如果b[j] = '\0',则说明字符匹配到了子串的最后,匹配成功,输出子串
                printf("%s\n",b);
        }
    }
    return 0;
}

输出结果实例:

posted @ 2012-08-18 21:31 hoshelly 阅读(234) | 评论 (0)编辑 收藏

#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N],b[N];
    int i,j=0,k,count=1,z;
    static int c=0;
    printf("Input the a string: "); //输入字符串
    gets(a);
    printf("Input the substring: "); //输入检测的子串,按先a的子串,后非a的子串输入
    gets(b);

    for(i=0;a[i]!=0;i++)
    {
        z=0;
        for(j=c;b[j]!=0;j++,z++)
        {
            
            if(a[i+z]!= b[j] && b[j]!= ' ')
                break;
            else if( b[j] == ' ')
            {
                for(k=c;k<j;k++)
                {
                    printf("%c",b[k]); //打印出子串,不是子串的不打印出
                }
                printf("\n");
                c=j+1;//跳脱空格
            }
            
        }
    }
    return 0;
}
    

posted @ 2012-08-18 21:14 hoshelly 阅读(216) | 评论 (0)编辑 收藏

编写一程序,检查一给定字符串是否是回文的程序(順读和倒读都是一样的字符串),不考虑空格。例如,对于字符串 if i had a hifi ,你的程序应该报告成功,否则打印失败。

代码测试通过:

#include<stdio.h>
#include<string.h>
#define N 1000
int main()
{
    char a[N];
    int i,n,m;
    printf("Input the string: ");
    gets(a);
    m=strlen(a);
    n=strlen(a)/2;
    for(i=0;i<n;i++,m--)
    {
        if(a[i] == ' ')
        {
            i++;
        }
        if(a[m-1] == ' ')
        {
            m--;
        }

        if(a[i] != a[m-1])
            break;
    }
    if( i == n)
        printf("succeed!\n");
    else
        printf("No\n");
     
    return 0;
    
}

posted @ 2012-08-18 15:20 hoshelly 阅读(927) | 评论 (0)编辑 收藏

编写一程序,接受一个字符串作为参数,并打印一张表。对于在字符串中出现的每个字符,该表给出该字符以及它的出现频率。

代码测试通过:

#include<stdio.h>
#define N 1000
int main()
{
    char a[N];
    int b[N]={0};
    int i,c[N]={0};
    printf("Input the string: ");
    gets(a);
    for(i=0;a[i]!='\0';i++)
    {
        b[a[i]]++;
    }
    for(i=0;a[i]!=0;i++)
    {
        if(c[a[i]] == 0)
        {
           printf("%c : %d\n",a[i],b[a[i]]);
           c[a[i]]=1;
        }
    }
    printf("\n");
    return 0;
}

posted @ 2012-08-18 11:43 hoshelly 阅读(195) | 评论 (0)编辑 收藏

找出单词在字符串中的位置

代码:
#include<stdio.h> 
#define N 1000
#define M 100
int main()
{
    char a[N], b[M];
    int i,j,n;
    printf("Input the string: ");
    gets(a);
    printf("Input the word: ");
    gets(b);
    for(i=0;a[i]!='\0';i++)
    {
        for(j=0;b[j]!='\0';j++)
        {
            if(a[i+j]!=b[j]) break;
        }
        if(b[j] == '\0')
            printf("%d ",i);
        
    }
    printf("\n");
    return 0;
}

posted @ 2012-08-18 11:15 hoshelly 阅读(114) | 评论 (0)编辑 收藏

仅列出标题
共12页: 1 2 3 4 5 6 7 8 9 Last