程序设计基础作业2

1. (程序题)

题目编号 :Exp02-Basic04

题目名称:养老金

题目描述:某一国家养老金发放的原则是:

(1)男人(假设用数字1表示)超过65岁每周给50元钱,如果超过70岁每周再加20元钱。

(2)女人(假设用数字2表示)超过60岁每周给45元钱,如果超过65岁每周再加25元钱。

编程序,读入一个人的性别和年龄,输出他每周可领养老金数额。如果一个人还没到拿取养老金的年龄,那么就输出一个适合的信息。


输入:输入一行包括2个整数,分别对应性别和年龄,输入保证性别年龄输入都为合理整数。

输出:如果达到领取养老金年龄,就请输出每周应得养老金数额;否则输出NULL。
 

样例1:

输入:
1 75
输出:
70

样例2:

输入:
2 63
输出:
45

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    int sex;
    int money;
    cin >> sex >> money;
    if(sex == 1){
        if(money > 70){
            cout << 70;
        }
        if(money <= 65){
            cout << "NULL";
        }else{
            cout << 50;
        }
    }
    if(sex == 2){
        if(money <= 60){
            cout << "NULL";
        }else if(money > 65){
            cout << 70;
        }else{
            cout << 45;
        }
    }
}

2. (程序题)

题目编号:Exp02-Basic10,GJBook3-04-15

题目名称:爱因斯坦阶梯

问题描述:设有阶梯,不知其数,但知:每步跨2阶,最后省1阶;每步跨3阶,最后省2阶;每步跨5阶,最后省4阶;每步跨7阶,正好到楼顶。编程序求最少共有多少阶。

输入:无

输出:台阶数目

样例:无。详见输入输出说明。

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    int i = 0;
    while(true){
        if(i%2 == 1&&i%3==2&&i%5==4&&i%7==0){
            break;
        }else{
            i++;
        }
    }
    cout << i;
}

3. (程序题)

题目编号 :Exp02-Basic06,GJBook3-04-09

题目名称:符合条件自然数

题目描述:编写程序,打印所有小于正整数data且可被11整除的自然数。

输入:从键盘输入一个正整数data

输出:输出所有小于data且可被11整除的自然数,数与数之间以一个空格做间隔,最后一个数后无多余字符。
 

样例1:

输入:50
输出:0 11 22 33 44

样例2:

输入:80
输出:0 11 22 33 44 55 66 7

#include <cstdio>

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

int main(){

    int n;

    cin >> n;

    for(int i = 0;i < n;i++){

        if(i%11 == 0){

            cout << i << " ";

        }

  }

}

4. (程序题)

题目编号 :Exp02-Basic08,GJBook3-04-12

题目名称:三位Armstrong数

题目描述:编写程序,打印所有3位的Armstrong数,Armstrong数是指其值等于它本身每位数字立方和的数,如153就是一个Armstrong数。153=


 

输入:无
输出:打印所有3位的Armstrong数,每个Armstrong数间用一个西文空格间隔,最后一个数后无多余字符。

样例:无。详见输入输出说明

​#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int ifa(int input){
    int a3 = input % 10;
    int a2 = ((input - a3)/10)%10;
    int a1 = (input - 10*a2 - a3)/100;
    if(pow(a1,3)+pow(a2,3)+pow(a3,3) == input){
        return 1;
    }else{
        return 0;
    }
}
int main(){
    for(int i = 100;i <= 999;i++){
        if(ifa(i)){
            cout << i << " ";
        }
    }
    }

5. (程序题)

题目编号:Exp02-Basic07,GJBook3-03-07

题目名称:倍数

题目描述:任意一个整数n,如果n能同时被3、5、7整除,则n是3、5、7的倍数,如果n只能同时被其中的两个数整除,则n是两个数的倍数,如果n只能被其中一个数整除,则n是一个数的倍数,否则n不是3、5、7的倍数。


输入:输入一个整数。

输出:按照是否是倍数输出。


样例1:

输入:105
输出:3,5,7

样例2:

输入:30
输出:3,5

样例3:

输入:21
输出:3,7

样例4:

输入:35
输出:5,7

样例5:

输入:14
输出:7

样例6:

