数组习题

//
//  main.m
//  练习
//
//  Created by zhangxueming on 14/12/26.
//  Copyright (c) 2014年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
//
//  main.c
//  字符串练习
//
//  Created by zhangxueming on 14/12/24.
//  Copyright (c) 2014年 zhangxueming. All rights reserved.
//

#include <stdio.h>
//*****************************/
//                            */
//          数组习题           */
//*****************************/

#include <stdio.h>

//1.给定某个字符数组,统计数组中所有英文字符的个数,比如“123fdd”中有 3 个。
//int main(int argc, const char * argv[]) {
//    char str[100]={};
//    int cnt=0;
//    int len=0;//统计字母的个数
//    for (int i=0; i<100; i++) {
//        str[i] = getchar();
//        if (str[i]=='
') {
//            str[i]='';//让字符数组中的字符以''结束
//            break;
//        }
//        cnt++;
//    }
//    for (int i=0; i<cnt; i++) {
//        if ((str[i]>='A' && str[i]<='Z') || (str[i]>='a'&& str[i]<='z')) {
//            len++;
//        }
//    }
//    printf("len = %d
" , len);
//    return 0;
//}

//3.给定某个拥有 5 个元素的字符数组,数组的成员都有阿拉伯字符构成,试着将该数
//组转换成一个整数,比如字符数组的内容是:{‘1’,’2’,’3’,’3’,’2’} 则将被转换成 12332。
//#include <stdlib.h>
//#include <ctype.h>
//
//int main(int argc,const char *argv[])
//{
//    //atoi("123")//把字符串转换成整型数据"123"  ==> 123
//    //printf("%d
", atoi("-12345adc"));
//    //printf("%d 
",digittoint('9'));//把0~f --> 0 ~ 15
//    char ch[5]={};
//    int num=0;
//    for (int i=0; i<5; i++) {
//        ch[i]=getchar();
//        ch[i]=ch[i]-48;
//        num=num*10+ch[i];
//    }
//    printf("num = %d
", num);
//    return 0;
//}

//5.给定一个完全由英文字符构成的数组,将数组中下标为偶数的字符都转换为大写(如
//果原来是大写则不变)。

//int main(int argc,const char *argv[])
//{
//    char ch[100]={};
//    int cnt =0;
//    for(int i=0; i<100; i++){
//        ch[i]=getchar();
//        if(ch[i]=='
')
//        {
//            ch[i]='';
//            break;
//        }
//        cnt++;
//    }
//
//    for (int i=0; i<cnt; i++) {
//        if (!(i%2) && (ch[i]>='a' && ch[i]<='z')) {
//            ch[i]-=32;
//        }
//        printf("%c", ch[i]);
//    }
//    printf("
");
//    return 0;
//}

//6.给一个完全由英文字符构成的字符数组加密,加密原则如下,除了字符‘Z’和‘z’
//之外,每个字符变成 ASCII 码值比它大 1 的字符,也就是‘A’变成‘B’。‘Z’或
//者‘z’转化为‘A’或者‘a’。

//int main(int argc, const char *argv[])
//{
//    char ch[100]={};
//    int cnt=0;
//    for (int i=0; i<100; i++) {
//        ch[i]=getchar();
//        if (ch[i]=='
') {
//            ch[i]='';
//            break;
//        }
//        cnt++;
//    }
//
//    for (int i=0; i<cnt; i++) {
//        if ((ch[i]>='A' && ch[i]<'Z')||(ch[i]>='a'&&ch[i]<'z')) {
//            ch[i]+=1;
//        }
//        else if(ch[i]=='Z')
//        {
//            ch[i] ='A';
//        }
//        else if(ch[i]=='z')
//        {
//            ch[i]='a';
//        }
//
//        printf("%c", ch[i]);
//    }
//    printf("
");
//    return 0;
//}

//7.计算某个由英文、数字以及标点符号构成的数组的总宽度,其中英文字符的宽度为
//1cm,数字宽度为 0.5cm、标点符号宽度为 0.8cm。

float lengthChar(char ch)
{
    if ((ch>='A'&&ch<='Z')  || (ch>='a'&&ch<='z')) {
        return 1.0;
    }
    else if(ch>='0'&&ch<='9')
    {
        return 0.5;
    }
    else
    {
        return 0.8;
    }
}

//int main(int argc,const char *argv[])
//{
//    char ch[100]={};
//    int cnt=0;
//    float len=0;
//    for ( int i=0; i<100; i++) {
//        ch[i]=getchar();
//        if (ch[i]=='
') {
//            ch[i]='';
//            break;
//        }
//        cnt++;
//    }
//    for (int i=0; i<cnt; i++) {
//        len+=lengthChar(ch[i]);
//    }
//    printf("len = %.2f
", len);
//
//    return 0;
//}

//8.接上题,如果规定行的宽度为 10cm,将某个字符长度超过 50 的字符串截断,恰好 使 10cm 宽的行能容纳。输出这个被截断的子数组。
//1234asd!@#$rtyukl;fghjk
//int main(int argc,const char *argv[])
//{
//    char ch[100]={};
//    char str[100]={};
//    int cnt=0;
//    float len=0;
//    for ( int i=0; i<100; i++) {
//        ch[i]=getchar();
//        if (ch[i]=='
') {
//            ch[i]='';
//            break;
//        }
//        cnt++;
//    }
//
//    for (int i=0; i<cnt; i++) {
//        len+=lengthChar(ch[i]);
//        if (len>10) {
//
//            break;
//        }
//        str[i]=ch[i];
//        printf("%c", str[i]);
//    }
//    printf("
");
//    return 0;
//}

