1000. ISBN

/* 题目意思是输入每个ISBN码的前11位,根据
权重乘每个数位的和与11的倍数还差多少确定最后一位 */ 
#include<iostream>
#include<string>

using namespace std;

int main() {
    string s;
    // 以EOF结束输入 
    while(cin>> s) {
        int sum= 0;
        int l= s.size();
        int weight= 10;
        for (int i= 0; i< l; i++) {
            if (s[i]!= '-') {
                int num= s[i]- '0';
                sum+= weight* num;
                weight--;
            }
        }
        int check;
        // 值得注意的是sum%11== 0的情况 
        if (sum% 11== 0) {
            check= 0;
        } else {
            check= 11- (sum% 11);
        }
        if (check== 10) {
            cout<< s<< "-X";
        } else {
            cout<< s<< "-"<< check;
        }
        cout<< endl;
    }
}

#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXSIZE 1000 typedef struct { char isbn[14]; char name[50]; double price; int year; } BOOK; typedef struct { BOOK books[MAXSIZE]; int length; } sqlist; void Initlist(sqlist* L) { L->length = 0; } void insertbook(sqlist* L, int pos, BOOK book) { int i; if (pos < 1 || pos > L->length + 1 || L->length >= MAXSIZE) { printf("插入位置无效\n"); return; } for (i = L->length; i >= pos; i--) L->books[i] = L->books[i - 1]; L->books[pos - 1] = book; L->length++; } void deletebook(sqlist* L, int pos) { int i; if (pos < 1 || pos > L->length) { printf("删除位置无效\n"); return; } for (i = pos; i < L->length; i++) L->books[i - 1] = L->books[i]; L->length--; } int findmaxprice(sqlist L) { int i; if (L.length == 0) return -1; int max = 0; for (i = 1; i < L.length; i++) { if (L.books[i].price > L.books[max].price) max = i; } return max; } int findoldestyear(sqlist L) { int i; if (L.length == 0) return -1; int oldest = 0; for (i = 1; i < L.length; i++) { if (L.books[i].year < L.books[oldest].year) oldest = i; } return oldest; } void printbook(BOOK book) { printf("%s %s %.2f %d\n", book.isbn, book.name, book.price, book.year); } void printbooks(sqlist L) { int i; for (i = 0; i < L.length; i++) { printbook(L.books[i]); } printf("\n"); } int main() { int i; sqlist L; BOOK books[5]; // 先声明数组 BOOK newBook; // 分别初始化每个BOOK元素 strcpy(books[0].isbn, "9787109258549"); strcpy(books[0].name, "C Language Programming"); books[0].price = 39.80; books[0].year = 2019; strcpy(books[1].isbn, "9787115576668"); strcpy(books[1].name, "Data Structures in C"); books[1].price = 49.80; books[1].year = 2022; strcpy(books[2].isbn, "9787213052545"); strcpy(books[2].name, "Big Data"); books[2].price = 49.90; books[2].year = 2013; strcpy(books[3].isbn, "9787030726858"); strcpy(books[3].name, "Internet of Things"); books[3].price = 49.00; books[3].year = 2022; strcpy(books[4].isbn, "9787115623430"); strcpy(books[4].name, "Artificial Intelligence"); books[4].price = 119.00; books[4].year = 2023; int n = 5; // 直接指定数组大小 Initlist(&L); for (i = 0; i < n; i++) insertbook(&L, i + 1, books[i]); printf("初始图书信息:\n"); printbooks(L); int maxprice = findmaxprice(L); if (maxprice != -1) { printf("价格最高的书: "); printbook(L.books[maxprice]); printf("\n"); } int oldestyear = findoldestyear(L); if (oldestyear != -1) { printf("删除年份最久的图书:\n"); printbook(L.books[oldestyear]); deletebook(&L, oldestyear + 1); printf("删除后:\n"); printbooks(L); } // 初始化新书 strcpy(newBook.isbn, "9787111639848"); strcpy(newBook.name, "Modern Robotics"); newBook.price = 139.00; newBook.year = 2020; insertbook(&L, 2, newBook); printf("插入新图书后:\n"); printbooks(L); return 0; }检查哪有c2143问题
09-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值