有关数组的题目

import java.util.Scanner;
class Demo01{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the integers between 1 and 100:");
        int[] arr=new int[0];
        /*
        给数组赋值的整个过程
         */
        while(true){
            int num=input.nextInt();
            if(num==0){
                break;
            }
            arr=copyOf(arr);
            arr[arr.length-1]=num;
            
        }
        /*
        冒泡排序
         */
        paiXu(arr);
        /*
        打印排序之后的数组
        */
        show(arr);
        /*
        记录次数的整个过程
         */
        ciShu(arr);
    }
    /*
    数组扩容
     */
    public static int[] copyOf(int[] arr){
        int[] newArr=new int[arr.length+1];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }
    /*
    冒泡排序
    */
    public static void paiXu(int[] arr){
        int temp1;
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr.length-1;j++){
                if(arr[j]>arr[j+1]){
                    temp1=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp1;
                }
            }
        }
    }
    /*
    记录次数的整个过程
    */
    public static void ciShu(int[] arr){
        int temp2=1;
        for(int j=0;j<arr.length-1;j++){
            if(j<arr.length-2){
                if(arr[j]!=arr[j+1]){
                    System.out.println(arr[j]+" occurs "+temp2+" times");
                    temp2=1;
                }else{
                    temp2++;
                }
            }else{
                if(arr[j]==arr[j+1]){
                    temp2++;
                    System.out.println(arr[j]+" occurs "+temp2+" times");
                }else{
                    System.out.println(arr[j+1]+" occurs "+temp2+" times");
                }
            }
            
        }
    }
    /*
    打印数组
     */
    public static void show(int[] arr){
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

 

import java.util.Scanner;
class Demo02{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter ten number:");
        int[] arr1=new int[10];
        int[] arr2=new int[10];//备用
        int count1=0,count2=1,count3=0;
        while(true){
            int num=input.nextInt();
            arr1[count1++]=num;
            if(count1==10){
                break;
            }
        }
        /*
        遍历数组arr1
         */    
        for(int i=0;i<count1;i++){
            if(i==0){
                arr2[count3++]=arr1[i];
            }else{
                for(int j=0;j<count3;j++){
                    if(arr1[i]==arr2[j]){
                        count2++;
                    }
                }
                if(count2==1){
                    arr2[count3++]=arr1[i];
                }
                count2=1;
            }
            
        }
        System.out.println("The number of distinct number:"+count3);
        System.out.print("The distinct numbers are:");
        for(int i=0;i<count3;i++){
            System.out.print(arr2[i]+" ");
        }
        System.out.println();
    }
}

 

import java.util.Scanner;
class Demo04{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a balls:");
        int balls=input.nextInt();
        System.out.print("Enter a slots:");
        int slots=input.nextInt();
        int[] slot=new int[slots];
        String str="";
        //int count=0;
        for(int i=1;i<=balls;i++){
            str=random(slots);
            System.out.println(str);
            slot[getCount(str)]++;
        }
        for(int i=0;i<slot.length;i++){
            System.out.print(slot[i]+" ");
        }
    }
    public static String random(int slots){
        String str="";
        for(int j=0;j<slots-1;j++){
            if((int)(Math.random()*2)==0){
                str=str+"L";
            }else{
                str=str+"R";
            }
        }
        return str;
    }
    public static int getCount(String str){
        int count=0;
        for(int l=0;l<str.length();l++){
            if(str.charAt(l)=='R'){
                count++;
            }
        }
        return count;
    }
}

 