//9.给定某个整型数组,计算该数组所有偶数的和。
//10.给某个整型数组赋值,赋值规律如下,下标能被 3 整除的都赋值为 1,能被 5 整除
//的都赋值为 2,能被 7 整除的都赋值为 3,能被 3、5、7 任意两个或者 3 个都能整除
//的数赋值为 8,其余都赋值为 0.

//int main(int argc,const char *argv[])
//{
//    int a[100];
//    for (int i=0; i<10; i++) {
//        if (i%3==0) {
//            a[i]=1;
//            if (i%5==0||i%7==0) {
//                a[i]=8;
//            }
//        }
//        else if(i%5==0)
//        {
//            a[i]=2;
//            if (i%7==0) {
//                a[i]=8;
//            }
//        }
//        else if(i%7==0)
//        {
//            a[i]=3;
//        }
//        else
//        {
//            a[i]=0;
//        }
//        printf("%d " ,a[i]);
//    }
//    printf("
");
//    return 0;
//}

//11.通过终端输入 10 个整数并将其保存在一个整型数组中,数字保存在数组中的顺序与
//下标正好相反,也就是第一个被输入的数放在数组最后一个元素中,最后一个输入
//的数字放到第一个元素中。
#include <stdio.h>

//int main(int argc,const char *argv[])
//{
//    int a[10];
//    for (int i=10-1; i>=0; i--) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=0; i<10; i++) {
//        printf("%d ", a[i]);
//    }
//    printf("
");
//    return 0;
//}

//13.给定一个 5 个元素构成的整型数组,每个元素的值都在 0-9 之间,按照位置将其组
//成一个 5 位数并输出,例如 int a[5] = {1,2,2,3,7};则输出 73221。

//int main(int argc, const char *argv[])
//{
//    int a[5];
//    int num=0;
//    for (int i=0; i<5; i++) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=4; i>=0; i--) {
//        num = num*10+a[i];
//    }
//    printf("%d
", num);
//    return 0;
//}

//17.给定一个有 10 个整形数的元素,将前 5 个元素跟后 5 个元素做整体交换,比如
//{1,1,1,1,1,2,3,2,2,2}->{2,3,2,2,2,1,1,1,1,1}。
//int main(int argc, const char *argv[])
//{
//    int a[10];
//    for (int i=0; i<10; i++) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=0; i<5; i++) {
//        int temp = a[i];
//        a[i]=a[i+5];
//        a[i+5]=temp;
//    }
//    for (int i=0; i<10; i++) {
//        printf("%d ", a[i]);
//    }
//    return 0;
//}

//18.判断一个整型数组是否是对称数组,例如{1,2,3,3,2,1}和{1,6,8,1,8,6,1}都是对称数组。

//int main(int argc,const char *argv[])
//{
//    int a[100]={};
//    int cnt = 0;
//    int flag =0;//标志变量
//
//    for (int i=0; i<100; i++) {
//        scanf("%d", &a[i]);
//        cnt++;
//        if (getchar()=='
') {
//            break;
//        }
//    }
//
//    for (int i=0; i<cnt/2; i++) {
//        if (a[i]!=a[cnt-i-1]) {
//            flag =1;
//        }
//    }
//
//    if (flag) {
//        printf("不是对称数组
");
//    }
//    else
//    {
//        printf("是对称数组
");
//    }
//    return 0 ;
//}

//int main(int argc,const char *argv[])
//{
//    int a[5];
//    int b[5];
//    for (int i=0; i<5; i++) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=0; i<5; i++) {
//        scanf("%d", &b[i]);
//    }
//    for (int i=0; i<5; i++) {
//        a[i]=b[i]=a[i]+b[i];
//        printf("%d ", a[i]);
//    }
//    printf("
");
//    return 0;
//}

//21.给定一个能容纳 10 个元素的整型数组,现有 9 个元素,现在第 5 个元素的位置插入
//一个数字 88,后面的数字顺序后移。

//int main(int argc, const char *argv[])
//{
//    int a[10]={};
//    for (int i=0; i<9; i++) {
//        scanf("%d", &a[i]);
//    }
//
//    for (int j=8; j>=4; j--) {
//        a[j+1]=a[j];
//    }
//    a[4]=88;
//    for (int i=0; i<10; i++) {
//        printf("%d ", a[i]);
//    }
//    return 0;
//}

//22.给定一个 10 个元素的整型数组,现在将第 2 个元素删除,后面的数组顺序前移。
//int main(int argc,const char *argv[])
//{
//    int a[10]={};
//    for (int i=0; i<10; i++) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=1; i<9; i++) {
//        a[i]=a[i+1];
//    }
//    for (int i=0; i<9; i++) {
//        printf("%d ",a[i]);
//    }
//    return 0;
//}

//25.给定一个整型数组,求该数组中第二大的数的下标。
//int main(int argc,const char *argv[])
//{
//    int a[20];
//    int cnt=0;
//    int max;
//    for (int i =0; i<20; i++) {
//        scanf("%d", &a[i]);
//        cnt++;
//        if (getchar()=='
') {
//            break;
//        }
//    }
//    max=a[0];
//    for (int i=1; i<cnt; i++) {
//        if (a[i]>max) {
//            max = a[i];
//        }
//    }
//    int k=-1;//
//    int temp=0;
//    for (int i=0; i<cnt; i++) {//找到第一个不等于max的值赋值给temp
//        if (a[i]==max) {
//            continue;
//        }
//        temp = a[i];
//        k=i;
//        break;
//    }
//    if (k==-1) {//判断是否有第二大的值
//        return -1;
//    }
//
//    for (int i=0; i<cnt; i++) {//找第二大的值,记录下标
//        if (max==a[i]) {
//            continue;
//        }
//        if (temp<a[i]) {
//            temp = a[i];
//            k=i;
//        }
//    }
//    printf("k =%d
", k);
//    return 0;
//}

