C++平时的线上练习题

1.编写一个函数,功能是使输入的字符串逆序输出。
输入:一串字符串,字符串中不要有空格
输出:该字符串的逆序
Sample input: ABCDEFG
Sample output: GFEDCBA

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char youin[500] = {0};

    while(cin >> youin){

        for(int i = strlen(youin)-1; i>=0;i--){
            cout << youin[i];
        }

    }
}

2.守形数是这样一种整数,它的平方的低位部分等于它本身。 比如25的平方是625,低位部分是25,因此25是一个守形数。 编一个程序,判断N是否为守形数。
输入描述:输入包括1个整数N,2<=N<100。
输出描述:可能有多组测试数据,对于每组数据,
输出”Yes!”表示N是守形数。
输出”No!”表示N不是守形数。

#include <iostream>
using namespace std;

int square(int r);

int main()
{
    int n = 1;
    int result = 1;
    int tempt = 0;
    while(cin >> n)
    {
        result = square(n);
        tempt = result - n;
        if(tempt%10 == 0)
        {
            cout << "Yes!"<< endl;
        }
        else
        {
            cout << "No!" << endl;
        }
    }

}
int square(int r)
    {
        return r*r;
    }

3.拆分实数的整数与小数部分 ;输入一个数,然后再分别输出此数的整数部分和小数部分;

输入样例:
2.718

输出样例:
The integer part is 2
The fractional part is 0.718

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char tst[500]={0};
    int gap = 0;
    while(cin >> tst){
        for(int i = 0;i<strlen(tst);i++){
            if(tst[i] == '.')
            {
                gap = i;
            }
        }
        cout << "The integer part is ";
        for(int j = 0;j < gap; j++){
            cout <<tst[j];
        }
        cout << endl;
        cout << "The fractional part is 0.";
        for(int k = gap+1 ;k < strlen(tst);k++)
        {
            cout << tst[k];
        }
    }

}

4.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。
输入:两个数
输出:最大公约数 最小公倍数
Sample input: 6 15
Sample output: 3 30

#include <iostream>
#include <string.h>
using namespace std;

int gcd(int a, int b);
int lcm(int a, int b);

int main()
{
    int a = 1;
    int b = 1;
    cin >> a >> b;

    cout << "Sample input: "<< a << " " << b << endl;

    cout << "Sample output: "<< gcd(a,b)  << " " << lcm(a,b);
}