输入:101
输出:NULL
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n;
        Scanner ss =new Scanner(System.in);
        int s1 = ss.nextInt();
        if(s1 % 3 == 0&&s1%5==0&&s1%7==0){
            System.out.println("3,5,7");
            System.exit(0);
        }
        if(s1 % 3 == 0&&s1%5==0){
            System.out.println("3,5");
            System.exit(0);
        }
        if(s1 % 3 == 0&&s1%7==0){
            System.out.println("3,7");
            System.exit(0);
        }
        if(s1 % 5 == 0&&s1%7==0){
            System.out.println("5,7");
            System.exit(0);
        }
        if(s1 % 5 == 0){
            System.out.println("5");
            System.exit(0);
        }
        if(s1 % 3 == 0){
            System.out.println("3");
            System.exit(0);
        }
        if(s1 % 7 == 0){
            System.out.println("7");
            System.exit(0);
        }
        if(s1%3!=0&&s1%5!=0&&s1%7!=0){
            System.out.println("NULL");
        }
    }
}

6. (程序题)

题目编号:Exp02-Enhance04,GJBook3-04-14

题目名称:字母矩阵

题目描述:用循环语句控制打印如下图形,其中输出的每个字母占用2个字符宽度(空格在前,字母在后)。

A B C D E F G H I
B C D E F G H I A
C D E F G H I A B
D E F G H I A B C
E F G H I A B C D
F G H I A B C D E
E F G H I A B C D
D E F G H I A B C
C D E F G H I A B
B C D E F G H I A
A B C D E F G H I

输入:无

输出:如上图字母矩阵

说明:请同学们根据字母、位置的规律实现该程序。打表爽一时,考试两行泪~

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    char a[100];
    for(int i = 0;i < 9;i++){
        a[i] = 'A'+i;
    }
    int count = 0;
    for(int i = 0;i < 6;i++){
        int count = i;
        if(count >= 9){
            count -= 9;
        }
        for(int j = 0;j < 9;j++){
            cout << " " << a[count];
            count++;
            if(count >= 9){
                count -= 9;
            }
        }
        cout <<endl;
    }
    for(int i = 4;i >= 0;i--){
        int count = i;
        if(count >= 9){
            count -= 9;
        }
        for(int j = 0;j < 9;j++){
            cout << " " << a[count];
            count++;
            if(count >= 9){
                count -= 9;
            }
        }
        cout <<endl;
    }
}

7. (程序题)

题目编号 :Exp02-Basic11,GJBook3-04-03

题目名称:勒让德多项式

题目描述:

编一个程序,输入x、n,计算勒让德(Legendre)多项式的第 n 项。

Exp02-Basic11.jpg

输入:一个浮点数和一个整数,分别对应x和n(0<=n<=20)。

输出:一个浮点数,即勒让德多项式第n项的值,注意小数点后保留到第2位。
 

样例1:

输入:3.4 2
输出:16.84

样例2:

输入:3.4 10
输出:30143685.82

样例3:

输入:3.4 21
输出:23525972077722828.00

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
double pp(double x,int n){
   if(n == 0){
       return 1;
   }
   if(n == 1){
       return x;
   }
   if(n > 1){
       return (double)(2*n-1)/(double)n*x*pp(x,n-1)-(double)(n-1)/(double)n*pp(x,n-2);
   }
}
int main(){
   double x;
   int n;
   cin >> x >> n;
   printf("%.2lf",pp(x,n));
}


 

8. (程序题)

题目编号:Exp02-Basic05

题目名称:嵌套函数

题目描述:编写程序,当x=1.0、2.0、…、20.0时,计算如下函数到5层嵌套。F(x)=1+1/(1+1/(1+1/(1+1/(1+1/x))))
 

输入:一个浮点数表示x的值,输入保证x不为零。

输出:一个浮点数F(x)的值,保留小数点后3位。


样例:

1
1.625



#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
double fun(double x){
double solution = x;
for(int i = 0;i < 5;i++){
solution = 1.0+1.0/solution;
}
return solution;
}
int main(){
double x;
cin >> x;

printf("%.3lf",fun(x));

​}

9. (程序题)

题目编号 :Exp02-Enhance01,GJBook3-04-02

