Huazhong University of Science and Technology SPOC Programming Questions Chapter 6

1 The calculation of the identification code in the book ISBN number (35 points)
title content:

  给出一个不包含识别码的 ISBN 号码(前 11 位),计算其识别码,并输出完整的 ISBN 号码。

  每一本正式出版的图书都有一个 ISBN 号码与之对应,ISBN 码包括 9 位数字、1 位识别码和 3 位分隔符,其规定格式如:“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如“0-670-82162-4”就是一个标准的 ISBN 码。

  ISBN 码的首位数字表示书籍的出版语言,例如,符号“0”代表英语;第一个分隔符“-”之后的三位数字代表出版社,第二个分隔符后的五位数字“82162”代表该书在该出版社的编号;最后一位识别码的计算方法:

  1、第 1 位数字乘以 1,加上第 2 位数字乘以 2,再加上第 3 位数字乘以 3,……,以此类推;

  2、再把所得结果对 11 求余,所得的余数即为识别码,如果余数为 10,则识别码为大写字母 X。

Code: (write function ISBN, perfect the following code)

#include

#include

using namespace std;

int main ()

{

char charISBN11[12], *charISBN;

cin>>charISBN11;

charISBN=ISBN(charISBN11);

cout<<charISBN<<endl;

delete charISBN;

return 0;

}

Input format:

ISBN number of the book (first 11 digits)

Output format:

ISBN number of the complete book (13 digits)

Input sample:

0-670-82162

Sample output:

0-670-82162-4

Time limit: 500ms Memory limit: 32000kb


This was made by my roommate Viagra. I watched him do it, and soon, he started to fuck, and in just a few seconds, he carried the package of code and came out. . .
Is this the horror of programming? It's clearly written, why is mine not good? . .


This is Viagra qq 1596892709,
xdm can consult how to solve the problem in a few seconds


#include <iostream>

#include <cstring>

using namespace std;
char z[14] = {
    
     0 };
char* ISBN(char* x)
{
    
    
    int num = 0;
    char y[12] = {
    
     0 };
    for (int i = 0; i <= 11; i++)
    {
    
    
        if (x[i] >= '0' && x[i] <= '9')
        {
    
    
            y[num] = x[i];
            num++;
        }
    }
    int goal = 0;
    for (int i = 0; i <= 8; i++)
    {
    
    
        goal += (i + 1) * (y[i]-'0');
        goal %= 11;
    }
    char goal0 = goal + '0';
    for (int i = 0; i <= 10; i++)
    {
    
    
        z[i] = x[i];
    }
    z[11] = '-';
    if (goal == 10)
    {
    
    
        z[12] = 'X';
    }
    else
        z[12] = goal0;
    z[13] = '\0';
    return z;
    
}
int main()
{
    
    
    char charISBN11[12], * charISBN;
    cin >> charISBN11;
    charISBN = ISBN(charISBN11);
    cout << charISBN << endl;
    return 0;
}

2 The longest word (35 points)
subject content:

Find the longest word in the string (if there are multiple longest words, the leftmost word shall prevail), and output the word.

Code: (write function fun, perfect the following code)

#include

using namespace std;

int main ()

{

 char c1[100];

 char c2[100];

 cin.getline(c1,100);

 fun(c1,c2);

 cout<<c2<<endl;

 return 0;

}

Input format:

An English sentence

Output format:

The longest word in an English sentence

Input sample:

A Grain of Sand William Blake

Sample output:

William

Time limit: 500ms Memory limit: 32000kb

#include<iostream>
#include<cstring>
using namespace std;
void fun(char c1[],char c2[])
{
    
    int len1=0,len2=0,pl=0;
for(int i=0;i<strlen(c1);i++)
{
    
    

if(c1[i]==' '||c1[i]=='\0')
len1=0;
else len1++;
if(len1>len2) {
    
    len2=len1;pl=i-len2+1;}
}int j=0;
for(int i=pl;i<pl+len2;i++)
c2[j++]=c1[i];
c2[j]='\0';
}
int main()

{
    
    

     char c1[100];

     char c2[100];

     cin.getline(c1,100);

     fun(c1,c2);

     cout<<c2<<endl;

     return 0;

}


3 Orderly insertion of data (30 points)
Question content:

   插入一个数据到一个有序的一维数组中,要求插入后,该数组任然保持原有顺序。

Code:

#include

using namespace std;

int main ()

{

int data[11]={12,34,56,78,90,167,258,389,945,1890};

int num;

cin>>num;

FunSort(data,10,num);

int i;

cout<<data[0];

for(i=1;i<11;i++)

    cout<<" "<<data[i];

cout<<endl;

return 0;

}

Input format:

An integer

Output format:

After the data is inserted, all the data stored in the array is output, separated by a space.

Input sample:

57

Sample output:

12 34 56 57 78 90 167 258 389 945 1890

Time limit: 500ms Memory limit: 32000kb

#include<iostream>
using namespace std;
void FunSort(int data[],int n,int num)
{
    
    
for(int i=n;i>0;i--)
{
    
    if(num>data[i-1]) {
    
    data[i]=num;break;}
else data[i]=data[i-1];
}
while(data[1]==data[0])
data[0]=num;
}
int main()

{
    
    

    int data[11]={
    
    12,34,56,78,90,167,258,389,945,1890};

    int num;

    cin>>num;

    FunSort(data,10,num);

    int i;

    cout<<data[0];

    for(i=1;i<11;i++)

        cout<<" "<<data[i];

    cout<<endl;

    return 0;

}


//The following is the first time I wrote it. As a result, the runtime example 1 timed out. . .

void FunSort(int data[], int n, int num)
{
    
    
	int flag = 1;
	for (int i = n; i > 0; i--)
		{
    
     if (num > data[i - 1]) {
    
     data[i] = num; flag = 0; break; }
		else data[i] = data[i - 1];
		}
		while (flag)
			data[0] = num;
}

Chinese language test tomorrow,
so play tonight.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51236357/article/details/112093056