import java.util.Scanner;
class Demo08{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter strings:");
        String[] str=new String[0];
        String word="";
        int count1=0,count2=0;
        while(true){
            String str1=input.next();
            /*
            记录输入字符的个数
             */
            count1++;
            str=copyOf(str);
            str[str.length-1]=str1;
            if(count1==4){
                break;
            }
        }
        word=str[(int)(Math.random()*str.length)];
        //存放密文
        char[] ciphertext=new char[word.length()];
        //将密文转化为字符串
        String string="",string1="";
        string1=word;
        for(int i=0;i<word.length();i++){
            ciphertext[i]='*';
        }
        while(true){
            while(true){
                System.out.print("Enter a letter in word:");
                for(int i=0;i<ciphertext.length;i++){
                    System.out.print(ciphertext[i]);
                }
                System.out.print("->");
                //存放用户猜测的字符
                char c=input.next().charAt(0);
                if(isExisten(ciphertext,c)){
                    System.out.println(c+" is already in the word");
                }else{
                    /*
                    判断该字符是否出现
                    */
                    if(!isExistence(c,word,ciphertext)){
                        /*
                        记录输入错误的次数
                        */
                        count2++;
                        System.out.println(c+" is not already in the word ");
                    }
                }
                for(int i=0;i<ciphertext.length;i++){
                    string+=ciphertext[i];
                }
                /*
                是否结束
                */
                if(string.equals(word)){
                    break;
                }
                string="";
            }
            /*
            重新赋值为空字符串
             */
            string=bulid();
            System.out.println("The word is program,You missed "+count2+" times");
            System.out.print("Do you want to guess another word?Enter y or n:");
            char inputChar=input.next().charAt(0);
            if(inputChar=='y'){
                word=str[(int)(Math.random()*str.length)];
                ciphertext=printText(word);
                for(int i=0;i<word.length();i++){
                    ciphertext[i]='*';
                }
                count2=0;
            }else{
                break;
            }
        }
    }
    /*
    重新创建一个空串
     */
    public static String bulid(){
        String string="";
        return string;
    }
    /*
    密文更新
     */
    public static char[] printText(String str){
        char[] newCiphertext=new char[str.length()];
        return newCiphertext;
    }
    /*
    数组的扩容
     */
    public static String[] copyOf(String[] str){
        String[] newStr=new String[str.length+1];
        for(int i=0;i<str.length;i++){
            newStr[i]=str[i];
        }
        return newStr;
    }
    /*
    数组的输出
     */
    public static void show(String[] str){
        for(int i=0;i<str.length;i++){
            System.out.print(str[i]+" ");
        }
        System.out.println();
    }
    /*
    判断该字符是否出现
     */
    public static boolean isExistence(char c,String word,char[] ciphertext){
        int count=0;
        char[] arrC=word.toCharArray();
        for(int i=0;i<arrC.length;i++){
            if(arrC[i]==c){
                ciphertext[i]=c;
                count++;
            }
        }
        if(count>=1){
            return true;
        }else{
            return false;
        }
    }
    /*
    遍历密文中是否存在该字符
     */
    public static boolean isExisten(char[] ciphertext,char c){
        int count=0;
        for(int i=0;i<ciphertext.length;i++){
            if(ciphertext[i]==c){
                count++;
            }
        }
        if(count>=1){
            return true;
        }else{
            return false;
        }
    }
}

运行结果:

Enter strings:write that program take
Enter a letter in word:*****->w
Enter a letter in word:w****->t
Enter a letter in word:w**t*->r
Enter a letter in word:wr*t*->i
Enter a letter in word:writ*->e
The word is program,You missed 0 times
Do you want to guess another word?Enter y or n:y
Enter a letter in word:*******->p
Enter a letter in word:p******->r
Enter a letter in word:pr**r**->p
p is already in the word
Enter a letter in word:pr**r**->o
Enter a letter in word:pro*r**->g
Enter a letter in word:progr**->n
n is not already in the word
Enter a letter in word:progr**->m
Enter a letter in word:progr*m->a
The word is program,You missed 1 times
Do you want to guess another word?Enter y or n:n

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言数组题目及答案 题目:求一个整型数组中的最大值和最小值,并计算平均值。 解答:可以通过遍历整型数组来求解最大值、最小值和平均值。 首先,定义一个整型数组,并赋值。假设数组名为arr,长度为n。 然后,初始化最大值和最小值为数组中的第一个元素arr[0],并初始化累加和sum为0。 接下来,使用for循环从数组的第二个元素(arr[1])开始遍历数组: - 如果当前元素arr[i]大于最大值max,则更新最大值为arr[i]; - 如果当前元素arr[i]小于最小值min,则更新最小值为arr[i]; - 将当前元素arr[i]累加到sum中。 循环结束后,最大值、最小值和sum的值就得到了。 最后,通过除以n求得数组的平均值avg,即avg = sum / n。 代码示例: #include <stdio.h> int main() { int arr[] = {5, 2, 8, 1, 9}; // 定义整型数组并赋值 int n = sizeof(arr) / sizeof(arr[0]); // 数组长度 int max = arr[0]; // 初始化最大值 int min = arr[0]; // 初始化最小值 int sum = 0; // 初始化累加和 float avg = 0; // 初始化平均值 for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; // 更新最大值 } if (arr[i] < min) { min = arr[i]; // 更新最小值 } sum += arr[i]; // 累加 } avg = (float)sum / n; // 计算平均值 printf("最大值:%d\n", max); printf("最小值:%d\n", min); printf("平均值:%f\n", avg); return 0; } 运行以上代码,输出结果为: 最大值:9 最小值:1 平均值:5.000000 这样,我们就可以通过遍历数组来求解最大值、最小值和平均值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值