//30.给定一个英文句子,单词之间用 1 个空格分开,求出第 2 个单词的偏移位置。例如
//“Professor du comes from Korea”的偏移位置是 10。

//int main(int argc,const char *argv[])
//{
//    char str[100];
//    int cnt=0;
//    int index=0;
//    for (int i=0; i<100; i++) {
//        str[i]=getchar();
//        if (str[i]=='
') {
//            str[i]='';
//            break;
//        }
//        cnt++;
//    }
//    for (int i=0; i<cnt; i++) {
//        if (str[i]==' ') {
//            index = i;
//            break;
//        }
//    }
//    printf("index = %d
", index+1);
//    return 0 ;
//}

//32.给定两个字符数组,将这两个拼接起来放在第一个数组中(假定第一个数组足够长),
//比如“abc”和“123”构成“abc123”。
#include <string.h>

//int main(int argc,const char *argv[])
//{
//    char str1[100]={};
//    char str2[50]={};
//    scanf("%s%s",str1,str2);
//    int len1= (int)strlen(str1);//求字符串有效字符个数"qianfeng"
//    int len2= (int)strlen(str2);
//
//    for (int i=len1; i<len1+len2; i++) {
//        str1[i]=str2[i-len1];
//    }
//    printf("%s
", str1);
//    return 0 ;
//}

//34.给定一个整型数组,数组的长度为 N(N>3),从数组中寻找一个连续的长度为 3 的
//子数组,要求该子数组的和最大。
//12345
//

//int main(int argc,const char *argv[])
//{
//    int a[100];
//    int cnt = 0;
//    int max;
//    for (int i=0; i<100; i++) {
//        scanf("%d", &a[i]);
//        cnt++;
//        if (getchar()=='
') {
//            break;
//        }
//    }
//    if (cnt<=3) {
//        return -1;
//    }
//    max = a[0]+a[1]+a[2];
//    int k=0;
//    for (int i=1; i<cnt-2; i++) {
//        if (max<a[i]+a[i+1]+a[i+2]) {
//            max=a[i]+a[i+1]+a[i+2];
//            k=i;
//        }
//    }
//    printf("k = %d
", k);
//
//    return 0 ;
//}

//35.给定两个长度一样的整型数组,判断两个数组是否相同,相同的原则是数组中的每 一个相互对应的元素的“和值”相同,“和值”是指元素对应的整数所有位的合,例 如:a[0]的值是 1112,b[0]的值是 23,则这两个元素“相同”。

int sumBit(int num)
{
    int sum=0;
    while (num) {
        sum+=num%10;
        num/=10;
    }
    return sum;
}

//int main(int argc, const char *argv[])
//{
//    int a[5];
//    int b[5];
//    for (int i=0; i<5; i++) {
//        scanf("%d", &a[i]);
//    }
//    for (int i=0; i<5; i++) {
//        scanf("%d", &b[i]);
//    }
//
//    for (int i=0; i<5; i++) {
//        if (sumBit(a[i])!=sumBit(b[i])) {
//            printf("两个数组不相等
");
//            return -1;
//        }
//    }
//    printf("两个数组相等
");
//    return 0;
//}

//36.给定两个字符数组,比较这两个字符数组的大小,比较的原则是字符数组中所有字 符的 ASCII 值相加后的和值,和值越大则字符数组越大。

//int main(int argc,const char *argv[])
//{
//    char str1[100];
//    char str2[100];
//    scanf("%s%s",str1,str2);
//    int sum1=0,sum2=0;
//    int len1 = (int)strlen(str1);
//    int len2 = (int)strlen(str2);
//    for (int i=0; i<len1; i++) {
//        sum1+=str1[i];
//    }
//    for (int i=0; i<len2; i++) {
//        sum2+=str2[i];
//    }
//    if (sum1 < sum2) {
//        printf("str1 < str2
");
//    }
//    else if(sum1 == sum2)
//    {
//        printf("str1 == str2
");
//    }
//    else
//    {
//        printf("str1 > str2
");
//    }
//
//    return 0;
//}


//输入一串小写字母(以"."为结束标志),统计出每个字母在该字符串中出现的次数(若某字母不出现,则不要输出)。
//int main(int argc,const char *argv[])
//{
//    //aaabbabbccccc
//    //4004000050000
//    //
//    char str[100];
//    int cnt[100];
//    int len=0;
//    for (int i=0; i<100; i++) {
//        str[i]=getchar();
//        if (str[i]=='.') {
//            str[i]='';
//            break;
//        }
//        len++;
//        cnt[i]=1;
//    }
//    for (int i=0; i<len; i++) {
//        if (cnt[i]!=0) {
//            for (int j=i+1; j<len; j++) {
//                if (cnt[j]!=0 && str[i]==str[j]) {
//                    cnt[i]++;
//                    cnt[j]=0;
//                }
//            }
//        }
//    }
////    for (int i=0; i<len; i++) {
////        printf("%d ", cnt[i]);
////    }
//    for (int i=0; i<len; i++) {
//        if (cnt[i]) {
//            printf("char = %c cnt = %d
", str[i],cnt[i]);
//        }
//    }
//
//    return 0;
//}