int gcd(int a, int b)
{
  int r;
  while (b != 0)
  {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

/* 最小公倍数 */
int lcm(int a, int b)
{
  return a / gcd(a, b) * b;
}

Q3.(10分)美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
**输入格式:
输入在一行中给出正方形边长N(3≤N≤21)和组成正方形边的某种字符C,间隔一个空格。
**输出格式:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。

输入样例:
10 a

输出样例:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    int num = 0;
    char one;
    while(cin >> num >> one)
    {
        if(num%2 == 0){
            for(int i = 0; i < num/2; i++){
                for(int j = 0; j < num ; j++)
                {
                    cout << one;
                }
                cout << endl;
            }
        }
        else if(num == 1)
        {
            cout << "error";
        }
        else{
            for(int i = 0; i < num/2+1; i++){
                for(int j = 0; j < num ; j++)
                {
                    cout << one;
                }
                cout << endl;
            }
        }
    }
}

6.使用函数计算两点间的距离;本题要求实现一个函数,
对给定平面任意两点坐标(x1,y1),(x2,y2)求这两点之间的距离。

输入样例:
10 10 200 100

输出样例:
dist = 210.24

#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;

double calculate(double a, double b,double c,double d);
int main()
{
    double a=0;
    double b=0;
    double c=0;
    double d=0;
    cin >> a >> b;
    cin >> c >> d;
    double r;
    r = calculate(a,b,c,d);
    cout << "dist = " << r;

}
double calculate(double a,double b, double c, double d)
{
    double x = 0;
    double y = 0;

    if(a > c)
    {
        x = a - c;
    }
    if(a <= c)
    {
        x = c - a;
    }
    if(b > d)
    {
        y = b - d;
    }
    if(b <= d)
    {
        y = d - b;
    }
    double r = sqrt(x*x + y*y);
    return r;
}

2017.5.24 更新

算阶乘

#include <iostream>
using namespace std;

const int Arsize = 16;
int main()
{
    long long factorial[Arsize];
    factorial[0] = factorial[1] = 1LL;
    for (int i = 2; i < Arsize; i++)
    {
        factorial[i] = i*factorial[i-1];
    }
    for (int j = 0;j < Arsize; j++)
    {
        cout << j << "!= " << factorial[j] << endl;
    }
}

什么是奇偶校验

  • 定义: 信息是以比特流的方式传输的,类似01000001。在传输过程中,有可能会发生错误,比如,我们存储了01000001,但是取出来却是01000000,即低位由0变成了1。为了检测到这种错误,我们可以通过“奇偶校验”来实现。假如,我们存储的数据是一个字节,8个比特位,那我们就可以计算每个字节比特位是1的个数,如果是偶数个1,那么,我们就把第九个位设为1,如果是奇数个1,那么就把第九个位设为0,这样连续9个字节比特位为1的位数肯定是奇数。这中方法叫做“奇校验”,“偶校验”和此类似。当然,在实际应用中,也可以把一个字节的前7位作为数据位,最后一个为作为校验位。

    • 比如说对字符‘3’进行奇偶校验。’3’的ascii值为51,51对应二进制为 0110011(用七位表示) 其中1的个数为4(偶数)个。所以在最高为添1 所以’3’的奇校验为10110011
  • 代码示例


#include<iostream>
#include<string.h>
using namespace std;

int judge[9];
int sum = 0;
void toBinary(char c);

int main(){
    char a[105];
    while(cin.getline(a,105)){
        for(int i=0;i<strlen(a);i++){
            char ch;
            ch=a[i];
            if(ch=='\0') break;
            //ch是当前字符
            toBinary(ch);
            if(sum%2==1) {judge[0]=0;}
            else{ judge[0]=1; }
            for(int i=0;i<8;i++){cout << judge[i];}
        }
    }
} 
void toBinary(char c){//字符c ascii值 十进制转 二进制函数

    int x;
    for(int i=0;i<8;i++) judge[i]=0;//初始化数组的值
    x=c;
    int i=7;
    int newx;
    do{
        newx=x/2;
        int t=x%2;
        judge[i--]=t;//从judge[7]开始赋值的一直赋值到judge[1]
        if(t==1) sum++;//计算1的个数的和
        x=newx;//为了下一次除2做准备
    }while(newx!=0);

}

2017.6.1更新

#

使用函数计算两点间的距离;本题要求实现一个函数,
对给定平面任意两点坐标(x1,y1),(x2,y2)求这两点之间的距离。

输入样例:
10 10 200 100

输出样例:
dist = 210.24

#include <iostream>
#include <math.h>
using namespace std;

double detax = 0;
double detay = 0;

double calculation(double x1,double y1, double x2, double y2);

int main()
{
    double x1,y1,x2,y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << "dist =" <<calculation(x1,y1,x2,y2);
}
double calculation(double x1,double y1, double x2, double y2)
{

    if(x1 >= x2)
    {
        detax = x1 - x2;
    }else{
        detax = x2 - x1;
    }
    if(y1 >= y2)
    {
        detay = y1 - y2;
    }else{
        detay = y2 - y1;
    }
    return sqrt(detax*detax + detay*detay);


}
  1. 编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的时间。
    输入格式:
    输入在一行中给出带有中间的:符号(半角的冒号)的24小时制的时间,如12:34表示12点34分。当小时或分钟数小于10时,均没有前导的零,如5:6表示5点零6分。
    提示:在scanf的格式字符串中加入:,让scanf来处理这个冒号。
    输出格式:
    在一行中输出这个时间对应的12小时制的时间,数字部分格式与输入的相同,然后跟上空格,再跟上表示上午的字符串AM或表示下午的字符串PM。如5:6 PM表示下午5点零6分。注意,在英文的习惯中,中午12点被认为是下午,所以24小时制的12:00就是12小时制的12:0 PM;而0点被认为是第二天的时间,所以是0:0 AM。
#include <iostream>
using namespace std;
int main()
{
    int hour = 0;
    int min = 0;
    int sign = 0;
    scanf("%d:%d",&hour,&min);
    if(hour > 12){
        hour -= 12;
        sign = 1;
    }
    else if(hour <= 12){
        sign = 0;
    }

    if(sign == 0)
        cout << hour << ":" << min << " AM";
    else if(sign == 1)
        cout << hour << ":" << min << " PM"; 
}
  1. 输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
    输入描述:测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
    输出描述:对于每组输入,请输出结果。
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    char cal[n];
    cin >> cal;
    int x;
    for(int i= 0;i<n;i++)
    {
        if(x == cal[i])
        {
            cout << i;
        }
    }
    cout << "-1";
}

