1001 && 1002

1001 Hello,World!

题目如下:

This is the first problem for test.

Since all we know the ASCII code, your job is simple: Input numbers and output corresponding messages.

Input

The input will contain a list of positive integers separated by whitespaces(spaces, newlines, TABs).
Please process to the end of file (EOF).
The integers will be no less than 32.

Output

Output the corresponding message.
Note there is NOT a newline character in the end of output.

Sample Input

72 101 108 108 111 44
32 119 111 114 108 100 33

Sample Output

Hello, world!

解题思路:

输入int类型数字,强制转换为char类型,然后输出。

AC代码

#include <iostream>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        cout << (char)n;
    }
}

1002 Maya Calendar

题目如下:

During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, professor discovered that the Maya civilization used a 365 day long year, called Haab, which had 19 months. Each of the first 18 months was 20 days long, and the names of the months were pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu. Instead of having names, the days of the months were denoted by numbers starting from 0 to 19. The last month of Haab was called uayet and had 5 days denoted by numbers 0, 1, 2, 3, 4. The Maya believed that this month was unlucky, the court of justice was not in session, the trade stopped, people did not even sweep the floor.

For religious purposes, the Maya used another calendar in which the year was called Tzolkin (holly year). The year was divided into thirteen periods, each 20 days long. Each day was denoted by a pair consisting of a number and the name of the day. They used 20 names: imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau and 13 numbers; both in cycles.

Notice that each day has an unambiguous description. For example, at the beginning of the year the days were described as follows:

1 imix, 2 ik, 3 akbal, 4 kan, 5 chicchan, 6 cimi, 7 manik, 8 lamat, 9 muluk, 10 ok, 11 chuen, 12 eb, 13 ben, 1 ix, 2 mem, 3 cib, 4 caban, 5 eznab, 6 canac, 7 ahau, and again in the next period 8 imix, 9 ik, 10 akbal …

Years (both Haab and Tzolkin) were denoted by numbers 0, 1, … , where the number 0 was the beginning of the world. Thus, the first day was:
•Haab: 0. pop 0

•Tzolkin: 1 imix 0
Help professor M. A. Ya and write a program for him to convert the dates from the Haab calendar to the Tzolkin calendar.

Input

The date in Haab is given in the following format:
NumberOfTheDay. Month Year

The first line of the input contains the number of the input dates in the input. The next n lines contain n dates in the Haab calendar format, each in separate line. The year is smaller then 5000.

Output

The date in Tzolkin should be in the following format:
Number NameOfTheDay Year

The first line of the output contains the number of the output dates. In the next n lines, there are dates in the Tzolkin calendar format, in the order corresponding to the input dates.

Sample Input

3
10. zac 0
0. pop 0
10. zac 1995

Sample Output

3
3 chuen 0
1 imix 0
9 cimi 2801

解题思路:

  • 本题是根据所输入Haab的年、月的名称、天,输出Tzolkin的年、天的名称以及对应的数字
  • 首先要根据输入的Haab月的名称找到对应的月份数字
  • 然后用公式:days = Haab年*365+(月份数字 - 1)*20+天,算出公有多少天
  • 用days/260算出对应的Tzolkin的年
  • 用moddays = days%260算出对应的Tzolkin算出余数天
  • 用moddays%20算出是第几天,对应出天的名称
  • 用moddays%13+1算出天对应的数字

AC代码:

#include <iostream>
#include <string>
using namespace std;

string monthhaab [19] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"};

string nameofthedaytzolkin [20] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik","lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem","cib", "caban", "eznab", "canac", "ahau"};

//取得输入的monthhaab对应的数值
int getmonthhaab(string Month)
{
    string m(Month);
    for(int j = 0;j < 19; j++)
    {
        if(m == monthhaab[j]) return j;
    }
}