//41.编写函数将一个n*n矩阵转置,例如:(****)
//1 2 3 4     1 5 3 4
//5 6 7 8 ->  2 6 2 7
//3 2 5 9     3 7 5 2
//4 7 2 3     4 8 9 3
//*/

//int main(int argc,const char *argv[]){
//    int a[4][4]={{1,2,3,4},
//                 {5,6,7,8},
//                 {3,2,5,9},
//                 {4,7,2,3}};
//
//    for (int i=0; i<4; i++) {
//        for (int j=i; j<4; j++) {
//            int temp=a[i][j];
//            a[i][j]=a[j][i];
//            a[j][i]=temp;
//        }
//    }
//    for (int i=0; i<4; i++) {
//        for (int j=0; j<4; j++) {
//            printf("%4d",a[i][j]);
//        }
//        printf("
");
//    }
//    return 0;
//}

//3.输入十个数,任意相邻的两个数不同,输出所有的递增,递减序列
//比如:
//输入:1 5 9 8 12 21 3 0 -1 9
//输出:
//1 5 9
//9 8
//8 12 21
//21 3 0 -1
//-1 9
//
//输入: 3 9 8 -11 4 21 8 -3 0 2
//输出:
//3 9
//9 8 -11
//-11 4 21
//21 8 -3
//-3 0 2

//int main(int argc, const char *argv[])
//{
//    int a[10];
//    for (int i=0; i<10; i++) {
//        scanf("%d", &a[i]);
//    }
//
//    printf("%d ", a[0]);
//    for (int i=1; i<=8; i++) {
//        printf("%d ",a[i]);
//        if ((a[i]>a[i-1] && a[i]>a[i+1]) || (a[i]<a[i-1] && a[i]<a[i+1])) {
//            printf("
%d ",a[i]);
//        }
//    }
//    printf("%d
",a[9]);
//
//    return 0;
//}

//1. 有m个人围成一圈,开始报数,报道n,退出,问最后剩下的是几号。(从1号开始)
//比如:
//输入:5 3
//输出:
//4
//
//输入: 6 2
//输出:
//5

//0 0 0 4 0
//

//int main(int argc, const char *argv[])
//{
//    int a[100];
//    int m, n;
//    int cnt=0;//统计出局人数
//    int k=0;
//    int i;
//    scanf("%d%d", &m,&n);
//    if (m>100) {
//        return -1;
//    }
//    for (i=0; i<m; i++) {
//        a[i]=i+1;
//    }
//    i=0;
//    while (cnt<m-1) {
//        if (a[i]) {//计数
//            k++;
//        }
//        if (k==n) {//统计出局人数
//            k=0;
//            a[i]=0;
//            cnt++;
//        }
//        i++;
//        if (i==m) {//处理边界
//            i=0;
//        }
//    }
//    i=0;
//    while (!a[i++]);
//    
//    printf("%d
", a[i-1]);
//    
//    return 0;
//}


//2. 输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限
//比如:
//输入:5 18
//输出:p q
//1  2  3  4  5
//16 17 18 0  6
//15 0  0  0  7
//14 0  0  0  8
//13 12 11 10 9
//
//输入: 4 12
//输出:
//1  2  3  4
//12 0  0  5
//11 0  0  6
//10 9  8  7

//#define RIGHT   1
//#define DOWN    2
//#define LEFT    3
//#define UP      4
//
//int main(int argc, const char * argv[]) {
//    int a[20][20]={};
//    int n, m;
//    scanf("%d%d", &n,&m);
//    if (n<1||m<1||m>n*n||n>20) {
//        return -1;
//    }
//    int type=RIGHT;//1:从左往右赋值 2:从上往下赋值 3:从右往左赋值4:从下往上赋值
//    int p=0,q=0;
//    int s1=n-1;
//    int s2=n-1;
//    int s3=0;
//    int s4=1;
//    int i=1;
//    while (i<=m) {
//        a[p][q]=i;
//        switch (type) {
//            case RIGHT:
//                if (++q==s1) {//判断是否到边界
//                    type = DOWN;
//                    s1--;
//                }
//                break;
//            case DOWN:
//                if (++p==s2) {
//                    type = LEFT;
//                    s2--;
//                }
//                break;
//            case LEFT:
//                if (--q==s3) {
//                    type = UP;
//                    s3++;
//                }
//                break;
//            case UP:
//                if (--p==s4) {
//                    type=RIGHT;
//                    s4++;
//                }
//                break;
//            default:
//                break;
//        }
//        i++;
//    }
//
//    for (int i=0; i<n; i++) {
//        for (int j=0; j<n; j++) {
//            printf("%4d", a[i][j]);
//        }
//        printf("
");
//    }
//    return 0;
//}


//4..输入10个数,找出出现次数最多的数 (如果多个并列,则按数字出现顺序分别输出)
//比如:
//输入:1 2 2 3 4 5 6 7 8 9
//输出:2
//数据结构: 定义一个整型数组 int count[10] 存储每个元素的个数, 开始时每个元素个数初始化为1
//
//算法:
//(1) 利用双层循环, 每一个元素都与后面一个元素比较, 如果两者相同, 则该元素次数+1,
//(2) 以上的算法中两个相同的元素的次数是一样的, 优化如下, 比较两个元素的时候, 如果两者相等, 则该元素次数+1, 后面的元素次数设置为0, 比较的时候再判断这个元素是否已经比较
//
//伪代码:
//
//定义数组保存每个元素次数
//
//使用双层循环遍历数组
//如果当前元素后后面元素相等, 并且后面的元素次数不为0
//当前元素次数+1, 后面的元素次数设置为0
//
//从保存每个元素次数的数组中查找最大的值