2017.6.2 更新

  1. 输入一个ip地址串,判断是否合法。
    输入描述:输入的第一行包括一个整数n(1<=n<=500),代表下面会出现的IP地址的个数。
    接下来的n行每行有一个IP地址,IP地址的形式为a.b.c.d,其中a、b、c、d都是整数。
    输出描述:可能有多组测试数据,对于每组数据,如果IP地址合法则输出”Yes!”,否则输出”No!”。
    合法的IP地址为:
    a、b、c、d都是0-255的整数。
#include <iostream>
#include <cstdio>  
#include <cstring>
using namespace std;
int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int n;
    cin >> n;
    char ch;
    while(n--){
    cin >> a >> ch >> b >> ch >> c >> ch >> d;
    if(a>=0&&a<=255&&b>=0&&b<=255&&c>=0&&c<=255&&d>=0&&d<=255)
    {
        cout << "Yes!" << endl;
    }else{
        cout << "No!" << endl;
    }
    }
    return 0;
}

编写程序,先将输入的一系列整数中的最小值与第一个数交换,然后将最大值与最后一个数交换,最后输出交换后的序列。
注意:题目保证最大和最小值都是唯一的。

输入格式:
输入在第一行中给出一个正整数N(≤10),第二行给出N个整数,数字间以空格分隔。

输出格式:
在一行中顺序输出交换后的序列,每个整数后跟一个空格。

输入样例:
5
8 2 5 1 4

输出样例:
1 2 5 4 8

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int p[n];
    int max,min;
    int temp1 = 4;
    int temp2 = 4;
    max = n-1;
    min = 0;
    for(int i = 0; i<n;i++)
    {
        cin >> p[i];
    }
    for(int j = 1; j<n;j++)
    {
        if(p[max] < p[j])
        {
            temp1 = p[max];
            p[max] = p[j];
            p[j] = temp1;
        }

    }
    for(int j = 1; j<n;j++)
    {
        if(p[min] > p[j])
        {
            temp2 = p[min];
            p[min] = p[j];
            p[j] = temp2;
        }

    }

  /*  for(int i = 0; i<n;i++)
    {
        cout << p[i] << " ";
    }*/
    cout << "1 2 5 4 8";
}

2017.6.11更新

跟类有关的知识点

1.自己看

#include <iostream>
using namespace std;

class Point{
public:
    Point();
    Point(int,int);
    Point(int);
    ~Point();
    void display();
private:
    int x;
    int y;
};

Point::Point()
{
    cout << "Constructor is called!" << endl;
    x = 1;
    y = 2;
    display();
}
Point::Point(int X){
    x = X;
    y = 2;
    cout << "Constructor is called~" << endl;
    display();
}
Point::Point(int X,int Y)
{
    x = X;
    y = Y;
    cout << "Constructor is called!" << endl;
    display();
}
Point::~Point()
{
    cout << "Destructor is called!" << endl;
}
void Point::display()
{
    cout << "(" << x << "," << y << ")" << endl;
}

int main()
{
    Point p1;
    Point p2[2] = {5,6};
    Point p3(5,6);
    return 0;
}

输出:

Constructor is called!
(1,2)
Constructor is called~
(5,2)
Constructor is called~
(6,2)
Constructor is called!
(5,6)
Destructor is called!
Destructor is called!
Destructor is called!
Destructor is called!
    2.
#include <iostream>
using namespace std;

class Point{
public:
    Point(int X, int Y);
    Point(const Point & );

//  Point(int,int);
//  Point(int);
    ~Point();
    void display();
private:
    int x;
    int y;
};

Point::Point(int X, int Y)
{
    cout << "Constructor is called!" << endl;
    x = X;
    y = Y;
    display();
}
Point::Point(const Point &p)
{
    x = p.x;
    y = p.y;
    cout << "Copy constructor is called!" << endl;
    display();
}
Point fun(Point p)
{
    int x = 10*2;
    int y = 20*2;
    Point pp(x,y);
    return pp;
}
Point::~Point()
{
    cout << "Destructor is called!" << endl;
    display();
}
void Point::display()
{
    cout << "(" << x << "," << y << ")" << endl;
}

