面试实例题

题目

Java

java 复制代码
package day4;

import java.time.LocalDateTime;
import java.time.Duration;

public class demo2 {
    public static void main(String[] args) {
        LocalDateTime freezeStart = LocalDateTime.of(2025, 6, 5, 14, 24, 50);
        LocalDateTime freezeEnd = freezeStart.plusDays(7);

        while (LocalDateTime.now().isBefore(freezeEnd)) {
            Duration duration = Duration.between(LocalDateTime.now(), freezeEnd);
            long days = duration.toDays();
            long hours = duration.minusDays(days).toHours();
            long minutes = duration.minusDays(days).minusHours(hours).toMinutes();
            long seconds = duration.minusDays(days).minusHours(hours).minusMinutes(minutes).getSeconds();

            System.out.printf("您的卡已被冻结,解冻时间是 %d 天 %d 小时 %d 分 %d 秒%n", days, hours, minutes, seconds);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
java 复制代码
import java.util.LinkedList;

class Stock {
    String name;
    String code;
    String increaseRate;
    String riseFall;

    public Stock(String name, String code, String increaseRate, String riseFall) {
        this.name = name;
        this.code = code;
        this.increaseRate = increaseRate;
        this.riseFall = riseFall;
    }
}

public class StockDisplay {
    public static void main(String[] args) {
        LinkedList<Stock> stockList = new LinkedList<>();
        stockList.add(new Stock("三祥新材", "603663", "-1.12%", "-0.26"));
        stockList.add(new Stock("合众思壮", "002383", "-0.21%", "-0.02"));
        stockList.add(new Stock("东望时代", "600052", "-0.24%", "-0.01"));
        stockList.add(new Stock("成飞集成", "002190", "4.78%", "1.72"));
        stockList.add(new Stock("易德龙", "603380", "0.45%", "0.11"));

        for (Stock stock : stockList) {
            System.out.println("股票名称:" + stock.name);
            System.out.println("股票代码:" + stock.code);
            System.out.println("涨幅:" + stock.increaseRate);
            System.out.println("涨跌:" + stock.riseFall);
            System.out.println("------------------");
        }
    }
}

C语言

cpp 复制代码
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    char a[5];
    char b[3];
    char c[3];
    char d[3];
    char e[3];
    char f[3];
} DateTime;
typedef struct {
    DateTime g;
    int h;
    int i;
    int j;
} CardStatus;
void initCardStatus(CardStatus* k) {
    strcpy(k->g.a, "2025");
    strcpy(k->g.b, "06");
    strcpy(k->g.c, "05");
    strcpy(k->g.d, "14");
    strcpy(k->g.e, "24");
    strcpy(k->g.f, "50");
    k->h = 6 * 24 * 3600 + 23 * 3600 + 59 * 60 + 30;
    k->i = k->h;
    k->j = 1;
}
time_t dateTimeToTimeT(DateTime* l) {
    struct tm m = { 0 };
    m.tm_year = atoi(l->a) - 1900;
    m.tm_mon = atoi(l->b) - 1;
    m.tm_mday = atoi(l->c);
    m.tm_hour = atoi(l->d);
    m.tm_min = atoi(l->e);
    m.tm_sec = atoi(l->f);
    return mktime(&m);
}
void secondsToDHMS(int n, int* o, int* p, int* q, int* r) {
    *o = n / (24 * 3600);
    *p = (n % (24 * 3600)) / 3600;
    *q = (n % 3600) / 60;
    *r = n % 60;
}
void updateFreezeStatus(CardStatus* s) {
    if (s->i > 0) {
        s->i--;
    }
    else {
        s->j = 0;
    }
}
void displayRemainingTime(CardStatus* t) {
    int o, p, q, r;
    secondsToDHMS(t->i, &o, &p, &q, &r);
    system("cls");
    printf("您的卡已被冻结,剩余时间为 %d 天 %d 小时 %d 分 %d 秒\n",
        o, p, q, r);
}
int main() {
    CardStatus u;
    initCardStatus(&u);
    while (u.j) {
        displayRemainingTime(&u);
        sleep(1);
        updateFreezeStatus(&u);
    }
    printf("您的卡已解冻,可以正常使用。\n");
    return 0;
}
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Stock {
    char code[10];
    char name[20];
    float change_rate;
    float change_price;
    struct Stock* next; 
} Stock;
Stock* create_node(char* code, char* name, float rate, float price) {
    Stock* node = (Stock*)malloc(sizeof(Stock));
    strcpy(node->code, code);
    strcpy(node->name, name);
    node->change_rate = rate;
    node->change_price = price;
    node->next = NULL;
    return node;
}
void append_node(Stock** head, char* code, char* name, float rate, float price) {
    Stock* new_node = create_node(code, name, rate, price);
    if (*head == NULL) {
        *head = new_node;
        return;
    }
    Stock* current = *head;
    while (current->next != NULL) current = current->next;
    current->next = new_node;
}
void display_stocks(Stock* head, int page_size) {
    Stock* current = head;
    int count = 0;
    printf("%-10s %-15s %-10s %-10s\n", "代码", "名称", "涨幅", "涨跌");
    printf("----------------------------------------\n");

    while (current != NULL && count < page_size) {
        printf("%-10s %-15s %-10.2f%% %-10.2f\n",
               current->code, current->name,
               current->change_rate, current->change_price);
        current = current->next;
        count++;
    }
}
int main() {
    Stock* head = NULL;
    append_node(&head, "603663", "三祥新材", -1.12, -0.26);
    append_node(&head, "002383", "合众思壮", -0.21, -0.02);
    append_node(&head, "600052", "东望时代", -0.24, -0.01);
    append_node(&head, "002190", "成飞集成", 4.78, 1.72);
    append_node(&head, "603380", "易德龙", 0.45, 0.11);
    display_stocks(head, 5);
    Stock* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    return 0; 
}
}
相关推荐
fouryears_2341737 分钟前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~1 小时前
C#---StopWatch类
开发语言·c#
桦说编程2 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen2 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研2 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员3 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋4 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cui__OaO4 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
阿华的代码王国4 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~4 小时前
《设计模式》装饰模式
java·设计模式