题目名称:计算 e^x

题目描述:请计算上述序列前100项的和

GJBook3-04-02.jpg

输入:一个浮点数,对应x值。

输出:一个浮点数,即e^x的近似值,小数点后保留到第2位。

注:本题不允许使用math.h头文件和相关的pow和exp等函数。

样例1:

输入:0
输出:1.00

样例2:

输入:4.3
输出:73.70

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
double fune(double x,int n){
    double t = x;
    if(n == 0){
        return 1;
    }
    for(int i = 0;i < n-1;i++){
        x *= t;
    }
    return x;
}
double funx(int input){
    if(input == 0){
        return 1;
    }
    if(input == 1){
        return 1;
    }else{
        return input*funx(input-1);
    }
}
int main(){
    double x;
    cin >> x;
    double solution = 0;
    for(int i = 0;i <= 99;i++){
        solution += (double)fune(x,i)/(double)funx(i);
    }
    printf("%.2lf",solution);

}

10. (程序题)

题目编号 :Exp02-Basic03

题目名称:数字求和

题目描述:给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,可以被a整除的整数和是多少?
 

输入:输入一行只包括6个小于100的正整数,其中第一个正整数是a,输入保证a不为零。

输出:输出一行,给出一个正整数,是5个数中可以被a整除的所有整数的和。


样例1:

输入:10 10 20 30 40 11
输出:100

样例2:

输入:11 10 20 30 40 12
输出:0
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
    int n,a,b,c,d,e;
    cin >> n >> a >> b >> c >> d >> e;
    int count = 0;
    if(b%n == 0){
        count += b;
    }
    if(c%n == 0){
        count += c;
    }
    if(d%n == 0){
        count += d;
    }
    if(e%n == 0){
        count += e;
    }
    if(a%n == 0){
        count += a;
    }
    cout << count;
}

11. (程序题)

题目编号 :Exp02-Basic12,GJBook3-04-13

题目名称:数字金字塔

题目描述:编程序,制打印如下所示的n行数字金字塔(n由用户从键盘输入)。

图10行.jpg

输入:一个正整数 n (≤10)

输出:如上所示的类似数字金字塔。输出由数字 0~9构成的n行数字三角矩阵:其中第一行有1个数,第二行有3个数,依次类推,每个数字占用2位英文字符宽度,宽度不足2位的的在数字左侧补空格;整个数字三角阵,除必要的空格、数字、回车换行符,无多余字符。

样例1:

输入:1
输出:

图1行.jpg

(注:1的前面有一个空格)

样例2:

输入:3
输出:

图3行.jpg

(注:末行的第一个数字1前面有一个空格)

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    int n;
    cin >> n;
    int imax = 2*n-1;
    int a[100][100];
    for(int i = 0;i < n;i++){
        for(int j = 0;j < imax;j++){
            if(i+j <= n - 2||j-i >= n){
                a[i][j] = 0;
            }else if(i+j == n - 1||j - i == n-1){
                a[i][j] = 1;
            }else if(j == n - 1){
                a[i][j] = i + 1;
                for(int k = 0;k < i - 1;k++){
                    a[i][j - (k+1)] = a[i][j] - (k+1);
                    a[i][j + (k+1)] = a[i][j] - (k+1);
                }
            }
        }

    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j < imax;j++){
            if(a[i][j] == 1&&a[i][j+1] == 0){
                printf(" 1\n");
                break;
            }
            if(a[i][j] == 10){
                printf(" 0");
                continue;
            }
            if(a[i][j] == 0){
                printf("  ");
            }else{
                printf("%2d",a[i][j]);
            }
        }
    }
}

12. (程序题)

题目编号 :Exp02-Enhance02,GJBook3-04-0102

题目名称:公式累乘

题目描述:用如下的展开式计算当n为某给定值时,圆周率π的值。

GJBook3-04-0102.jpg

输入:一个正整数n (n<100000)

输出:一个浮点数,保留小数点后10位。

样例1:

输入:1000
输出:3.1408077460

样例2:

