0%

字符串左旋

题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

思路:先把这个字符串的前半部分保存到一个新的字符串中,再把后半部分保存到另一个字符串中。最后,拼接这两个字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
void Get_Front(char* ch,int input,int length,char* Front)//获得前半部分
{
    int i=0;
    for (i=0; i<input; i++)
    {
        Front[i] = ch[i];
    }
}
void Get_Behind(char* ch,int input,int length,char* Behind)//获得后半部分
{
    int j = 0;
    int i=0;
    for (i=input; i<length; i++) {
        Behind[j] = ch[i];
        j++;
    }
}
void Left(char* ch,int input)
{
    int length = 0;//字符串长度
    int x = 0;
    while (ch[length] != '\0')//先看看ch里存了几个字符
    {
        length++;
    }
    char Front[100];//先定义一个字符串来存前边的字符串
    char Behind[100];//定义一个字符串存后半部分字符串
    Get_Front(ch,input,length,Front);//把前半部分存到Front里
    Get_Behind(ch,input,length,Behind);//把后半部分存到Behind里
    int i = 0;
    for (i=0; i<length-input; i++) //替换
    {
        ch[i] = Behind[i];
    }
    for (i=length-input; i<length; i++)//替换
    {
        ch[length-input+x] = Front[x];
        x++;
    }
}
int main()
{
    char ch[100] = {0};
    int input = 0;
    printf("请输入字符串:->");
    scanf("%s",ch);
    printf("请输入要左旋几个字符:->");
    scanf("%d",&input);
    Left(ch,input);
    printf("%s\n",ch);
    return 0;
}

思路:先把这个字符串的后边的字符串保存到一个新的临时字符串中,再把后边的字符往前移,最后把新字符串接到后面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
void Left(char* ch,int input)
{
    int i = 0;
    char temp[input];
    int length = 0;
    while (ch[length] != '\0')//看一下输入的字符串长度是多少
    {
        length++;
    }
    for (i=0; i<input; i++)//先把要后移的字符串保存到一个临时字符串中
    {
        temp[i] = ch[i];
    }
    for (i=input; i<length; i++)//把后边要前移的字符串移动一下
    {
        ch[i-input] = ch[i];
    }
    for (i=0; i<input; i++) //把之前保存的临时字符串赋值到原数组的最后
    {
        ch[length-input+i] = temp[i];
    }
}

int main()
{
    char ch[100] = {0};
    int input = 0;
    printf("请输入字符串:->");
    scanf("%s",ch);
    printf("请输入要左旋几个字符:->");
    scanf("%d",&input);
    Left(ch,input);
    printf("%s\n",ch);
    return 0;
}

字符串右旋

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
void Right(char* ch,int input)
{
    int i = 0;
    char temp[input];
    int length = 0;
    int x = 0;
    while (ch[length] != '\0')//看一下输入的字符串长度是多少
    {
        length++;
    }
    for (i=length-input; i<length; i++)//先把要前移的字符串保存到一个临时字符串中
    {
        temp[x] = ch[i];
        x++;
    }
    for (i=length-input; i>=0; i--)//把前边的字符串往后移,给临时字符串挪位置
    {
        ch[i+input-1] = ch[i-1];
    }
    for (i=0; i<input; i++)//把之前保存的临时字符串赋值到原数组的最前边
    {
        ch[i] = temp[i];
    }
}
int main()
{
    char ch[100] = {0};
    int input = 0;
    printf("请输入字符串:->");
    scanf("%s",ch);
    printf("请输入要右旋几个字符:->");
    scanf("%d",&input);
    Right(ch,input);
    printf("%s\n",ch);
    return 0;
}

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
void Exchange_Biggest(int* input)
{
    int Biggest = input[0];
    int i;
    int temp;
    int record_Biggest = 0;
    for (i=0; i<10; i++)
    {
        if(input[i]>Biggest)//遍历数组,如果比记录到最大的还大,就把Biggest的值换成更大的。
        {
            Biggest = input[i];
            record_Biggest = i;
        }
    }
    temp = input[0];//把第一个值和最大的交换一下
    input[0] = input[record_Biggest];
    input[record_Biggest] = temp;
}
void Exchange_Smallest(int* input)
{
    int Smallest = input[0];
    int i;
    int temp;
    int record_Smallest = 0;
    for (i=0; i<10; i++)
    {
        if(input[i]<Smallest)
        {
            Smallest = input[i];
            record_Smallest = i;
        }
    }
    temp = input[9];
    input[9] = input[record_Smallest];
    input[record_Smallest] = temp;
}
int main() {
    int input[10];
    int i;
    for (i=0; i<10; i++)//输入数组
    {
        scanf("%d",&input[i]);
    }
    Exchange_Biggest(input);//交换最大的数
    Exchange_Smallest(input);//交换最小的数
    for (i=0; i<10; i++) {
        printf("%d ",input[i]);
    }
    return 0;
}

题目:将一个数组逆序输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
void Reverse(char* ch,int sz)
{
    int i,j=0;
    char temp[sz];
    for (i=sz-1; i>=0; i--)//先把逆序的数组保存到一个新的临时数组中
    {
        temp[j] = ch[i];
        j++;
    }
    for (i=0; i<sz; i++) //把新的临时数组内容赋值给原数组
    {
        ch[i] = temp[i];
    }
}
int main() {
    char ch[] = "abcdefghijsdfwdfasdfewfdqxsaefw";
    int sz = sizeof(ch)/sizeof(ch[0])-1;
    Reverse(ch,sz);
    printf("%s",ch);
    return 0;
}

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
void Insert(int* arr, int input)
{
    int i=0;
    if (arr[8]>input) {
        for (i=0; i<10; i++)
        {
            if (arr[i]>input)
            {
                int j;
                for (j=9; j>i; j--)
                {
                    arr[j] = arr[j-1];
                }
                arr[i] = input;
                break;
            }
        }
    }
    else
    {
        arr[9] = input;
    }
}