//a[10]
//1 2 2 3 4 3 4 5 8 5
//count[10]
//1 2 0 4 2 0 0 0 1 0
//
//int main(int argc,const char *argv[])
//{
//    int a[10];
//    int count[10];
//    for (int i=0; i<10; i++) {
//        scanf("%d", &a[i]);
//        count[i]=1;
//    }
//
//    for (int i=0; i<10; i++) {
//        if (count[i]) {
//            for (int j=i+1; j<10; j++) {
//                if (a[j]==a[i]) {
//                    count[i]++;
//                    count[j]=0;
//                }
//            }
//        }
//    }
////    for (int i=0; i<10; i++) {
////        printf("%d ", count[i]);
////    }
//    //找最大值
//    int max = count[0];
//    for (int i=0; i<10; i++) {
//        if (max<count[i]) {
//            max=count[i];
//        }
//    }
//    //查找cnt最大值对应的数
//    for (int i=0; i<10; i++) {
//        if (max==count[i]) {
//            printf("%d ", a[i]);
//        }
//    }
//    printf("
");
//    return  0;
//}

//5.魔方阵是一个古老的智力问题,它要求在一个m*m的矩阵中填入1~m^2的数字(m为奇数),使得每一行、每一列、每条对角线的累加和都相等。请编程实现输入m,输出m*m的魔方阵出来。
//比如:
//输入:3
//输出:
//6 1 8   上一个数i=0 下一个数i=n-1  j--
//7 5 3   上一个数j=n-1 下一个数j=0  i--
//2 9 4   i++;
//(1)将1放在第一行中间一列;
//(2)从2开始直到n×n止各数依次按下列规则存放;每一个数存放的行比前一个数的行数减1,列数加1/减1(例如上面的三阶魔方阵,5在4的上一行后一列);
//(3)如果上一个数的行数为1,则下一个数的行数为n(指最下一行);例如1在第一行,则2应放在最下一行,列数同样加1;
//(4)当上一个数的列数为1时,下一个数的列数应为n,行数减去1。例如2在第3行最后一列,则3应放在第二行第一列;
//(5)如果按上面规则确定的位置上已有数,或上一个数是第一行第n列时,则把下一个数放在上一个数的下面。例如按上面的规定,4应该放在第1行第2列,但该位置已经被占据,所以4就放在3的下面;

//int main(int argc,const char *argv[])
//{
//    int a[19][19]={};
//    int n;
//    scanf("%d", &n);
//    if (!(n%2) || n>19 || n<3) {
//        return -1;
//    }
//    int k=1;
//    int i=0,j=n/2;
//    int tempi,tempj;//保存上一次的位置
//    while (k<=n*n) {
//        a[i][j]=k;
//        tempi =i;
//        tempj =j;
//        i--;
//        j--;
//        if (tempi==0) {
//            i=n-1;
//        }
//        if (tempj==0) {
//            j=n-1;
//        }
//        if (a[i][j]) {
//            i=tempi+1;
//            j=tempj;
//        }
//        k++;
//    }
//
//    for (i=0; i<n; i++) {
//        for (j=0; j<n; j++) {
//            printf("%4d", a[i][j]);
//        }
//        printf("
");
//    }
//    return 0;
//}

//随机数问题
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

/*int main(int argc,const char *argv[])
{
    //void     srand(unsigned);
    //int     rand(void);
    while (1) {
        //
        //time_t time(time_t *);
        //time(NULL)
        //srand((unsigned)time(NULL));//利用系统时间产生随机数种子
        int num = rand()%101;
        printf("num = %d
" , num);
        sleep(1);
        //usleep(1000);
        
    }
    //    int a[100];
    //    srand((unsigned)time(NULL));
    //    for (int i=0; i<50; i++) {
    //        a[i]=rand()%100;
    //    }
    //    for (int i=0; i<50; i++) {
    //        printf("%d ", a[i]);
    //    }
    return 0;
}
*/


//atoi函数的实现

//实现一个函数,传递一个字符数组,返回字符数组中的整数值,并在main函数中输出。
//例如:
//输入:134a231b
//输出:134
//输入:-129cde
//输出:-129
/*#include <stdlib.h>
 
 int myatoi(char *src)
 {
 char *pstr=src;
 int num=0;
 int flag=1;
 while (*pstr==' ') {
 pstr++;
 }
 if (*pstr=='+') {
 pstr++;
 }
 else if (*pstr=='-') {
 flag=-flag;
 pstr++;
 }
 while (*pstr>='0' && *pstr<='9') {
 num =num*10+(*pstr-48);
 pstr++;
 }
 num*=flag;
 return num;
 }
 
 
 int main(int argc, const char * argv[]) {
 
 printf("%d
", myatoi("123abe"));
 
 return 0;
 }*/


//实现一个函数,传递一个有10个整形元素的数组,将数组中的0全部移动到数组末尾,将非0的值移至开始(保持原来顺序不变),并在main函数中将转换的结果输出。
//例如:
//输入: 5 9 -1 0 2 8 0 3 7 0
//5 9 -1 2 8 0 3 7 4 0

//输出: 5 9 -1 2 8 3 7 4 0 0

