面试实例题

题目

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; 
}
}
相关推荐
程序员小假8 分钟前
线程池执行过程中遇到异常该怎么办?
java·后端
稚辉君.MCA_P8_Java13 分钟前
DeepSeek Java 单例模式详解
java·spring boot·微服务·单例模式·kubernetes
洛_尘21 分钟前
数据结构--4:栈和队列
java·数据结构·算法
疯癫的老码农27 分钟前
【小白入门docker】创建Spring Boot Hello World应用制作Docker镜像并运行
java·spring boot·分布式·docker·微服务
Mr.Entropy35 分钟前
Hibernate批量查询方法全面解析
java·后端·hibernate
绝顶少年1 小时前
Spring 框架中 RestTemplate 的使用方法
java·后端·spring
小趴菜82271 小时前
安卓人机验证View
android·java·前端
信安成长日记1 小时前
golang 写路由的时候要注意
开发语言·后端·golang
那个什么黑龙江1 小时前
关于C++中的“类中的特殊成员函数”
开发语言·c++
观望过往1 小时前
【Java SE 运算符】全面解析与实践指南
java