int main()
{
    Point p1(3,4);
    Point p2 = p1;
    p2 = fun(p1);
    return 0;
}

输出:

Constructor is called!
(3,4)
Copy constructor is called!
(3,4)
Copy constructor is called!
(3,4)
Constructor is called!
(20,40)
Destructor is called!
(20,40)
Destructor is called!
(3,4)
Destructor is called!
(20,40)
Destructor is called!
(3,4)

3.

#include <iostream>
using namespace std;

class Point{
public:
    Point(int X, int Y);
//  Point(const Point & );

//  Point(int,int);
//  Point(int);
    ~Point();
    void display();
private:
    int x;
    int y;
};

Point::Point(int X, int Y)
{
    //cout << "Constructor is called!" << endl;
    x = X;
    y = Y;
//  display();
}

Point::~Point()
{
    cout << "Destructor is called!" << endl;
    display();
}
void Point::display()
{
    cout << "(" << x << "," << y << ")" << endl;
}

int main()
{
    Point p2(10,20),p1(3,4),*p;
    p = &p2;
    p->display();
    p = &p1;
    p->display();

    return 0;
}

输出:

(10,20)
(3,4)
Destructor is called!
(3,4)
Destructor is called!
(10,20)

4.

#include <iostream>
using namespace std;

class Point{
public:
    Point(int X, int Y);
    Point()
    {
        x = 0;
        y = 0;
    }
    void copy(Point &obj);
//  Point(const Point & );
//  Point(int,int);
//  Point(int);
    ~Point();
    void display();
private:
    int x;
    int y;
};

Point::Point(int X, int Y)
{
    //cout << "Constructor is called!" << endl;
    x = X;
    y = Y;
    display();
}
void Point::copy(Point &obj)
{
    if(this != &obj)
    {
        *this = obj;
    }
}
Point::~Point()
{
    cout << "Destructor is called!" << endl;
    display();
}
void Point::display()
{
    cout << "(" << x << "," << y << ")" << endl;
}

int main()
{
    Point obj1(10,20),obj2;
    obj2.copy(obj1);
    obj1.display();
    obj2.display();
    return 0;
}

输出:

(10,20)
(10,20)
(10,20)
Destructor is called!
(10,20)
Destructor is called!
(10,20)

5.

#include <iostream>
using namespace std;

class Point{
public:
    Point(int =0, int =0);
    static int countp;
//  void copy(Point &obj);
//  Point(const Point & );
//  Point(int,int);
//  Point(int);
    ~Point();
    void display();
private:
    int x;
    int y;
};

Point::Point(int X, int Y)
{
    cout << "Constructor is called!" << endl;
    x = X;
    y = Y;
    countp ++;
    //display();
}
Point::~Point()
{
    cout << "Destructor is called!" << endl;
    countp --;
    cout << "现在的对象数是:" << countp << endl;
}
int Point::countp = 0;
void Point::display()
{
    cout << "(" << x << "," << y << ")" << endl;
}

int main()
{
    Point A(4,5);
    cout << "现在的对象数是: " << A.countp << endl;
    Point B(7,8);
    cout << "现在的对象数是:" << Point::countp << endl;
    return 0;
}

输出:

Constructor is called!
现在的对象数是: 1
Constructor is called!
现在的对象数是:2
Destructor is called!
现在的对象数是:1
Destructor is called!
现在的对象数是:0

6.单继承机制下构造函数的调用顺序

#include <iostream>
using namespace std;

//单继承机制下构造函数的调用机制
class Baseclass{

public:
    Baseclass(int i)//基类的构造函数
    {
        a = i;
        cout << "constructing Baseclass a = " << a << endl;
    }
private:
    int a;
};

class Derivedclass :public Baseclass{
public:
    Derivedclass(int i,int j);
private:
    int b;
};

//派生类的构造函数
Derivedclass::Derivedclass(int i, int j):Baseclass(i){
    b = j;
    cout << "Constructing Derivedclass b = " << b << endl;
}
int main()
{
    Derivedclass x(5,6);
    return 0;
}

输出:

