2022年春季学期《算法分析与设计》练习7

问题 A: X星数列

题目描述

爱冒险的X星人在一艘海底沉船上发现了一串神秘数列,这个数列的前8项如下:
5, 8, 18, 34, 70, 138, 278, 554
X星人对这串数列产生了浓厚的兴趣,他希望你能够帮他发现这个神秘数列中所蕴含的规律,并且编写一个程序输出该数列前N项的和。
当输入一个正整数N时,请输出这个神秘数列前N项的和。

输入

单组输入,每组输入一个正整数N(N<=20)。

输出

输出一个正整数,对应这个数列前N项的和。

样例输入 Copy

4

样例输出 Copy

65
import java.util.Scanner;
 
public class Main{
 
    public static int fun(int n){
        if(n==1)
         return 5;
        else if(n==2)
            return 8;
        else return 2*fun(n-2)+fun(n-1);
        }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
    sum=sum+fun(i);
System.out.println(sum);
    }
 
}

问题 B: X星计数

题目描述

热爱数学的X星人又发现了一个有趣的游戏,游戏名叫做:1的个数。
具体游戏规则为:给定一个十进制正整数,计算对应的二进制整数中有多少个1。

输入

多组输入。

第一行输入一个整数T(1<=T<=100),表示总共有T组数据。

接下来T行,每行一个十进制正整数n,其中1<=n<=1e^9。

输出

对于每一组输入,输出十进制正整数对应的二进制整数中包含的1的个数。

样例输入 Copy

3
1
9
12

样例输出 Copy

1
2
2
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int m= scan.nextInt();
       while(scan.hasNext()){
           
          
        int n= scan.nextInt();
       String a="";
       while(n!=0){
           a=n%2+a;
           n=n/2;
       }
       int count=0;
       for(int i=0;i<a.length();i++){
           if(a.charAt(i)=='1')
               count+=1;
       }
           System.out.println(count);
       
       }
 
    }
}

问题 C: X星宿舍

题目描述

X星大学的宿舍很有意思,男生都是6人间,女生都是5人间。
现在已知N个学生的性别,性别用'0'或者'1'表示,其中'0'表示男生,'1'表示女生。
请你编写一个程序计算最少需要多少间男生宿舍?多少间女生宿舍?
(注:不要求每一间宿舍都住满)。

输入

单组输入。
输入N个0或1,两两之间用英文空格隔开,且N不超过1000。

输出

输出两个整数,分别表示最少需要的男生宿舍和女生宿舍数量,两者之间用英文空格隔开。

样例输入 Copy

0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 1 0 0 1

样例输出 Copy

2 2
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        String str=scan.nextLine();
        int count=0;
        int man=0;
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='1')
                count+=1;
       else if(str.charAt(i)=='0')
            man+=1;
    }
    int x=man/6;
        int y=count/5;
        if(man%6!=0) x++;
        if(count%5!=0) y++;
        System.out.println(x+" "+y);
    }
}

问题 D: Welcome

题目描述

”How happy we are, To meet friends from afar!”
Welcome to Hunan University of Chinese Medicine!
Hope all of you can enjoy the competition ^ v ^
Now your task is to read an integer w and output the character painting of ”HNUCM”, there are w
space(s) (space are represented by dot) between two letters. Please refer to the sample for the specific
format.

输入

There are several test files and each contains one case.
The input contains only 1 integer w (1 ≤ w ≤ 2018).

输出

The output has 5 lines, each line has 25+4w characters which only contains ’o’(lowercase letter ’o’) and
’.’(English period ’.’)

样例输入 Copy

1

样例输出 Copy

o...o.o...o.o...o.ooooo.o...o
o...o.oo..o.o...o.o.....oo.oo
ooooo.o.o.o.o...o.o.....o.o.o
o...o.o..oo.o...o.o.....o...o
o...o.o...o.ooooo.ooooo.o...o
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        String a="";
        for(int i=0;i<n;i++)
            a=a+'.';
        System.out.println("o...o"+a+"o...o"+a+"o...o"+a+"ooooo"+a+"o...o");
        System.out.println("o...o"+a+"oo..o"+a+"o...o"+a+"o...."+a+"oo.oo");
        System.out.println("ooooo"+a+"o.o.o"+a+"o...o"+a+"o...."+a+"o.o.o");
        System.out.println("o...o"+a+"o..oo"+a+"o...o"+a+"o...."+a+"o...o");
        System.out.println("o...o"+a+"o...o"+a+"ooooo"+a+"ooooo"+a+"o...o");
    }
}

