2020年09月 HNUCM-OJ算法分析与设计作业6

@ZHANGQIANYI2020

HNUCM-OJ 与7相关的数,鸡兔共笼,买房,快速排序,随机化快速排序,数组合并

问题 A: 与7相关的数

(时间限制: 1 Sec 内存限制: 128 MB)

题目描述:

一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数。
现求所有小于等于n(n<100)的与7无关的正整数的平方和。

输入:

案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)。

输出:

对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。

样例输入:

21

样例输出:

2336

参考答案:

import java.util.Scanner;
 
public class Main {
     public static boolean isIn7(int a) {
         int temp=a;
         while(temp>0){
             if(temp%10==7){
                 return true;
             }
             temp /=10;
         }
         if(a%7==0) {
             return true;
         }
         return false;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            int n=sc.nextInt();
            int sum=0;
            for(int i=1;i<=n;i++) {
                if(!isIn7(i)) {
                sum+=i*i;
                }
            }
            System.out.println(sum);  
        } 
    }
}

问题 B: 鸡兔共笼

(时间限制: 1 Sec 内存限制: 128 MB)
题目描述:

一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。
已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。

输入:

每组测试数据占1行,每行一个正整数a (a < 32768)。

输出:

输出包含n行,每行对应一个输入,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开。
如果没有满足要求的答案,则输出两个0。

样例输入:

2
3
20

样例输出:

0 0
5 10

参考答案:

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n>0) {
            int m=sc.nextInt();
            if(m%2!=0) {
                System.out.print(0);
                System.out.print(" ");
                System.out.println(0);
            }else {
                int min,max,q,e,n1,n2;
                q=m/2;
                e=m/4;
                n1=m%2/4;
                n2=m%4/2;
                max=q+n1;
                min=n2+e;
                System.out.print(min);
                System.out.print(" ");
                System.out.println(max);
            }
            n--;
        }
    }
}

问题 C: 买房

(时间限制: 1 Sec 内存限制: 128 MB)

题目描述:

某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万。
假设房子价格以每年百分之K增长,并且该程序员未来年薪不变,且不吃不喝,不用交税。
每年所得N万全都积攒起来,问第几年能够买下这套房子(第一年房价200万,收入N万)。

输入:

有多行,每行两个整数N(10<=N<=50), K(1<=K<=20)。

输出:

针对每组数据,如果在第21年或者之前就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible,输出需要换行。

样例输入:

50 10
40 10
40 8

样例输出:

8
Impossible
10

参考答案:

#include<iostream> 
using namespace std;
int main()
{
    double n;
    double k;
    while(cin>>n>>k)
    {
        double y=1;
        double M=200;
        double All=n;
        while(true)
        {
            All+=n;
            M*=(1+k/100);
            if(All>M)
            {
                cout<<y+1<<endl;
                break; 
            }
            if(y>20)
            {
            cout<<"Impossible"<<endl;
            break;
            }
        y++;
        }
    }
    return 0;
}

问题 D: 快速排序

(时间限制: 1 Sec 内存限制: 256MB)

题目描述:

编程实现快速排序算法,深入理解快速排序算法的基本思想。

输入:

多组输入,每组第一个数字为数组长度,然后输入一个一维整型数组。

输出:

输出快速排序之后的一维整型数组(升序)

样例输入:

6 1 8 6 5 3 4
5 12 42 2 5 8

样例输出:

1 3 4 5 6 8
2 5 8 12 42

参考答案:

import java.util.Scanner;
 
