首页 > 中国系统笔试可能出现的编程题
头像
小明不刷算法
发布于 2021-05-20 20:27
+ 关注

中国系统笔试可能出现的编程题

中国系统

 

 

输入十六进制输出十进制

/* 

 *问题描述 

从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。 

  注:十六进制数中的10~15分别用大写的英文字母ABCDEF表示。 

 *样例输入 

    FFFF 

 *样例输出 

    65535 

*/  

import java.util.Scanner;  

public class Main {  

    public static void main(String[] args) {  

        Scanner in = new Scanner(System.in);  

        String hex_num = in.nextLine();  

        long dec_num = Long.parseLong(hex_num, 16);  

        System.out.println(dec_num);  

    }  

}  

 

 

 

 

输入一个数,计算他的位数并逆序输出

 

 

猴子吃桃,一个猴子,每天吃桃子的一半,再多吃一个。第十天桃子就剩一个了,问第一天桃子有多少个?

#include<stdio.h>

int main()

{

       int  a=1,b=1;

       for (int i = 9; i>0; i--)//天数递减(倒推计算)

       {   

              b =2*(b + 1);//计算每一天的吃桃数

              a = b + a;//计算吃桃数的总和

       }

       printf("10天共吃%d\n第一天吃%d", a,b);

       return 0

}

 

 

三线程循环打印,三线程从1开始循环打印,打印到75

public class sortedAlgorithm {

       

       static int type = 1;//线程对应的类型,三个线程分开处理

       static int number = 1;//打印数字的起始

       

       public static void main(String[] args) {

              new Thread("第一个线程:") { // 创建一个线程,命名为第一个线程

                     public void run() {

                            try {

                                   synchronized (sortedAlgorithm.class) {

                                          while (number <= 75) {                            

                                                 if (type == 1) {//type的值为1的时候,由第一个线程来打印

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

                                                               System.out.println(Thread.currentThread().getName() + ":" + number++);

                                                        }      

                                                        type = 2;//修改类型值

                                                        sortedAlgorithm.class.notifyAll();//唤醒其他进入休眠的线程

                                                 } else {

                                                        sortedAlgorithm.class.wait();

                                                 }

                                          }

                                   }

                            } catch (InterruptedException e) {

                                   e.printStackTrace();

                            }

                     };

              }.start();

              

              new Thread("第二个线程:") { // 创建二个线程,命名为第二个线程

                     public void run() {

                            try {

                                   synchronized (sortedAlgorithm.class) {

                                          while (number <= 75) {

                                                 if (type == 2) {

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

                                                               System.out.println(Thread.currentThread().getName() + ":" + number++);

                                                        }

                                                        type = 3;

                                                        sortedAlgorithm.class.notifyAll();

                                                 } else {

                                                        sortedAlgorithm.class.wait();

                                                 }

                                          }

                                   }

                            } catch (InterruptedException e) {

                                   e.printStackTrace();

                            }

                     };

              }.start();

              new Thread("第三个线程:") { // 创建三个线程,命名为第三个线程

                     public void run() {

                            try {

                                   synchronized (sortedAlgorithm.class) {

                                          while (number <= 75) {

                                                 if (type == 3) {

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

                                                               System.out.println(Thread.currentThread().getName() + ":" + number++);

                                                        }

                                                        type = 1;

                                                        sortedAlgorithm.class.notifyAll();

                                                        if (number < 76)

                                                        sortedAlgorithm.class.wait();

                                                 } else {

                                                        sortedAlgorithm.class.wait();

                                                 }

                                          }

                                   }

                            } catch (InterruptedException e) {

                                   e.printStackTrace();

                            }

                     };

              }.start();

       }

}

 

数组反转(只能java代码编写)

void invertUsingFor(Object[] array) {

    for (int i = 0; i < array.length / 2; i++) {

        Object temp = array[i];

        array[i] = array[array.length - 1 - i];

        array[array.length - 1 - i] = temp;

    }

}

 

 

有符号整数反转(任意语言)

public class ReverseNum {

 

    public static void main(String[] args) {

        int num = -1085774335;

        int c = 0;

        while (num != 0) {

            int temp = c;   //临时记录用于判断是否溢出

            c = c * 10 + num % 10;

            num /= 10;

            if(c/10 != temp) {  //如果c/10和临时记录并不相等,说明已经溢出了 结束循环

                c = 0;

                break;

            }

        }

        System.out.println(Long.MAX_VALUE+"反转后:" + c);

    }

}

 

 

 

 

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