int main()
{
    int n;
    cin >> n;
    int NumberOfTheDay;
    char ch;
    string Month;
    int YearHaab;

    cout << n <<endl;

    while(n--)
    {
        cin >> NumberOfTheDay >> ch >> Month >> YearHaab;
        int days = YearHaab * 365 + getmonthhaab(Month) * 20
                   + NumberOfTheDay;
        int YearTzolkin = days / 260;
        int modyeartzolkin = days % 260;
        int numnameofthedaytzoklin = modyeartzolkin % 20;
        int Number = modyeartzolkin % 13 +1;
        cout << Number << " " <<                           
                nameofthedaytzolkin[numnameofthedaytzoklin]
                << " " << YearTzolkin << endl;
    }
    return 0;
}
<think>好的,我需要回答用户关于C++结构体引用变量定义与使用的问题,特别是结构体名后加&的含义或用法。首先,用户提到“结构体名& a”,这里的&在C++中通常表示引用。我需要先确认引用的基本概念,然后结合结构体的使用来具体说明。 根据提供的引用资料,特别是引用[2]提到结构体指针和结构体变量的区别和应用,而引用[4]提到结构体变量不能整体输入输出,只能操作成员。这可能涉及到结构体作为函数参数传递时的效率问题,使用引用可以避免复制整个结构体,提高效率。 首先,结构体变量的引用定义,比如在函数参数中使用结构体引用。例如,void func(Student &s)中的&表示s是一个引用,指向传入的结构体变量,这样在函数内部修改s会影响原变量。而如果不用引用,传递的是副本,修改不会影响原变量。 接下来,引用变量的初始化必须在定义时进行,不能像指针那样先声明再指向。例如,Student s1; Student &ref = s1; 这样ref就是s1的别名,操作ref等同于操作s1。 然后,需要比较结构体引用和指针的区别。引用更安全,不需要解引用,语法更简洁。指针则可以重新指向其他变量,而引用一旦绑定不能更改。 可能还需要提到结构体引用在函数返回值中的应用,比如返回引用可以避免拷贝,但需要注意不能返回局部变量的引用。 结合用户的问题,可能需要给出代码示例,比如定义结构体,然后创建引用变量,展示如何通过引用修改成员的值。例如: struct Student { int num; char sex; int age; }; Student s = {1001, 'M', 24}; Student &ref = s; ref.age = 25; // 修改原结构体的age成员 这样用户可以看到引用变量的实际用法。同时,需要说明引用的限制,比如必须在初始化时绑定,不能为NULL等。 最后,根据用户要求生成相关问题,可能需要包括引用与指针的区别、引用的使用场景、如何避免引用导致的副作用等。 需要确保回答中的代码符合引用中的示例,比如引用[1]中的结构体定义,引用[3]中的成员赋值方式。同时,按照用户的系统级指令,数学公式用$...$,独立公式用$$...$$,但这里可能不需要数学公式,主要涉及代码示例和结构体语法。 总结回答结构:先解释引用的基本概念,再说明在结构体中的应用,代码示例,与指针的对比,使用注意事项,最后生成相关问题。</think>### C++结构体引用变量定义与使用详解 在C++中,**结构体引用变量**是通过在结构体名称后添加`&`符号定义的,表示对已存在结构体变量的别名引用。引用本质上是原变量的别名,操作引用等同于直接操作原变量。 --- #### 一、结构体引用变量的定义 1. **基本语法** ```cpp 结构体类型 &引用名 = 原变量名; ``` **示例**: ```cpp struct Student { int num; char sex; int age; } student1 = {1001, 'M', 24}; // 定义结构体变量并初始化[^1] Student &ref_student = student1; // 定义引用变量 ``` - 引用必须在定义时初始化,且不能重新绑定到其他变量。 - 通过引用修改变量成员会直接影响原变量。 2. **函数参数中的引用** 在函数传递结构体时,使用引用可避免拷贝开销: ```cpp void modify_age(Student &s) { s.age += 1; // 直接修改原结构体成员 } ``` --- #### 二、结构体引用的核心特性 1. **别名特性** 引用变量与原变量共享内存地址,例如: ```cpp student1.age = 24; ref_student.age = 25; // 此时student1.age也被修改为25 ``` 2. **与指针的区别** - **语法简化**:引用无需解引用运算符`->`,直接使用`.`访问成员。 - **安全性**:引用不能为`NULL`,且初始化后不可更改绑定对象[^2]。 --- #### 三、典型应用场景 1. **函数参数传递** 避免大结构体的复制开销: ```cpp void print_student(const Student &s) { // 使用const引用防止误修改 cout << "学号:" << s.num << " 年龄:" << s.age; } ``` 2. **返回值优化** 返回引用可避免拷贝,但需确保返回对象生命周期有效: ```cpp Student& get_student() { static Student s; // 静态变量生命周期持续到程序结束 return s; } ``` --- #### 四、注意事项 1. **不可绑定临时变量** ```cpp Student &ref = Student{1002, 'F', 20}; // 错误!临时变量会销毁 ``` 2. **引用与指针的转换** 可通过取地址操作获取引用的指针: ```cpp Student *ptr = &ref_student; // ptr指向student1的地址 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值