public class Main {
    public static void quicksort(int a[],int p,int r) {
        if(p<r) {
            int q=partition(a,p,r);
            quicksort(a,p,q-1);
            quicksort(a,q+1,r);
        }
    }
    private static int partition(int[] a, int p, int r) {
        int x=a[r];
        int i=p-1;
        int temp=0;
        for(int j=p;j<=r-1;j++) {
            if(a[j]<=x) {
                i=i+1;
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
                temp=0;
            }
        }
        temp=a[i+1];
        a[i+1]=a[r];
        a[r]=temp;
        return i+1;
    }
    public static void swap(int a,int b) {
        int temp=a;
        a=b;
        b=temp;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            int n=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<n;i++) {
                a[i]=sc.nextInt();
            }
            quicksort(a,0,n-1);
            for(int i=0;i<n;i++) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
    }
}

问题 E: 随机化快速排序

(时间限制: 1 Sec 内存限制: 128 MB)

题目描述:

使用Java或C++等语言中内置的随机函数实现随机化快速排序,在数组中随机选择一个元素作为分区的主元(Pivot)。

输入:

多组样例输入,每组由一个一维整型数组组成。

输出:

随机化快速排序之后的一维整型数组(升序排列)。

样例输入:

6 1 8 6 5 3 4
5 12 42 2 5 8

样例输出:

1 3 4 5 6 8
2 5 8 12 42

参考答案:

import java.util.Random;
import java.util.Scanner;
 
public class Main {
    public static int Ran(int p,int r) {
        Random ran=new Random();
        int i=ran.nextInt(r-p)+p;
        return i;
    }
    private static int partition(int[] a, int p, int r) {
        int x=a[r];
        int i=p-1;
        int temp=0;
        for(int j=p;j<=r-1;j++) {
            if(a[j]<=x) {
                i=i+1;
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
                temp=0;
            }
        }
        temp=a[i+1];
        a[i+1]=a[r];
        a[r]=temp;
        return i+1;
    }
    public static void ranqsort(int a[],int p,int r) {
        if(p<r) {
            int q=ranpartition(a,p,r);
            ranqsort(a,p,q-1);
            ranqsort(a,q+1,r);
        }
    }
    private static int ranpartition(int[] a, int p, int r) {
        // TODO Auto-generated method stub
        int q=Ran(p,r);
        int temp=a[q];
        a[q]=a[r];
        a[r]=temp;
        return partition(a,p,r);
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            int n=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<n;i++) {
                a[i]=sc.nextInt();
            }
            ranqsort(a,0,n-1);
            for(int i=0;i<n;i++) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
    }
}

问题 F: 数组合并

(时间限制: 1 Sec 内存限制: 256MB)

题目描述:

编写一个程序,将两个有序数组合并成一个更大的有序数组,要求时间复杂度为O(n)。

输入:

多组数据输入,每组输入包括两行,每行第一个数字为数组长度n,然后输入n个有序整数。

输出:

输出合并后的数组(升序),每组输出用一个空行隔开。

样例输入:

3 1 3 5
3 2 4 6
2 1 2
4 3 4 5 6

样例输出:

1 2 3 4 5 6

1 2 3 4 5 6

参考答案:

import java.util.Scanner;
 
public class Main {
    public static void shuzuhebing(int a[],int b[],int n1,int n2) {
        int i=0;
        int j=0;
        int k=0;
        int c[]=new int[n1+n2];
        while((i<n1)&&(j<n2)) {
            if(a[i]<=b[j]) {
                c[k++]=a[i++];
            }else {
                c[k++]=b[j++];
            }
        }
        if(i>=n1) {
            for(int q=j;q<n2;q++) {
                c[k++]=b[q];
            }
        }else {
            for(int q=i;q<n1;q++) {
                c[k++]=a[q];
            }
        }
        for(int x =0;x<c.length;x++) {
            System.out.print(c[x]+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            int n1=sc.nextInt();
            int a[]=new int[n1];
            for(int i=0;i<n1;i++) {
                a[i]=sc.nextInt();
            }
            int n2=sc.nextInt();
            int b[]=new int[n2];
            for(int i=0;i<n2;i++) {
                b[i]=sc.nextInt();
            }
            shuzuhebing(a,b,n1,n2);
            System.out.println();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值