第1章 程序设计入门

程序1-1  计算并输出1+2的值

#include<stdio.h>
int main()
{
     printf("%d\n", 1+2);
     return 0;              
} 

程序1-2  计算输入8/5的值,保留小数点后1位

#include <stdio.h>
int main()
{
     printf("%.1f\n", 8.0/5.0);
     return 0;   
}

程序1-3  复杂的表达式计算

#include <stdio.h>
#include <math.h>

int main()
{
    printf("%.8f\n",1+2*sqrt(3)/(5-0.1);
    return 0;    
}

程序1-4 a+b问题

#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n",a+b);
    return 0;   
}

程序1-5  圆柱体的表面积

#include<stdio.h>
#include<math.h>
int  main()
{
    const double pi = acos(-1.0);
    double r, h, s1, s2, s;
    scanf("%1f%1f", &r, &h);
    s1 = pi * r * r;
    s2 = 2 * pi * r * h;
    s = s1 * 2.0 + s2;
    printf("Area = %.3f\n", s)
    return 0;
}

程序1-6 三位数的反转(1)

#include<stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d%d%d\n", n%10, n/10%10, n/100);
    return 0;      
}

程序1-7   三位数的反转(2)

#include <stdio,h>
int main()
{
    int n,m;
    scanf("%d", &n);
    m = (n%10)*100 + (n/10%10)*10 + (n/100);
    printf("%03d\n", m);
    return 0;
}

程序1-8 变量交换(1)

#include <stdio.h>
int main()
{
    int a,b,t;
    scanf("%d%d", &a, &b);
    t=a;
    a=b;
    b=t;
    printf("%d %d\n", a, b);
    return 0;
}

程序1-9 变量交换(2)

#include <stdio.h>
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    a = a+b;
    b=a-b;
    a=a-b;
    printf("%d %d\n", a, b);
    return 0;
}

程序1-10 变量交换(3)

#include <stdio.h>
int main()
{
     int a, b;
    scanf("%d%d",&a,&b);
    printf("%d %d\n", b, a);
    return 0;
}

 

posted @ 2017-08-30 15:41  御风少爷  阅读(125)  评论(0编辑  收藏  举报