zoukankan      html  css  js  c++  java
  • 指针知识复习

    基础1
    输入年和天数,输出对应的年月日
    样例输入
    2000 61
    样例输出
    2000-3-1

    #include<stdio.h> 
    
    void GetData(int *year,int *data) 
    {//该函数读入年份和天数,并通过形参指针将结果写入主函数 
        scanf("%d%d",year,data);
    }
    
    void Count(int year,int data,int *month,int *day)
    {//该函数计算第几月份和第几天 
        int num[2][13]{{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
        int find = 0;
        int i;
        if(year%4 == 0&&year%100!=0||year%400== 0)
            find  = 1;
        for(i = 1;data-num[find][i]>= 0;i++)
            data -= num[find][i];
        *month = i;
        *day = data;
    }
    
    void PrintData(int year,int month,int day)
    {//该函数输出年月日 
        printf("%d-%d-%d
    ",year,month,day);
    }
    
    int main()
    {
        int year,data,month,day;
        GetData(&year,&data);
        Count(year,data,&month,&day);
        PrintData(year,month,day);
        return 0;
    }

    基础2
    实数的截断取整,去掉小数的前导0,只取整数部分,否则输出0

    #include<stdio.h>
    
    int main()
    {
        char str[100];
        gets(str);
        char *p = str;
        char *q = NULL;
        while(*p == '0')
            p ++;
        if(*p == ''||*p == '.')
            p--;
        while(*p!='.')
        {
            printf("%c",*p);
            p++;
            if(p==NULL||*p =='')
                break;
        }
        printf("
    ");
        return 0;
    }

    基础3
    将十进制数转换为r进制

    #include<stdio.h>
    #include<string.h>
    void DTor(int n,int r,char *str)
    {
        char p[] ="0123456789abcdef";
        int k = 0,i,d;
        while(n)
        {
            d = n%r;
            str[k] = p[d];
            k++;
            n = n/r;
        }
        for(i = k-1; i >= 0; i--)
            printf("%c",str[i]);
        printf("
    ");
    }
    
    int main()
    {
        int n;
        int r;
        char str[100];
        scanf("%d%d",&n,&r);
        DTor(n,r,str);
        return 0;
    }

    基础4
    输出字符串的最后三个字符

    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char str[100];
        char *p;
        gets(str); 
        int l = strlen(str);
        p = str+l-3;
        puts(p);
        return 0;
     } 

    基础5
    账单处理,输入一个整数n(n<100),再输入n行账单信息,每一行由事物的名字name和对应的花费c组成,长度不超过200,中间会有一个或多个空格,而每一行的开头和结尾没有空格。
    输出总的花费,小数点后保留一位数字
    输入样例
    3
    apple 2.3
    BUY clothes for girl friend 260.5
    Go to cinema 30
    输出样例
    292.8

    #include<stdio.h>
    
    void Count(int n)
    {
        int i;
        char *j;
        char str[100];
        double d,sum = 0.0;
        for( i = 1; i <= n; i++)
        {
            gets(str);
            for(j =str;j!=NULL;j++)
            {
                if(*j >= '0'&&*j<= '9')
                    break;
            }
            sscanf(j,"%lf",&d);
            sum += d;
        }
        printf("%.1lf
    ",sum);
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        getchar();
        Count(n);
        return 0;
    }

    基础6
    动态申请一个一维数组。

    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int n,i;
        int *p;
        scanf("%d",&n);
        p = (int*)malloc(n*sizeof(int));
        for( i =1; i <= n; i ++)
            scanf("%d",p+i);
        for( i = 1; i <= n; i ++)
            printf("%d ",*(p+i));
        printf("
    ");
        return 0;   
    } 
  • 相关阅读:
    yourphp常用标签
    如何访问他人电脑上的共享文件夹
    Win7如何分享局域网并设置共享文件夹账户和密码
    CLR Via CSharp读书笔记(21):自动内存管理(垃圾回收)
    《Java编程思想》之I/O系统
    WebCore::Node Dump
    java中的IO整理(3)数据操作流合并流压缩流输入输出重定向 老秋的日志 网易博客
    WebKit 分析–for android Braincol 博客园
    JavaScript EE,第 2 部分: 用 Ajax 调用远程 JavaScript 函数
    java中System重定向输出流
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350140.html
Copyright © 2011-2022 走看看