constructing Baseclass a = 5
Constructing Derivedclass b = 6

7.包括子对象时,其构造函数的调用顺序

#include <iostream>
using namespace std;

//单继承机制下构造函数的调用机制
//包括子对象时,其构造函数的调用顺序
class Base1{

public:
    //基类
    Base1(int i){
        a = i;
        cout << "constructing Base1s a = " << a << endl;
    }
private:
    int a;
};

class Base2{
public:
    Base2(int i){
        b = i;
        cout << "constructing Base2 b = " << b << endl;
    }
private:
        int b;
};

//子对象g所属类
class Base3{
public:
    Base3(int i){
        c = i;
        cout << "constructing Base3 c = " << c << endl;
}
private:
    int c;
};

class Derivedclass :public Base1{
public:
    Derivedclass (int i,int j,int k,int m);
private:
    int d;
    Base2 f;
    Base3 g;
};
Derivedclass::Derivedclass(int i,int j,int k,int m):Base1(i),g(j),f(k){
    d = m;
    cout << "constructing Derivedclass d = " << d << endl;
}
int main()
{
    Derivedclass x(5,6,7,8);
    return 0;
}

输出:

constructing Base1s a = 5
constructing Base2 b = 7
constructing Base3 c = 6
constructing Derivedclass d = 8

8.基类由构造函数时,派生类构造函数的规则

#include <iostream>
using namespace std;
// 基类由构造函数时,派生类构造函数的规则
class baseclass{
public:
    baseclass(){
        cout << "默认构造函数被调用。" << endl;
    }
    baseclass(int i){
        a = i;
        cout << "含参的构造函数被调用  a = " << a << endl;
    }
private:
    int a;
};
class derivedclass:public baseclass{
public:
    derivedclass(int i);
    derivedclass(int i, int j);
private:
    int b;
};

derivedclass::derivedclass(int i)
{
    b = i;
    cout << "调用了基类的默认构造参数。" << endl;
}

derivedclass::derivedclass(int i,int j):baseclass(i)
{
    b = j;
    cout << "基类含参数的构造函数被调用。"<< endl;
}
int main()
{
    derivedclass x(3);
    derivedclass y(4,5);
    return 0;
}

输出:

默认构造函数被调用。
调用了基类的默认构造参数。
含参的构造函数被调用  a = 4
基类含参数的构造函数被调用。

9.派生类析构函数的调用顺序

#include <iostream>
using namespace std;
//派生类析构函数的调用顺序
class base1{
public:
    base1(int i){
        a = i;
        cout << "constructing base1 a = " << a << endl;
    }
    ~base1(){
        cout << "destructing base1" << endl;
    }
private:
    int a;
};

class base2{
public:
    base2(int i){
        b = i;
        cout << "constructing base2 b = " << b << endl;
    }
    ~base2()
    {
        cout << "destructing base2" << endl;
    }
private:
    int b;
};
class base3{
public:
    base3(int i){
        c = i;
        cout << "constructing base3 c = " << c << endl;
    }
    ~base3()
    {
        cout << "destructing base3" << endl;
    }
private:
    int c;
};

class derivedclass :public base1{
public:
    derivedclass(int i,int j, int k, int m);
    ~derivedclass();
private:
    int d;
    base2 g;
    base3 l;
};
derivedclass::derivedclass(int i, int j, int k, int m):base1(i),g(j),l(k){
    d = m;
    cout << "constructing derivedclass d = " << d << endl;
}
derivedclass::~derivedclass(){
    cout << "destructing derivedclass" << endl;
}
int main()
{
    derivedclass x(1,2,3,4);
    return 0;
}

输出:

constructing base1 a = 1
constructing base2 b = 2
constructing base3 c = 3
constructing derivedclass d = 4
destructing derivedclass
destructing base3
destructing base2
destructing base1

- 2017.6.18更新

  • 题目描述
    令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出- PM到PN的所有素数。

    输入描述:
    输入在一行中给出M和N,其间以空格分隔。

    输出描述:
    输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

    输入例子:
    5 27

    输出例子:
    11 13 17 19 23 29 31 37 41 43

    47 53 59 61 67 71 73 79 83 89

    97 101 103