输入:10000
输出:3.1415141187
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    long int n;
    cin >> n;
    double solution = 1;
    for(double i = 1;i <= n;i++){
        solution *= (double)(i*4*i);
        solution = solution/(double)(2*i-1)/(double)(2*i+1);
    }
    printf("%.10f",solution*2);
}

13. (程序题)

题目编号:Exp02-Basic01,GJBook3-03-03

题目名称:递增排序

题目描述:任意三个实数a、b、c,按照从小到大的顺序输出。

输入:输入三个实数(测试数据均只有一位小数)。

输出:按照从小到大顺序输出之前输入的三个实数,以一个西文空格间隔,且每个实数小数点后保留1位。

样例:

输入:
2.3  5.6  1.2
输出:
1.2 2.3 5.6
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    double a[10];
    for(int i = 0;i < 3;i++){
        cin >> a[i];
    }
    sort(a,a+3);
    for(int i = 0;i < 3;i++){
        printf("%.1lf ",a[i]);
    }
}

14. (程序题)

题目编号 :Exp02-Basic09,GJBook3例-04-10

题目名称:斐波纳契序列

问题描述:

开始,有一对小兔子。

 一个月后,变成大兔子开始怀孕;

两个月后, 生出一对小兔子,这时共有两对兔子(一对大兔子, 一对小兔子), 同时大兔子又再次怀孕;

 三个月后, 以前出生的小兔子变成大兔子,以前怀孕的大兔子又生出一对小兔子, 这时共有三对兔子(两对大兔子, 一对小兔子), 所有大兔子又全部怀孕;

 四个月后, 以前出生的小兔子变成大兔子,以前怀孕的大兔子又各生出一对小兔子, 这时共有五对兔子(三对大兔子, 两对小兔子), 所有大兔子又全部怀孕;

五个月后, 以前出生的小兔子变成大兔子,以前怀孕的大兔子又各生出一对小兔子, 这时共有八对兔子(五对大兔子, 三对小兔子), 所有大兔子又全部怀孕;

…… ……

假设在兔子的生养过程中没有死亡。编程序,输入 n , 计算 n 个月后,有多少对兔子, 并输出。

提示:注意序列各项间的递推关系。 

输入:一个非负整数n,表示月份(n≤91)

输出:n 个月后的兔子数(单位:对)

样例1:输入 0   输出 1

样例2:输入 1   输出 1

样例3:输入 2   输出2

样例4:输入10   输出89

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    int n;
    cin >> n;
    long long int small = 1;
    long long int big = 0;
    long long int newSmall = 0;
    for(int i = 0;i < n;i++){
        newSmall = big;
        big += small;
        small = newSmall;
    }
    cout << big + small;
}
 

15. (程序题)

题目编号:Exp02-Basic02

题目名称:括号统计

题目描述:编程序,判断给定以字符‘@’结束的字符序列中‘(’与‘)’、‘[’与‘]’、‘{’与‘}’的个数是否相等。

输入:输入一串以字符‘@’结束的字符序列,其间可能含有若干空白字符。

输出:个数不相等的括号(按花括号、方括号、圆括号的顺序),如果没有不等则输出NULL。


样例1:

输入:{a+b*c+(d/e-f]}}@
输出:{}[]()

样例2:

输入:{a  +  b*c+(d/e-f]}@
输出:[]()
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    char s1[100000];
    int count = 0;
    for(int i = 0;i < 100000;i++){
        cin >> s1[i];
        count++;
        if(s1[i] == '@'){
            break;
        }
    }
    int a1,b1,c1,a2,b2,c2;
    a1 = b1 = c1 = b2 = a2 = c2 = 0;
    for(int i = 0;i < count;i++){
        if(s1[i] == '{'){
            a1++;
        }
        if(s1[i] == '}'){
            a2++;
        }
        if(s1[i] == '['){
            b1++;
        }
        if(s1[i] == ']'){
            b2++;
        }
        if(s1[i] == '('){
            c1++;
        }
        if(s1[i] == ')'){
            c2++;
        }
    }
    if(a1!=a2){
        cout << "{}";
    }
    if(b1!=b2){
        cout << "[]";
    }
    if(c1!=c2){
        cout << "()";
    }
    if(a1 == a2&&b1 == b2&&c1 == c2){
        cout << "NULL";
    }
}
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值