面试实例题

题目

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; 
}
}
相关推荐
Flittly11 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了11 小时前
Java 生成二维码解决方案
java·后端
人活一口气15 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP17 小时前
Vibe Coding -- 完整项目案例实操
java
荣码17 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
SimonKing17 小时前
Google第三方授权登录
java·后端·程序员
明月光81817 小时前
从一行 @Builder 说起:重新拾起 Java 的 Lombok、注解与 Builder 模式
java
考虑考虑1 天前
Mybatis实现批量插入
java·后端·mybatis
咖啡八杯1 天前
GoF设计模式——中介者模式
java·后端·spring·设计模式
青石路1 天前
记一次多JDK版本问题的排查,一坑套一坑,差点没爬上来
java