• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天之道

            享受編程的樂趣。
            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 閱讀(210) | 評論 (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 閱讀(248) | 評論 (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 閱讀(413) | 評論 (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 閱讀(210) | 評論 (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 閱讀(187) | 評論 (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 閱讀(250) | 評論 (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 閱讀(228) | 評論 (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 閱讀(940) | 評論 (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 閱讀(211) | 評論 (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 閱讀(128) | 評論 (0)編輯 收藏

            僅列出標題
            共12頁: 1 2 3 4 5 6 7 8 9 Last 
            国产精品免费福利久久| 久久久久人妻一区精品色| 91超碰碰碰碰久久久久久综合| 人人狠狠综合久久亚洲婷婷| 国产免费久久精品丫丫| 久久精品国产男包| www久久久天天com| 亚洲欧洲精品成人久久奇米网| 久久久无码精品亚洲日韩蜜臀浪潮| 久久国产精品99久久久久久老狼| 久久性生大片免费观看性| 久久亚洲精品无码AV红樱桃| 999久久久国产精品| 欧洲成人午夜精品无码区久久| 精品无码久久久久久久动漫| 久久久久亚洲AV无码网站| 久久青青草原精品影院| 亚洲国产精品无码久久久不卡| 亚洲伊人久久大香线蕉苏妲己 | 久久久精品人妻一区二区三区蜜桃| 99久久超碰中文字幕伊人| 欧美日韩精品久久免费| 99久久国产综合精品成人影院| 久久久久久亚洲AV无码专区| 99久久综合国产精品免费| 国产激情久久久久影院小草 | 久久免费视频观看| 久久久久人妻一区精品色| 亚洲精品无码久久久影院相关影片 | 亚洲伊人久久综合影院| 国产ww久久久久久久久久| 久久久久国产一级毛片高清版| 亚洲AV无码1区2区久久| 中文国产成人精品久久不卡| 国产精品乱码久久久久久软件| 青草久久久国产线免观| 99久久伊人精品综合观看| 国产精品99久久久久久宅男| 88久久精品无码一区二区毛片 | 久久最近最新中文字幕大全| 精品一区二区久久|