posts - 12,comments - 0,views - 2078

语法基础

第一节 变量输入输出

1.1基础知识

1.头文件

#include <cstdio>( scanf printf)#include <iostream> (cin >> cout << endl)

定义变量#define PI 3.14159,定义函数#include <cmath>

2.整体框架
#include <iostream>

using namespace std;

int main()

{
	return 0;
}

3.变量的定义

int 1Byte float 4Byte char 1Byte int 4Byte double 8Byte long long 8Byte long double

注意 long long 输入输出时是 long long = 000000LL

字符串定义时,用string name;例如读入ABC。读入时用cin >> name;即可

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int N;
    cin >> N;
    printf("%d ano(s)\n",N/365);
    N=N%365;
    printf("%d mes(es)\n",N/30);
    N=N%30;
    printf("%d dia(s)\n",N);
    return 0;
    
}
4.字符型

char 型,浮点数型,长整型输入输出(%99用double)

scanf ("%d %d",&a, &b);//输入 scanf("%f",&r);不可以写成    scanf("%.2f",&r);
printf("%d %d\n",a+b,a*b);//输出
char (%c)
cin >> a >> b;
cout << a + b << endl;
浮点数
float a,b;
scanf ("%f %f",&a  ,&b);
printf("a+b= %.1f\n a*b=%.2f\n",a+b,a*b);

5.&作用

取地址(在scanf语句里,通过在变量x前加一个&符号,我们就可以把输入值存放到变量x的地址中。在输出语句printf中,在变量x前加个&符号,就可以知道变量在内存中的实际位置。与运算)

6.例题

<1>题目理解错

加权平均值:加权平均值即将各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数。

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    double A,B;
    scanf("%lf%lf",&A,&B);
    printf("MEDIA = %.5f",(A * 3.5 + B * 7.5)/11);
    return 0;
}

<2>接受数据类型和变量类型不符

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    double n1,count1,n2,count2;
    double price1,price2;
    scanf("%lf%lf%lf\n",&n1,&count1,&price1);
    //错误原因:接受数据类型和变量类型不符 %lf(double)写成%d(int)
    scanf("%lf%lf%lf\n",&n2,&count2,&price2);
    
    printf("VALOR A PAGAR: R$ %.2lf", count1*price1 + count2*price2);
    return 0;
}

<3>定义数据类型需注意:数据长度长不长

image-20221105102903328

定义时用double s,t;用int的话数据过长,会变成0

<4>浮点数没有整除类型,需要先把浮点数转换成整数

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    double m;
    scanf("%lf", &m);
    int n = m * 100;
    printf("NOTAS:\n");
    printf("%d nota(s) de R$ 100.00\n", n / 10000); n %= 10000;
    printf("%d nota(s) de R$ 50.00\n", n / 5000); n %= 5000;
    printf("%d nota(s) de R$ 20.00\n", n / 2000); n %= 2000;
    printf("%d nota(s) de R$ 10.00\n", n / 1000); n %= 1000;
    printf("%d nota(s) de R$ 5.00\n", n / 500); n %= 500;
    printf("%d nota(s) de R$ 2.00\n", n / 200); n %= 200;

    printf("MOEDAS:\n");
    printf("%d moeda(s) de R$ 1.00\n", n / 100); n %= 100;
    printf("%d moeda(s) de R$ 0.50\n", n / 50); n %= 50;
    printf("%d moeda(s) de R$ 0.25\n", n / 25); n %= 25;
    printf("%d moeda(s) de R$ 0.10\n", n / 10); n %= 10;
    printf("%d moeda(s) de R$ 0.05\n", n / 5); n %= 5;
    printf("%d moeda(s) de R$ 0.01\n", n / 1); n %= 1;

    return 0;
}

<易错点>

在某些语言中4/3只能得到1(整数和1整数相除),若要得到1.333……需要使用4/3.0即可。abs(-5.5):是返回函数的绝对值

1.2判断语句

posted on   cathyd  阅读(210)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示