//void  func(int *arr, int len)
//{
//    //int *p = arr;
//    int cnt = 0;//统计0的个数
//    for (int i=0; i<len-cnt-1; i++) {
//        if (*(arr+i)==0) {
//            for (int j=i; j<len-cnt-1; j++) {
//                *(arr+j) = *(arr+j+1);
//            }
//            *(arr+len-cnt-1)=0;
//            cnt++;
//            i--;
//        }
//    }
//}
//void  func(int *arr, int len)
//{
//    int *p = arr;
//    int cnt = 0;//统计0的个数
//    for (int i=0; i<len-cnt-1; i++) {
//        if (!(*p)) {
//            for (int j=0; j<len-cnt-1-i; j++) {
//                *(p+j) = *(p+j+1);
//            }
//            *(p+len-cnt-i-1)=0;
//            cnt++;
//            p--;
//            i--;
//        }
//        p++;
//    }
//}
//
//int main(int argc,const char *argv[])
//{
//    int a[10]={5,0,-1,0,2,8,0,3,7,0};
//    func(a, 10);
//    for (int i=0; i<10; i++) {
//        printf("%d ", a[i]);
//    }
//    printf("
");
//    return 0;
//}




//*****************************************/
//              字符串练习
//*****************************************/
//注:字符串的习题中有的地方用了 sscanf 跟sprintf 这两个函数,大纲中没有要求这两个函数,但是希望大家可以通过看官方文档自己把这两个函数学习下


#include <string.h>
//实现一个函数,传递两个字符数组,输出第二个字符数组在第一个字符数组中出现的次数。
//比如:
//输入:
//abcdfewabcssaba
//abc
//输出:2
//int main(int argc, const char *argv[])
//{
//    char *str1="abcdfewabcssaba";
//    char *str2="abc";
//    int cnt = 0;
//    char *pstr=str1;
//
//    while ((pstr = strstr(pstr, str2))) {
//        cnt++;
//        pstr+=strlen(str2);
//    }
//    printf("cnt = %d
", cnt);
//
//    return 0;
//}


//查找一个字符串2在字符串1中第一次出现的位置
//比如:
//输入
//asdfwd
//df
//输出为
//3
//
//输入
//hhff
//hf
//输出为
//2
#include <stdlib.h>
#include <string.h>
/*
 int main(int argc, const char * argv[]) {
 char *str = (char *)malloc(100*sizeof(char));
 char *substr = (char *)malloc(100*sizeof(char));
 if (!str || !substr) {
 return -1;
 }
 scanf("%s%s", str,substr);
 int index=0;
 
 char *pstr = strstr(str,substr);
 if (pstr) {
 index = (int)(pstr-str+1);
 }
 printf("%d
", index);
 
 return 0;
 }*/

//int main(int argc,const char *argv[])
//{
//    char *str = (char *)malloc(100*sizeof(char));
//    if (!str) {
//        return -1;
//    }
//
//    //方法一
////    gets(str);
////    printf("%s
", str);
//    //方法二
//    //scanf("%[^
]",str);
//    scanf("%[A-Z,a-z,0-9]",str);//必须是A-Z之间
//    printf("%s
", str);
//    return 0;
//}

//int main(int argc,const char *argv[])
//{
////    char str[100]={};
////    scanf("%s", str);
////    char ch;
////    //fflush(stdin);
////    getchar();
////    scanf("%c", &ch);
////    if (ch =='
') {
////        printf("hello world
");
////    }
////    printf("%s %c
", str,ch);
//
//    char ch1 ;
//    ch1 = getchar();
//    printf("%c
", ch1);
//
//    return 0;
//}

//查找一个字符串2在字符串1中所有出现的位置
//比如:
//输入
//asdfdfdfwd
//df
//输出为
//3 5 7
//
//输入
//hhfhf
//hf
//输出为
//2
//4

//int searchStringIndex(char *src, char *substr, int *index)
//{
//    int count = 0;
//    int len = (int)strlen(substr);
//    char *pstr = src;
//    while ((pstr = strstr(pstr, substr))) {
//        index[count++] = (int)(pstr-src)+1;
//        pstr+=len;
//    }
//    return count;
//}
//int main(int argc,const char *argv[])
//{
//    char *str = malloc(100*sizeof(char));
//    char *substr = malloc(100*sizeof(char));
//    int index[100]={};
//    if (!str || !substr) {
//        exit(-1);
//    }
//    scanf("%s %s", str,substr);
//
//    int cnt = searchStringIndex(str,substr,index);
//
//    for (int i=0; i<cnt; i++) {
//        printf("%d ", index[i]);
//    }
//    printf("
");
//
//    return 0;
//}

//int main(int argc,const char *argv[])
//{
//    int len = (int)strlen(NULL);//不能传入NULL
//    printf("len = %d
", len);
//    return 0;
//}

//字符串原地压缩。题目描述:“eeeeeaaaff" 压缩为 "e5a3f2"。
//字符串压缩算法,把s字符串压缩处理后结果保存在res中
//
//比如:
//输入
//aaaaeefggg
//输出为
//a4e2f1g3
//
//输入
//hhfhhhhff
//输出为
//h2f1h4h2

//char * compress(char *src, char *buf)
//{
//    char *pstr = src;
//    int cnt = 1;
//    char *pbuf = buf;
//    while (*pstr) {
//        if (*pstr == *(pstr+1)) {
//            cnt++;
//        }
//        else
//        {
//            pbuf += sprintf(pbuf,"%c%d", *pstr,cnt);
//            cnt = 1;
//        }
//        pstr++;
//    }
//    return buf;
//}
//
//int main(int argc,const char *argv[])
//{
//    char str[100]={};
//    char buf[200]={};
//    scanf("%s",str);
//
//    printf("%s
", compress(str, buf));
//    return 0;
//}

//int main(int argc,const char *argv[])
//{
//    int a=100;
//    char *str = "hello world";
//    float f = 3.14;
//
//    char buf[100]={};
//    int len = sprintf(buf, "%f",f);
//    printf("%s
", buf);
//    printf("len = %d
", len);
//
//    return 0;
//}

