《算法笔记》第二章 C/C++快速入门 第2.2小节 顺序结构

问题 A: 例题1-1-1 按要求输出信息(1)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("This is my first c program!");
    return 0;
}

问题 B: 例题1-1-2 按要求输出信息(2) 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("********************\nVery Good!\n********************");
    return 0;
}

问题 C: 例题1-2-1 求两个整数之和(1)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=123;
    int b=456;
    int sum;
    sum = a+b;
    printf("sum=%d", sum);
    return 0;
}

问题 D: 例题1-2-2 求两整数数之和(2)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d", a+b);
    return 0;
}

问题 E: 例题3-5 求一元二次方程的根

本题涉及的一元二次方程求根公式为:

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

int main()
{
    double a,b,c, r1, r2, temp;
    scanf("%lf%lf%lf",  &a, &b, &c);
    temp = pow(b, 2.0) - 4*a*c;
    if(a!=0 && temp > 0)
    {
        r1 = (-b+sqrt(temp))/2*a;
        r2 = (-b-sqrt(temp))/2*a;
    }
    else{
        return 0;
    }
    printf("r1=%7.2f\n", r1);
    printf("r2=%7.2f", r2);
    return 0;
}

问题 F: 例题3-9 字符输入输出

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char arr[3];
    scanf("%s", arr);
    printf("%s\n", arr);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值