int main()
{
    int arr[10] = {111,222,333,444,555,666,777,888,999};
    int input;
    printf("请输入要插入的数字:->");
    scanf("%d",&input);
    Insert(arr,input);
    int i;
    for (i=0; i<10; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#define ARR_LENGTH 10
void Insert(int* arr, int input)
{
    int i=0;
    if (arr[ARR_LENGTH-2]>input) {
        for (i=0; i<ARR_LENGTH; i++)
        {
            if (arr[i]>input)
            {
                int j;
                for (j=ARR_LENGTH-1; j>i; j--)
                {
                    arr[j] = arr[j-1];
                }
                arr[i] = input;
                break;
            }
        }
    }
    else
    {
        arr[ARR_LENGTH-1] = input;
    }
}
int main()
{
    int arr[ARR_LENGTH] = {111,222,333,444,555,666,777,888,999};
    int input;
    printf("请输入要插入的数字:->");
    scanf("%d",&input);
    Insert(arr,input);
    int i;
    for (i=0; i<ARR_LENGTH; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

题目:求一个3*3矩阵对角线元素之和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int Cal_Matrix(int arr[3][3])
{
    int i,j;
    int sum = 0;
    for (i=0,j=0; i<3,j<3; i++,j++)
    {
        sum = arr[i][j] + sum;
    }
    return sum;
}
int main() {
    int arr[3][3] = {0};
    int i,j;
    for (i=0; i<=2; i++)//往二维数组里添加数据
    {
        for (j=0; j<=2; j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("%d",Cal_Matrix(arr));
    return 0;
}

题目:字符串反转,如将字符串“Hello World”反转为”dlroW olleH”

代码一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
void Char_Reverse(char* ch)
{
    int i = 0;
    while (ch[i] != '\0')
    {
        i++;
    }
    char temp[i];
    int j = 0;
    int x = i;
    for (j=0; j<i; j++)
    {
        temp[j] = ch[x-1];
        x--;
    }
    for (j=0; j<i; j++)
    {
        ch[j] = temp[j];
    }
}
int main()
{
    char ch[50];
    scanf("%s",ch);
    Char_Reverse(ch);
    printf("%s",ch);
    return 0;
}

但是这段代码无法反转带空格的字符串,原因是scanf遇到空格就停止读取了。
解决这个问题,使用%[^\n],表示读取除了换行符 \n 以外的所有字符,这样可以读取整行文本。


代码二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
void Char_Reverse(char* ch)
{
    int i = 0;
    while (ch[i] != '\0') {
        i++;
    }
    char temp[i];
    int j = 0;
    int x = i;
    for (j=0; j<i; j++)
    {
        temp[j] = ch[x-1];
        x--;
    }
    for (j=0; j<i; j++) {
        ch[j] = temp[j];
    }
}
int main()
{
    char ch[50];
    scanf("%[^\n]",ch);
    Char_Reverse(ch);
    printf("%s",ch);
    return 0;
}

这里改进了scanf不能读取空格之后的问题。
输入Hello World
输出为dlroW olleH

题目:判断一个数字是否为质数。
程序分析:质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数整除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int Prime_Number(int input)
{
    int i;
    for (i=2; i<input; i++)
    {
        if (input%i==0)
        {
            return 1;
        }
    }
    return 0;
}
int main() {
    int input;
    scanf("%d",&input);
    if (Prime_Number(input) == 1)
    {
        printf("不是素数");
    }
    else
    {
        printf("是素数");
    }
    return 0;
}

题目:删除一个字符串中的指定字母,如:字符串“aca”,删除其中的a字母。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
void Check(char* arr,char input)
{
    int i = 0;//i就是输入的字符串的长度
    int j = 0;
    while (arr[i] != '\0')
    {
        i++;
    }
    for (j=0; j<i; j++)
    {
        if (arr[j] == input)
        {
            while (arr[j] != '\0')
            {
                arr[j] = arr[j+1];
                j++;
            }
            j = 0;
        }
    }
}

int main()
{
    char arr[20] = {0};
    char input;
    int sz;
    printf("输入字符串:->");
    scanf("%s",arr);
    while (getchar()!='\n')
    {
        ;
    }
    printf("输入要删除的字符:->");
    scanf("%c",&input);
    Check(arr,input);
    printf("%s\n",arr);
    return 0;
}

题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
monday tuesday wednesday thursday friday saturday sunday

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
int main()
{
    char i;
    char j;
    i = getchar();
    switch (i) {
        case 'm':
            printf("Monday");
            break;
        case 't':
            while (getchar() != '\n')
            {
                ;
            }
            printf("在输入一个字母:->");
            j = getchar();
            if (j == 'u') {
                printf("Tuesday");
            }else
            {
                printf("Thursday");
            }
            break;
        case 'w':
            printf("Wednesday");
            break;
        case 'f':
            printf("Friday");
            break;
        case 's':
            while (getchar() != '\n')
            {
                ;
            }
            printf("在输入一个字母:->");
            j = getchar();
            if (j == 'a') {
                printf("Saturday");
            }else
            {
                printf("Sunday");
            }
            break;
        default:
            break;
    }
    return 0;
}

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int Judge(int input)
{
    if (input/10000 == input %10 || ((input/100)%10 == (input/10)%10 ))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main() {
    int input;
    scanf("%d",&input);
    if (Judge(input) == 1)
    {
        printf("是回文数");
    }
    else
    {
        printf("不是回文数");
    }
    return 0;
}