//字符串原地压缩。题目描述:"e5a3f2" 解压缩为 “eeeeeaaaff"。
//字符串压缩算法,把s字符串压缩处理后结果保存在res中
//
//比如:
//输入
//a14e2f1g3
//输出为
//aaaaeefggg
//
//输入
//h2f1h4h2
//输出为
//hhfhhhhff
//#include <ctype.h>
//
//int main(int argc,const char *argv[])
//{
//    char str[100]={};
//    char buf[1000]={};
//
//    char ch;
//    int cnt=0;
//    scanf("%s", str);
//
//    char *pbuf = buf;
//    char *pstr = str;
//    while (*pstr) {
//        sscanf(pstr,"%c%d",&ch,&cnt);
//        memset(pbuf, ch, cnt);
//        pbuf+=cnt;
//        pstr++;
//        while (isdigit(*pstr)) {
//            pstr++;
//        }
//    }
//    printf("%s
",buf);
//
//    return 0;
//}

//求一个字符串s的最大连续递增数字子串。
//
//
//比如:
//输入
//f123fffwf3256789:abcd
//输出为
//123
//
//输入
//abcd345bbw1357f123
//输出为
//123

//#include <ctype.h>
//
//int main(int argc,const char *argv[])
//{
//    char str[100]={};
//    scanf("%s", str);
//    char *pstr = str;
//    int max=0;
//    char *pmax = NULL;
//    int cnt=0;
//    while (*pstr) {
//        if (isdigit(*pstr)) {
//            cnt = 1;
//            while ((*pstr == *(pstr+1)-1) && (*pstr!='9')) {
//                cnt++;
//                pstr++;
//            }
//            pstr++;
//            if (max < cnt) {
//                max = cnt;
//                cnt = 0;
//                pmax = pstr-max;
//            }
//        }
//        else
//        {
//            pstr++;
//        }
//    }
//    if (pmax) {
//        *(pmax+max)='';
//        printf("%s
", pmax);
//    }
//    return 0;
//}


//字符串排序。比较三个字符串的大小,然后按从小到大的顺序将字符串输出。
//比如:
//输入
//asdfwd
//ddrwf
//ffweff
//输出为
//asdfwd
//ddrwf
//ffweff
//
//输入
//sgfgeasdfw
//aabbe
//wrwwdfaf
//输出为
//aabbe
//sgfgeasdfw
//wrwwdfaf

/*
 int main(int argc, const char *argv[])
 {
 char *pstr[20]={NULL};
 int cnt = 0;
 for (int i=0; i<20; i++) {
 pstr[i] = calloc(100, sizeof(char));
 if (pstr[i]) {
 scanf("%s", pstr[i]);
 cnt++;
 }
 if (getchar() == '
') {
 break;
 }
 }
 //排序
 for (int i=0; i<cnt-1; i++) {
 for (int j=0; j<cnt-i-1; j++) {
 if (strcmp(pstr[j], pstr[j+1])>0) {
 char *ptemp = pstr[j];
 pstr[j]= pstr[j+1];
 pstr[j+1] = ptemp;
 }
 }
 }
 for (int i=0; i<cnt; i++) {
 printf("%s
", pstr[i]);
 free(pstr[i]);
 pstr[i]=NULL;
 }
 return 0;
 }
 */

//memmem()
//void    *memmem(const void *srcmem, size_t size1, const void *submem, size_t size2)
//srcmem: 源内存块的地址
//size1:源内存块的大小
//submem: 子内存块的首地址
//size2: 查找字节个数

void *my_memmem(const void *srcmem,size_t len1,const void *submem,size_t len2)
{
    char *psrc = (char *)srcmem;
    
    for (int i=0; i<len1; i++) {
        char *ptmp = psrc;
        char *psub = (char *)submem;
        int j=0;
        while (*psrc == *psub) {
            psrc++;
            psub++;
            j++;
            if (j==len2) {
                return ptmp;
            }
        }
        psrc = ptmp+1;
    }
    return NULL;
}

//int main(int argc,const char *argv[])
//{
//    char srcmem[100]="hello world qian feng";
//    char submem[20]="world";
//    printf("%s
", my_memmem(srcmem, 100, submem, 6));
//
//    return 0;
//}
//返回在两字符串中第一个相同的子串(大于1个字符),并将子串输出
//
//
//比如:
//输入
//adbAFEHHFS
//acwfagAFEf
//输出为
//AFE
//
//输入
//qfdgJKlin
//qdfgJKwfo
//输出为
//gJK

/*
 int main(int argc,const char *argv[])
 {
 char str1[100]={};
 char str2[100]={};
 scanf("%s%s", str1,str2);
 
 int len1 = (int)strlen(str1);
 int len2 = (int)strlen(str2);
 char *pstr = str2;
 char *ptmp=NULL;
 
 for (int i=0; i<len2-1; i++) {
 if ((ptmp = (char *)my_memmem(str1, len1,  pstr, 2))) {
 //printf("%s
",ptmp);
 int j=2;
 while (*(ptmp+j) == *(pstr+j)) {
 j++;
 }
 *(ptmp+j)='';
 printf("%s
",ptmp);
 break;
 }
 pstr++;
 }
 return 0;
 }*/


//泛型算法
//实现排序整型, 浮点 ,字符型
//从小到大
//冒泡排序

