第一题
这个题目也是熟悉结构体的使用方法为主,有些同学可能以为这样
struct node{
int a=0;
int b=0;
int c=1;
c=a+b;
}
就可在结构体中达到令c=a+b的目的,实际上结构体这个概念和后面学习的类的概念更为相似,现阶段同学们只需要理解结构体内的语句也是需要在内部函数的作用域内才可以执行的,这块的使用方法类似于定义全局变量。if,else,赋值这些都不能正确执行,只能定义。
另外这题的做法也类似于我们平时练习过的特殊进制加法一样,分和秒的加法都是和对60取余留在本位,除以60加给上一位,时的基本一致,就是把60修改为24而已。
关于减法的话,其实题目也提示过了,直接转化为秒随后相减即可
#include<bits/stdc++.h>
using namespace std;
struct electronic_clock
{
int h;
int m;
int s;
void setTime(int hh,int mm,int ss)
{
h=hh;
m=mm;
s=ss;
}
void increase()
{
if (s!=59)
{
s++;
}
else
{
s=0;
if (m!=59)
{
m++;
}
else
{
m=0;
if (h!=23)
{
h++;
}
else
{
h=0;
}
}
}
}
bool less(electronic_clock &ec)
{
if (h<ec.h) return true;
else if (h>ec.h) return false;
else
if (m<ec.m) return true;
else if (m>ec.m) return false;
else
if (s<ec.s) return true;
else return false;
}
int delta_time(electronic_clock &ec)
{
electronic_clock t1,t2;
if (this->less(ec))
{
t1=*this;
t2=ec;
}
else
{
t1=ec;
t2=*this;
}
int delta=0;
while (t1.h!=t2.h||t1.m!=t2.m||t1.s!=t2.s)
{
t1.increase();
delta++;
}
return delta;
}
void showTime()
{
printf("%02d:%02d:%02d\n",h,m,s);
}
};
int main(){
int hh1,mm1,ss1,hh2,mm2,ss2;
cin>>hh1>>mm1>>ss1>>hh2>>mm2>>ss2;
electronic_clock time1,time2;
time1.setTime(hh1,mm1,ss1);
time2.setTime(hh2,mm2,ss2);
time1.showTime();
time2.showTime();
time1.increase();
time2.increase();
time1.showTime();
time2.showTime();
cout<<time1.delta_time(time2)<<endl;
}
第二题
这个题目化简的话使用gcd的思想计算一下即可,忘记的同学自己复习一下,处理这个就是有个测试集比较抽象(分子为0那个)的问题而且,就不浪费时间多说了
rationalNumber reduction(rationalNumber n)
{
// 请在这里补充代码,实现函数reduction
/********** Begin *********/
if(n.fenzi == 0)
{
n.fenmu = 1;
return n;
}
int num1 = abs(n.fenzi);
int num2 = abs(n.fenmu);
int big = max(num1,num2);
int small = min (num1,num2);
int left = big % small;
while(left != 0)
{
big = small;
small = left;
left = big % small;
}
n.fenzi /= small;
n.fenmu /= small;
return n;
/********** End **********/
}
第三题
这个题目算法上看没啥难度,简单的排序而已,就是有些同学的编译环境不支持中文的读取,(vscode的话)这个问题可以把launch.json
中那个 "externalConsole
改为ture,在命令行输入基本就可以读到中文了
#include <iostream>
#include <string>
using namespace std;
struct hero
{
string name;
int age;
string sex;
};
int main()
{
hero h;
hero hero_arr[5];
int len=5;
for (int i=0;i<len;i++)
{
cin>>hero_arr[i].name>>hero_arr[i].age>>hero_arr[i].sex;
}
/*补充代码*/
for(int i=5;i>0;i--)
{
for(int j=1;j<i;j++)
{
if(hero_arr[j-1].age>hero_arr[j].age)
{
h = hero_arr[j-1];
hero_arr[j-1] = hero_arr[j];
hero_arr[j] = h;
}
}
}
/*补充代码*/
for (int i = 0; i < len; i++)
{
cout << hero_arr[i].name << " " << hero_arr[i].age << " " << hero_arr[i].sex << endl;
}
return 0;
}
第四题
这个题目也是基础题,按照题目的要求将结构体中的函数补全即可
/********* Begin *********/
struct Complex
{
int real, imag;
// add 成员函数, 调用时 x.add(y) 结果等价于 x += y
void add(const Complex& rhs)
{
real += rhs.real;
imag += rhs.imag;
}
// times 成员函数,调用时 x.times(y) 结果等价于 x *= y
void times(const Complex& rhs)
{
int real_copy = real;
real = real * rhs.real - imag * rhs.imag;
imag = real_copy * rhs.imag + imag * rhs.real;
}
// 打印函数,成员函数,调用时 x.disp() 将打印 x 为 a + bi 的形式
void disp()
{
cout << noshowpos << real << showpos << imag << "i\n";
}
};
// add 函数,返回 lhs + rhs
Complex add(Complex lhs, const Complex& rhs)
{
lhs.add(rhs);
return lhs;
}
// times 函数,返回 lhs * rhs
Complex times(Complex lhs, const Complex& rhs)
{
lhs.times(rhs);
return lhs;
}
/********* End *********/
/********* 不需要修改 main 函数 *********/
int main(int argc, char const *argv[])
{
Complex x, y;
cin >> x.real >> x.imag >> y.real >> y.imag;
cout << "x = "; x.disp();
cout << "y = "; y.disp();
x.add(y); // x += y
cout << "x += y; x = ";
x.disp(); // 打印 x;
y.times(x); // y *= x;
cout << "y *= x; y = ";
y.disp(); // 打印 y;
cout << "x + y = ";
add(x, y).disp();
cout << "y * x = ";
times(y, x).disp();
cout << "x = "; x.disp();
cout << "y = "; y.disp();
return 0;
}
第五题
这个题目把思想弄明白就好,一个简单的推理,如果你在跑步并且和前面那个人差100米,当你们用一样的速度前进的时候,他到终点时,你离终点多远。一样的思路,要删除倒数第k个结点,就设置两个指针指向当前结点和当前结点后第k个结点即可,至于由于结点的删除的需要(需要知道前一个结点的地址),所以这块可能两个指针之间的差距是k
个结点而不是k-1
个结点,另外就是考虑整个链表只有k个结点的情况做下单独讨论即可
#include<bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
//这三句就是简答的初始化函数而已,允许代码用ListNode n();这样的方法申明变量。
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode* removeNode(ListNode* head, int n) {
ListNode* dummy = new ListNode(0, head);
ListNode* first = head;
ListNode* second = dummy;
for (int i = 0; i < n; ++i) {
first = first->next;
}
while (first) {
first = first->next;
second = second->next;
}
second->next = second->next->next;
ListNode* ans = dummy->next;
delete dummy;
return ans;
}
void print_linked_list(ListNode* head)
{
while (head->next)
{
cout<<head->val<<" ";
head=head->next;
}
cout<<head->val<<endl;
}
int main()
{
int x,n,k;
cin>>n;
ListNode* dummy = new ListNode(0);
ListNode* cur = dummy;
for (int i=0;i<n;i++)
{
cin>>x;
cur->next = new ListNode(x);
cur = cur -> next;
}
cin>>k;
auto new_head = removeNode(dummy->next,k);
print_linked_list(new_head);
}