问题 E: 大整数乘法

题目描述

使用分治算法实现两个大整数相乘。

输入

两个十进制大整数,满足每一个整数长度为2^n且两个大整数的长度相等。(多组数据)

输出

两个大整数的乘积。

样例输入 Copy

1234 5678

样例输出 Copy

7006652
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        while(scan.hasNext()){
        int a= scan.nextInt();
        int b= scan.nextInt();
            System.out.println(a*b);}
    }
}

问题 F: 整数划分问题之备忘录法

题目描述

使用备忘录法编写一个程序,求一个正整数n的所有划分个数。 
例如,输入3,输出3;输入4,输出5。 

输入

多组输入,每一组是一个正整数n。

输出

输出划分数。

样例输入 Copy

3
4

样例输出 Copy

3
5
import java.util.Scanner;
 
public class Main{
    private static int fun(int n, int m) {
        int a[][]=new int[110][110];
        if(n<1||m<1) return 0;
        if(n==1||m==1) return 1;
        if(n<m) return fun(n,n);
        if(n==m){
            if(a[n][n-1]==0)
                a[n][n-1]=fun(n,n-1);
            return 1+a[n][n-1];
        }
        if(n>m){
            if(a[n][m-1]==0)
                a[n][m-1]=fun(n,m-1);
           if(a[n-m][m]==0)
               a[n-m][m]=fun(n-m,m);
           return a[n][m-1]+a[n-m][m];
        }
        return 0;
    }
    public static void main(String[] args) {
        Scanner scan =new Scanner(System.in);
        while(scan.hasNext()){
            int n= scan.nextInt();
            System.out.println(fun(n,n));
        }
    }
 
}

问题 G: 矩阵乘法

题目描述

设M1和M2是两个n×n的矩阵,使用分治法计算M1×M2 的乘积。n为2^k,且k<=10。

输入

一个整数n表示矩阵的维数,接下来n行为第一个矩阵,再下面n行为第二个矩阵。

输出

矩阵的乘积(两个数字之间空一格,数字右对齐)。

样例输入 Copy

2
1 1
2 2
3 3
4 4

样例输出 Copy

 7  7
14 14
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int k= scan.nextInt();
        int m[][]=new int[k][k];
        int n[][]=new int[k][k];
        int h[][]=new int[k][k];
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++)
                m[i][j]=scan.nextInt();
        }
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++)
                n[i][j]=scan.nextInt();
        }
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++)
            {
                for(int q=0;q<k;q++)
                    h[i][j]=h[i][j]+m[i][q]*n[q][j];
            }
 
        }
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++) {
                System.out.printf("%2d ", h[i][j]);
                if(j==k-1&&i!=k-1)
                    System.out.println();
            }
        }
    }
}

问题 H: 大还是小?

题目描述

输入两个实数,判断第一个数大,第二个数大还是一样大。每个数的格式为:

[整数部分].[小数部分] 

简单起见,整数部分和小数部分都保证非空,且整数部分不会有前导 0。不过,小数部分的最 后可以有 0,因此 0.0和 0.000是一样大的。 

输入

输入包含不超过 20组数据。每组数据包含一行,有两个实数(格式如前所述)。每个实数都 包含不超过 100个字符。 

输出

对于每组数据,如果第一个数大,输出"Bigger"。如果第一个数小,输出"Smaller"。如果两个 数相同,输出"Same"。 

样例输入 Copy

1.0 2.0
0.00001 0.00000
0.0 0.000

样例输出 Copy

Case 1: Smaller
Case 2: Bigger
Case 3: Same
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
       int n=1;
        while(scan.hasNext()){
            double a= scan.nextDouble();
            double b= scan.nextDouble();
          if(a<b) System.out.println("Case "+n+": Smaller");
            if(a>b) System.out.println("Case "+n+": Bigger");
            if(a==b) System.out.println("Case "+n+": Same");
            n++;
 
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君临๑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值