/*
 #include <math.h>
 
 int compare_int(void *a, void *b)
 {
 return *(int *)a-*(int *)b;
 }
 
 int compare_float(void *a, void *b)
 {
 
 if (fabs(*(float *)a-*(float *)b)<0.00005) {
 return 0;
 }
 else if(*(float *)a>*(float *)b)
 {
 return 1;
 }
 else
 {
 return -1;
 }
 }
 
 int compare_char(void *ch1, void *ch2)
 {
 return *(char *)ch1-*(char *)ch2;
 }
 
 //7 6 4 5 3
 //第一次
 //6 7 4 5 3
 //6 4 7 5 3
 //6 4 5 7 3
 //6 4 5 3 7
 
 //第二次
 //4 6 5 3 7
 //4 5 6 3 7
 //4 5 3 6 7
 
 void sort_data(int (*pfunc)(void *, void *), void *pdata,size_t len,size_t size)
 {
 char *p_i = (char *)pdata;
 for (int i=0; i<len-1; i++) {
 for (int j=0; j<len-i-1; j++) {
 if (pfunc(p_i+j*size, p_i+(j+1)*size) > 0)
 {
 char temp[size];
 memcpy(temp, p_i+j*size, size);
 memcpy(p_i+j*size, p_i+(j+1)*size, size);
 memcpy(p_i+(j+1)*size, temp, size);
 }
 }
 }
 }
 
 int main(int argc,const char *argv[])
 {
 //    int a[10]={7,6,4,5,3};
 //    sort_data(compare_int, a, 5, sizeof(int));
 //    for (int i=0; i<5; i++) {
 //        printf("%d ", a[i]);
 //    }
 
 //    float a[10]={7.8,6.7,4.3,5.2,3.0};
 //    sort_data(compare_float, a, 5, sizeof(float));
 //    for (int i=0; i<5; i++) {
 //        printf("%.2f ", a[i]);
 //    }
 char a[10]="hello";
 sort_data(compare_char, a, 5, sizeof(char));
 for (int i=0; i<5; i++) {
 printf("%c ", a[i]);
 }
 return 0;
 }*/


//输入一个字符串,然后返回连续最大的字符串
//
//比如demo
//1,3,3,3,4,4,4,4,4,0,0,0,4,4,4,4
//,此例中由5个连续的4为最大连续子串,返回结果为44444.
//1,3,3,3,4,4,4,4,4,0,0,0,5,5,5,5 返回44444;
//1,3,3,3,5,5,5,5,5,4,4,4,4,4,0,0,0,0,0,0,4,4,4,4},返回000000;

//int main(int argc, const char * argv[]) {
//    @autoreleasepool {
//        char str[100]={};
//        scanf("%s", str);
//        char *pstr = str;
//        while (*pstr) {
//            if (*pstr == ',') {
//                int len = (int)strlen(pstr+1);
//                memmove(pstr, pstr+1, len);
//                *(pstr+len)='';
//                pstr--;
//            }
//            pstr++;
//        }
//        //printf("%s
", str);
//        pstr = str;
//        int cnt = 1;
//        int max =0;
//        char *pmax = NULL;
//        while (*pstr) {
//            while(*pstr == *(pstr+1)) {
//                cnt++;
//                pstr++;
//            }
//            if (cnt>max) {
//                max = cnt;
//                pmax = pstr-cnt+1;
//            }
//            cnt = 1;
//            pstr++;
//        }
//        *(pmax+max) = '';
//        printf("%s
", pmax);
//    }
//    return 0;
//}
//从键盘输入个数不定的字符串(不超过10个),以最后一个字符串整体连接到每两个字符串之间,组成一个新的字符串,并且输出这个字符串;
//比如:
//输入:
//I am Optimus Prime!
//:
//输出为
//I:am:Optimus:Prime!
//输入:
//Hi welcome to qianfeng
//#@
//输出为
//Hi#@welcome#@to#@qianfeng
//输入
//abc def ghi jkl heihei haha mygod
//**
//输出为
//abc**def**ghi**jkl**heihei**haha**mygod
//int main(int argc,const char *argv[])
//{
//    char *pstr[10]={NULL};
//    char demi[10]={};
//    char *pbuf = (char *)malloc(1000*sizeof(char));
//    int cnt = 0;
//    for (int i=0; i<10; i++) {
//        pstr[i]=(char *)malloc(100*sizeof(char));
//        scanf("%s", pstr[i]);
//        cnt++;
//        if (getchar() =='
') {
//            break;
//        }
//    }
//    scanf("%s", demi);
//    char *p = pbuf;
//    int length =(int)strlen(demi);
//    for (int i=0; i<cnt; i++) {
//        strcat(p, pstr[i]);
//        p+=(int)strlen(pstr[i]);
//        if (i<cnt-1) {
//            strcat(p, demi);
//            p+=length;
//        }
//    }
//    printf("%s
", pbuf);
//
//    return 0;
//}
//输入两个字符串,以第二个字符串整体作为分割条件把第一个字符串进行分割,然后输出分割之后的单词。
//比如:
//输入:
//I:am:Optimus:Prime!
//:
//输出:
//I
//am
//Optimus
//Prime!
//输入:
//Hi#@welcome#@to#@qianfeng#@
//#@

//int main(int argc, const char *argv[])
//{
//    char str[100]={};
//    char substr[10]={};
//    scanf("%s%s", str,substr);
//    char *p,*q;
//    p = q = str;
//    int len = (int)strlen(substr);
//    while ((p = strstr(p, substr))) {
//        *p = '';
//        printf("%s
",q);
//        p += len;
//        q = p;
//    }
//    if(*q!='')
//    {
//        printf("%s
", q);
//    }
//    
//    return 0;
//}
原文地址:https://www.cnblogs.com/0515offer/p/4560551.html