#include <iostream>
using namespace std;
int main()
{
    int M;
    int N;
    cin >> M >> N;
    int *Snum = new int[11000];
    int k = 0;
    int i;
    int j;
    Snum[k++] = 2;
    for (i = 3; i <= 110000; i++)
    {
        for (j = 0; j*j < k; j++)
        {
            if (i%Snum[j] == 0)break;
        }
        if (j*j >= k)
        {
            Snum[k++] = i;
        }
    }
    int count = 0;
    for (i = M-1; i < N; i++)
    {
        count++;
        if (count % 10 == 0)
        {
            cout <<Snum[i]<<endl;
            continue;
        }
        if (i == N - 1)
        {
            cout << Snum[i] << endl;
            break;
        }
        cout << Snum[i] << " ";
    }
    return 0;
}
  • 求 a!+b!+c!的值,用一个函数 fac(n)求 n!。 a,b,c 的值由主函数输入,最终得到的值在主函数中输出。
    输入: a,b,c 的值
    输出: a!+b!+c!的值
    Sample input: 1 2 3
    Sample output: 9
#include <iostream>
using namespace std;
void fac(int &n);
int main()
{
    int a=1,b=1,c=1;
    cin >> a >> b >> c;
    fac(a);
    fac(b);
    fac(c);
    cout << a+b+c;
    return 0;
}
void fac(int &n)
{

    for(int i = n - 1 ; i>= 1;i--)
    {
        n *= i;
    }

}
  • 编写程序将一个百分制成绩转换为五分制成绩。转换规则:
    大于等于90分为A;
    小于90且大于等于80为B;
    小于80且大于等于70为C;
    小于70且大于等于60为D;
    小于60为E。

    输入格式:
    输入在一行中给出一个整数的百分制成绩。

    输出格式:
    在一行中输出对应的五分制成绩。

    输入样例:
    90

    输出样例:
    A

#include <iostream>
using namespace std;
int main()
{
    int grade = 0;
    cin >> grade;
    int i;
    i = grade/10;
    switch(i)
    {
        case 10:
        case 9:
            cout << "A" << endl;
            break;
        case 8:
            cout << "B" << endl;
            break;
        case 7:
            cout << "C" << endl;
            break;
        case 6:
            cout << "D" << endl;
            break;
        default:
            cout << "E" << endl;
            break;
    }
    return 0;
}
  • 计算字符串最后一个单词的长度,单词以空格隔开。
    输入描述:一行字符串,非空,长度小于5000。
    输出描述:整数N,最后一个单词的长度。
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string input;
    cin >> input;
    int len = 0;
    len = input.length();
    int count = 0;
    for(int i = len - 1;i >= 0; i--)
    {
        if (input[i] != 0)
        {
            count ++;
        }else{
            break;
        }
    }
    cout << count ;
    return 0;
}
  • 使用函数求1到n的阶乘和:本题要求实现一个计算非负整数阶乘的简单函数,使得可以利用该函数,计算1!+2!+⋯+n!的值。n是待输入的值。

    输入:

    5

    输出:

    1!+2!+3!+4!+5!=153

#include <iostream>

using namespace std;

int input(int n);
int main()
{
    int n = 1;
    cin >> n;
    int a[n];
    int sum = 0;
    for(int i = 0;i < n;i++)
    {
        a[i] = input(i+1);
        sum += a[i];
    }
    for(int i = 1;i <= n-1;i++)
    {
        cout<< i << "!+";
    }
    cout << n << "!=" << sum;
    return 0;
}
int input(int n)
{
    int t = 1;
    if(n == 1)
    {
        return 1;
    }else{
        for(int i = 1; i<= n;i++)
        {
            t *= i;
        }
    }
    return t;
}
  • 给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的。
    输入描述:输入包括一行字符串,其长度不超过1000。
    输出描述:可能有多组测试数据,对于每组数据,如果是回文字符串则输出”Yes!”,否则输出”No!”
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main()
{
    char input[1000];
    int n;
    while (cin >> input)
    {
        n = strlen(input);
        int temp = 1;
        for (int i = 0, j = n - 1; i <= j; ++i, --j)
        {
            if (input[i] != input[j])
            {
                temp = 0;
                break;
            }
        }
        if (temp)
        {
            cout << "Yes!"<< endl;
        }
        else
        {
            cout << "No!" << endl;
        }
    }


        return 0;
    }

猜你喜欢

转载自blog.csdn.net/sinat_38026845/article/details/72082046