栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础课-----1.1 变量、输入输出、表达式和顺序语句

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础课-----1.1 变量、输入输出、表达式和顺序语句

案例一:
#include
using namespace std;//为了防止命名冲突
int main(){
    cout << "hello world" << endl;
    return 0;
}

#include//引入可以使用
print()
scanf()

变量类型
    bool false/true   1byte
    char 'c','a',' ','n'   1byte
    int -2147483648---2147483647     4byte
    float 1.23,2.5,1.235e2,  6-7位有效数字   4byte
    double 15--16有效数字    8byte
    long long -2^63-----2^63-1    8byte
    long double 18--19位有效数字    12byte 16byte
提交代码常见错误
Wrong Answer 错误
Time Limit Error 超时
Memory Limit Error 超内存
Segmentation Fault 数组越界




presentation error 意为输出格式错误,也就是你的输出没有按照输出要求进行格式化。

斐波那契数列数列


#include
using namespace std;

int main(){
   int a,b;//斐波那契数列数列。
   a = 1;
   b = 1;
   int n;
   cin >> n;
   int i = 0;
   while (i < n - 1){
       int c = a + b;
       a = b;
       b = c;
       i++;
   }
   cout << a << endl;
   return 0;
}

如图:

曼哈顿距离:
(x1,y1)
(x2,y2)
d=|x2-x1| + |y2 - y1|


完全数

一个整数,除了本身以外的其他所有约数的和如果等于该数,那么我们就称这个整数为完全数。

例如,6 就是一个完全数,因为它的除了本身以外的其他约数的和为 1+2+3=6。

现在,给定你 N 个整数,请你依次判断这些数是否是完全数。

解答:

#include
using namespace std;

int main(){
    int n;
    cin >> n;
    while(n--){
        int x;
        cin >> x;
        int s = 0;
        for(int i = 1;i< x;i++){
            if (x % i == 0){
                s +=i;
            }
        }
        if(s == x){
            printf("%d is perfectn",x);
        }else{
            printf("%d is not perfectn",x);
        }
    }
    return 0;
}```//代码提交状态: Time Limit Exceeded   
//c++一秒内算不能超过一亿次,
//10亿次。

优化版本:

#include
using namespace std;

int main(){
    int n;
    cin >> n;
    while(n--){
        int x;
        cin >> x;
        int s = 0;
        for(int i = 1;i*i< x;i++){
            if (x % i == 0){
                if(i< x){
                    s +=i;
                }
                if(x/i != i && x/i < x){
                    s += x/i;
                }
            }
        }
        if(s == x){
            printf("%d is perfectn",x);
        }else{
            printf("%d is not perfectn",x);
        }
    }
    return 0;
}
斐波那契数列
#include
#include
using namespace std;

int main(){
    int f[100];
    
    f[0] = 0,f[1] = 1;
    int n;
    cin >>n;
    for(int i = 2;i <=n;i++){
        f[i]=f[i-1]+f[i-2];
    }
    printf("%d",f[n]);
    return 0;
}
输入一个n,再输入n个整数.将这n个整数逆序输出.
#include
#include
using namespace std;

int main(){
    int a[100];
    int n,k;
    cin >> n >> k;
    
    for(int i = 0;i> a[i];
    }
    while(k--){
        int t = a[n-1];
        for(int i = n-2;i>=0;i--){
            a[i+1] =a[i];
        }
        a[0]=t;
    }
    for(int i =0;i 


优化;

#include
#include
#include
using namespace std;

int main(){
    int a[100];
    int n,k;
    cin >> n >> k;
    for(int i =0;i< n;i++){
        cin >> a[i];
    }
    reverse(a,a + n);
    reverse(a,a+k);
    reverse(a+k,a+n);
    
    for(int i =0;i < n;i++){
        cout << a[i] << ' ';
    }
    return 0;
}

reverse简介

求阶乘
#include
#include
#include
using namespace std;

const double eps = 1e-6;

double C(int a,int b){
    double res  = 1;
    for(int i=1,j=a;i<=b;i++,j--){
        res = res*j/i;
    }
    return res;
}
int main(){
    int a,b;
    cin >> a >> b;
    cout << C(a,b) << endl;
    return 0;
}

计算2的N次方。N <=10000.


个位存在a[0],高位在后面。

2的N次方的大数求法:
#include
#include
using namespace std;

const int N = 3010;

int main(){
    int a[N] = {1};
    int n;
    cin >> n;
    int t=1;
    // for(int i =0;i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/510132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号