• 算法实现c语言--02


    1. 从键盘上输入字符,将小写字母转换成大写字母。输入ctl + z” 结束
    #include<stdio.h>
    #include<stdlib.h>
    //从键盘上输入字符,将小写字母转换成大写字母。输入“ctl + z” 结束 
    
    int main()
    {
        char c;
        while (fflush(stdin),scanf_s("%c", &c) != EOF)
        {
            if (c >= 'a'&&c<='z' )
            {
                c = c - 32;
                printf("%c", c);
                continue;
            }
            if (c >= 'A'&&c < ='Z')
            {
                printf("%c", c);
                continue;
            }
            else printf("error,input agian
    ");
        }
    
        printf("
    ");
        system("pause");
        return 0;
    }
    1. 从键盘上输入字符,(1分别统计一下其中字母数字,其他字符的个数 2将统计的字母,数字,其他字符的个数以柱状图的形式打印。例如 

      5

    *****

    *****     3

    *****   *****     2

    *****   *****   *****

    *****   *****   *****

     alp     num     oth

     

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct {
        int n;
        char *name;
    }elem;
    elem alp, num, oth, print[3], tmp;
    void paixu(elem *print)
    {
        if ((print[0].n < print[1].n) == 1)
        {
            tmp = print[0];
            print[0] = print[1];
            print[1] = tmp;
        }
        if ((print[0].n < print[2].n) == 1)
        {
            tmp = print[0];
            print[0] = print[2];
            print[2] = tmp;
        }
        if ((print[1].n < print[2].n) == 1)
        {
            tmp = print[1];
            print[1] = print[2];
            print[2] = tmp;
        }
    }
    int main()
    {
        char c;
        int cnt[3] = { 0 };
        int i = 0, j = 0;
        int maxc;
        alp.n = 0;
        num.n = 0;
        oth.n = 0;
        alp.name = "alp";
        num.name = "num";
        oth.name = "oth";
    
        int flag = 1;
        while (fflush(stdin), scanf_s("%c", &c) != EOF)
        {
            if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')
            {
                alp.n++;
                printf("%c", c);
                continue;
            }
            else if (c >= 48 && c <= 57)
            {
                num.n++;
                printf("%c", c);
                continue;
            }
            else if (c != '') {
                oth.n++;
                printf("%c", c);
            }
            if (c == '
    ')break;
        }
        oth.n--;
    
    
        print[0] = alp;
        print[1] = num;
        print[2] = oth;
        paixu(print);
    
        for (i = 0; i <= print[0].n; i++)
        {
            printf("
    ");
            if (print[0].n - i <= print[0].n)
            {
    
                if (print[0].n - i == print[0].n)
                {
                    if (print[0].n == print[1].n)
                    {
                        if (print[1].n == print[2].n)
                            printf("%s	%s	%s", print[0].name, print[1].name, print[2].name);
                        else printf("%s	%s", print[0].name, print[1].name);
                    }
                    else printf("%s", print[0].name);
                    continue;
                }
                printf("*****	");
                //////////////////////////////////
                if (print[0].n - i <= print[1].n)
                {
                    if (print[0].n - i == print[1].n)
                    {
                        if (print[1].n == print[2].n)
                            printf("%s	%s", print[1].name, print[2].name);
                        else printf("%s", print[1].name);
                        continue;
                    }
                    printf("*****	");
                    /////////////////////////////////////
                    if (print[0].n - i <= print[2].n)
                    {
                        if (print[0].n - i == print[2].n)
                        {
                            printf("%s", print[2].name);
                            continue;
                        }
                        printf("*****	");
                    }
                }
            }
        }
        printf("
    ");
    
        printf("%d	", print[0].n);
        printf("%d	", print[1].n);
        printf("%d	", print[2].n);
        printf("
    ");
    
    
        printf("
    ");
        system("pause");
        return 0;
    }

    3.进制转换。

    (1) 将十进制数转换成二进制数。输入进制数输出对应的二进制数,输入“ctl + z”结束

     

    #include<stdio.h>
    #include<stdlib.h>
    
    #define N 8
    void fun(int num, int k)
    {
        int arr[N], i;
        for (i = 0; i <N; i++)
        {
            arr[i] = num % k;
            num = num / k;
            if (num == 0)
                break;
        }
        for (; i >= 0; i--)
            printf("%d", arr[i]);
        printf("
    
    
    ");
    }
    
    int main()
    {
        int n;
    
        while (fflush(stdin), scanf_s("%d", &n) != EOF)
        {
            fun(n, 2);
        }
        printf("
    ");
        system("pause");
        return 0;
    }

     

    (2)将二进制转换成十进制数。输入进制数输出对应的十进制数输入“ctl + z”结束

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        char *p, s[6]; int n;
        p = s;
        gets(p);
        n = 0;
        while (*p != '')
        {
            n = n * 2 + *p - '0';
            p++;
        }
        printf("%d", n);
        system("pause");
        return 0;
    }

    (3) 将十进制数转换成十六进制数输入十进制数输出对应的十六进制数。输入ctl + z” 结束

     

    #include<stdio.h>
    #include<stdlib.h>
    
    #define N 8
    void fun(int num)
    {
        int arr[N], i;
        for (i = 0; i <N; i++)
        {
            arr[i] = num % 16;
            num = num / 16;
            if (num == 0)
                break;
        }
        for (; i >= 0; i--)
            switch (arr[i])
            {
            case 10: printf("A"); break;
            case 11: printf("B"); break;
            case 12: printf("C"); break;
            case 13: printf("D"); break;
            case 14: printf("E"); break;
            case 15: printf("F"); break;
            default: printf("%d", arr[i]);
            }
        printf("
    
    
    ");
    }
    
    int main()
    {
        int n;
    
        while (fflush(stdin), scanf_s("%d", &n) != EOF)
        {
            fun(n);
        }
        printf("
    ");
        system("pause");
        return 0;
    }

     

    (4) 将十六进制数转换成十进制数。输入十六进制数输出对应的十进制数。输入ctl +  z” 结束

     

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        char *p, s[6]; int n;
        p = s;
        gets(p);
        n = 0;
        
        while (*p != '')
        {
            switch (*p)
            {
            case '0':n = 0; break;
            case '1':n = 1; break;
            case '2':n = 2; break;
            case '3':n = 3; break;
            case '4':n = 4; break;
            case '5':n = 5; break;
            case '6':n = 6; break;
            case '7':n = 7; break;
            case '8':n = 8; break;
            case '9':n = 9; break;
            case 'A':n = 10; break;
            case 'B':n = 11; break;
            case 'C':n = 12; break;
            case 'D':n = 13; break;
            case 'E':n = 14; break;
            case 'F':n = 15; break;
            default:break;
    
            }
            n = n * 16 + *p - '0';
            p++;
        }
        printf("%d", n);
        system("pause");
        return 0;
    }

     

    4.统计一个整数对应的二进制数的1的个数。输入一个整数(可正可负) 输出该整数的二进制包含1的个数ctl+ z” 结束

    #include<stdio.h>
    #include<stdlib.h>
    
    #define N 8
    void fun(int num, int k)
    {
        int arr[N], i, cnt = 0;
        for (i = 0; i <N; i++)
        {
            arr[i] = num % k;
            num = num / k;
            if (num == 0)
                break;
        }
        for (; i >= 0; i--)
        {
            if (arr[i] == 1)cnt++;
            printf("%d", arr[i]);
        }
        printf("共有%d个1",cnt);
        printf("
    
    
    ");
    }
    
    int main()
    {
        int n;
    
        while (fflush(stdin), scanf_s("%d", &n) != EOF)
        {
            fun(n, 2);
        }
        printf("
    ");
        system("pause");
        return 0;
    }

    5.有101个整数,其中有50个数出现了两次,1个数出现了一次, 找出出现了一次的那个数。

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        int a[101];
        int i, ret = 0;
        for (i = 0; i < 50; i++) a[i] = i + 1;
        for (i = 51; i <= 100; i++) a[i] = i - 50;
        scanf_s("%d", &a[50]);
        for (i = 0; i <= 100; ++i)
        {
            ret = ret ^ a[i];
        }
        printf("%d
    ", ret);
        printf("
    ");
        system("pause");
        return 0;
    }

    6.

    1输入年月日输出该日期是当年的第几天。

    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31 };
        int year, mon, day,sum=0;
        scanf_s("%d%d%d", &year, &mon, &day);
        for (int i = 0; i < mon - 1; i++)sum += a[i];
        sum += day;
        if (mon > 2)sum =sum+(year % 4 && year % 100 || year % 400);
        printf("%d", sum);
    
        printf("
    ");
        system("pause");
        return 0;
    };

    (2)输入两个日期( 日) 输出这两个日期之间差多少天

    #include<stdio.h>
    #include<stdlib.h>
    
    int isleap(int year)
    {
        return year % 4==0 && year % 100==0 || year % 400==0;
    }
    int main()
    {
        int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        int year1 = 0, mon1 = 0, day1 = 0, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0;
    
        scanf_s("%d %d %d", &year1, &mon1, &day1);
        scanf_s("%d %d %d", &year2, &mon2, &day2);
    
        for (int i = 0; i < mon1 - 1; i++)sum1 += a[i];
        sum1 += day1;
        if (mon1 > 2)sum1 = sum1 + isleap(year1);
        printf("%d天", sum1);
    
        for (int i = 0; i < mon2 - 1; i++)sum2 += a[i];
        sum2 += day2;
        if (mon2 > 2)sum2 = sum2 + isleap(year2);
        printf("%d天", sum2);
    
        gap = abs(year2 - year1);
        sum = sum2 - sum1 + gap * 365 + gap / 4 ;
    
        printf("%d天", sum);
    
        printf("
    ");
        system("pause");
        return 0;
    }

    (3)输入一个日期,输出该日期是星期几。

    #include<stdio.h>
    #include<stdlib.h>
    
    int isleap(int year)
    {
        return year % 4==0 && year % 100==0 || year % 400==0;
    }
    int main()
    {
        int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        int year1 = 2018, mon1 = 3, day1 = 4, year2 =0 , mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0, gap = 0;
        int week = 0;
    
        scanf_s("%d %d %d", &year2, &mon2, &day2);
    
        for (int i = 0; i < mon1 - 1; i++)sum1 += a[i];
        sum1 += day1;
        if (mon1 > 2)sum1 = sum1 + isleap(year1);
        for (int i = 0; i < mon2 - 1; i++)sum2 += a[i];
        sum2 += day2;
    
        if (mon2 > 2)sum2 = sum2 + isleap(year2);
    
        gap = abs(year1 - year2);
        sum = sum2 + gap * 365 + gap / 4 - sum1;
        week = sum;
        while (week /7!=0)
        {
            week %= 7;
        }
        if(week==0||week==7)
            printf("星期日", week);
        else
            printf("星期%d", week);
    
        printf("
    ");
        system("pause");
        return 0;
    }

    4.输入 一个日期 一个整数 n输出从该日期经过n天以后日期。

     -> date -> tomorrow of the date

    #include<stdio.h>
    #include<stdlib.h>
    
    int isleap(int year)
    {
        return year % 4 == 0 && year % 100 == 0 || year % 400 == 0;
    }
    int main()
    {
        int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        int year1 = 2018, mon1 = 3, day1 = 5, year2 = 0, mon2 = 0, day2 = 0, sum1 = 0, sum2 = 0, sum = 0,
            gap = 1, days = 0;
        int week = 0;
        int i;
        scanf_s("%d", &days);
    
        for (i = 0; i < mon1 - 1; i++)sum1 += a[i];
        sum1 += day1;
        if (mon1 > 2)sum1 = sum1 + isleap(year1);
    
        sum = sum1 + days;
        year2 = year1;
    
        while (sum > 365 + isleap(year2))
        {
            year2 = year2 + gap;
            sum -= 365 - isleap(year2);
            
        } ;
    
        if (sum < 365 + isleap(year2))
        {
            for (i = 0; sum > a[i]; i++)
            {
                sum -= a[i];
            }
            i++;
            mon2 = i;
            day2 = sum;
            printf("%d年%d月%d日", year2, mon2, day2);
        }
    
            printf("
    ");
        system("pause");
        return 0;
    }        

     

     

     

  • 相关阅读:
    UESTC 913 握手 Havel定理+优先队列
    UESTC 912 树上的距离 --LCA+RMQ+树状数组
    UESTC 901 方老师抢银行 --Tarjan求强连通分量
    UESTC 900 方老师炸弹 --Tarjan求割点及删点后连通分量数
    UESTC 899 方老师和农场 --双连通分量的构造
    UESTC 898 方老师和缘分 --二分图匹配+强连通分量
    ACdream OJ 1099 瑶瑶的第K大 --分治+IO优化
    Codeforces Zepto Code Rush 2014 -C
    Floyd判最小环算法模板
    POJ 1364 King --差分约束第一题
  • 原文地址:https://www.cnblogs.com/cthon/p/8885999.html
Copyright © 2020-2023  润新知