173 条题解
-
0
童鞋们你们好!
打开这道题,就相当于你们迈出了万里 OI 路的第一步。让我们重视这一步!
这里提醒同学们:好的代码风格是非常重要的。大家一定要养成良好的代码风格。
来看看我打的代码:
#include<bits/stdc++.h> //万能头 using namespace std; //使用std这个命名空间 int main() { int a, b; //1e6 int够用 cin >> a >> b; //cin = console+input 标准读入 cout << a + b; //cout = console+output 标准输出 return 0; //不要忘了一个华丽的结尾 }
最后想请童鞋们注意:强烈建议
cin
/cout
中<<
/>>
符号前后打一个空格!这样能让你的代码更加美观!这就是我们万里编程路的第一步。在以后的刷题中,大家会遇到更多的难题。希望大家能不忘初心,砥砺前行!
说歪了。感谢观看!
-
0
A+B Problem
题解+思路
题解
#include <iostream> using namespace std; struct Node { int data; Node *prev; Node *next; Node(int val) : data(val), prev(nullptr), next(nullptr) {} }; Node *createList(int num) { Node *head = nullptr; Node *tail = nullptr; while (num > 0) { int digit = num % 10; Node *newNode = new Node(digit); if (head == nullptr) { head = newNode; tail = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } num /= 10; } return head; } Node *addTwoNumbers(Node *num1, Node *num2) { Node *result = nullptr; Node *current = nullptr; int carry = 0; while (num1 != nullptr || num2 != nullptr || carry != 0) { int sum = carry; if (num1 != nullptr) { sum += num1->data; num1 = num1->next; } if (num2 != nullptr) { sum += num2->data; num2 = num2->next; } carry = sum / 10; sum %= 10; Node *newNode = new Node(sum); if (result == nullptr) { result = newNode; current = newNode; } else { current->prev = newNode; newNode->next = current; current = newNode; } } return result; } void printList(Node *head) { if (head == nullptr) { cout << "Empty list" << endl; return; } while (head != nullptr) { cout << head->data; head = head->next; } cout << endl; } void deleteList(Node *head) { while (head != nullptr) { Node *temp = head; head = head->next; delete temp; } } int main() { int num1 = 12345; int num2 = 6789; Node *list1 = createList(num1); Node *list2 = createList(num2); cout << "Number 1: "; printList(list1); cout << "Number 2: "; printList(list2); Node *sumList = addTwoNumbers(list1, list2); cout << "Sum: "; printList(sumList); deleteList(list1); deleteList(list2); deleteList(sumList); return 0; }
思路
- 首先,定义一个双向链表的节点结构体,包含一个整数值和两个指针,分别指向前一个节点和后一个节点。
- 创建两个双向链表,分别表示要相加的两个数字。可以通过遍历输入的整数,从个位开始,逐个创建节点并将其插入链表中。
- 定义一个变量来表示进位,初始值为0。
- 从链表的最后一个节点开始,逐位相加,并将结果存储在新的链表中。具体步骤如下: - 从两个链表的最后一个节点开始,分别取出对应的值,并加上进位。 - 将相加的结果对10取模得到当前位的值,同时更新进位为相加结果除以10的商。 - 创建一个新的节点,将当前位的值存储在该节点中,并将该节点插入到结果链表的头部。 - 分别将两个链表的指针指向上一个节点,继续下一位的相加操作。 - 如果其中一个链表已经遍历完,但另一个链表还有剩余位数,则将剩余位数与进位相加,并将结果插入到结果链表中
- 最后,得到的结果链表即为相加后的结果。
-
0
Python 压行技巧,详细讲一讲:
split
函数,可以将字符串按一定分隔符分割,放在一个list
中。例如,s
是'abc.abcd'
,那么s.split ('.')
就是['abc', 'abcd']
,如果没有参数,默认为' '
;map
函数,可以将一个序列依次进行某个操作的最终序列。例如,a
是[1, 1, 4, 5, 1, 4]
,func
函数定义如下:
那么def func (int x): return x + 1
map (func, a)
就是[2, 2, 5, 6, 2, 5]
。sum
函数,可以求一个序列的和。例如,按照上面的a
,那么sum (a)
就是16
。
最终代码(注释参考样例一,不含注释一行):
print(sum(map(int,input().split()))) #print(sum(map(int, '1 2' .split()))) #print(sum(map(int, ['1', '2'] ))) #print(sum( [1, 2] ))) #print( 2 )
-
0
使用 C++ 风格的输入输出
#include <bits/stdc++.h>//头文件,可以暂时理解为告诉编译器你要拿标准库中的函数来用 using namespace std; //如果不想引入整个 std 命名空间,你需要使用 int main() { //std::cin、std::cout、std::endl int a, b; //定义整形变量 a 和 b,它们可存储的整数范围约为 ±21 亿, cin >> a >> b; //对于本题的数据范围绰绰有余;从标准输入中读入两个值并赋给 a, b cout << a + b; //输出结果。你也可以先使用另一个变量存储结果,但在这里没什么意义 return 0; //返回 0 以外的值会让操作系统或在线判题系统认为你的程序异常退出 } //最后的 return 0; 也可以不写,编译器会自动帮你加上正常退出
使用 C 语言风格的输入输出
#include <bits/stdc++.h> using namespace std; int main() { int a, b; //%d 是整形的占位符 scanf("%d %d", &a, &b); //输入非字符串时你需要在变量名前面加上取地址符 & printf("%d", a + b); //输出时不要加 &,否则你的输出会是一个地址而不是你想要的答案 return 0; //其余的部分请看上面的注释 }
-
-1
文言文
由【键盘】入之甲、乙二整形元素。 以操作符【+】计算甲、乙之和。 存入另一整形元素丙。书之。
伪代码(PQ)
set keyboard as stdin set screen as stdout input int1024 a b c=a(operator +)b output c as int1024
伪代码(XD20)
这个在XD22中也可运行function main begin. stdin a stdin b stdout a+b end.
伪代码(XD22)
select keyboard as stdin select screen as stdout gnu -O2 -Wall function main begin. stdin a stdin b stdout a+b end.
伪代码(Z)
select keyboard as input select screen as output select windows11 as system select Z22 as language select ZS22 as compiler begin. read a read b write a+b end. flush.
Pascal
var a, b: longint; begin readln(a,b); writeln(a+b); end.
C++
#include<bits/stdc++.h> using namespace std; int fu=1,f=1,a,b,c=0; int main(){ cin>>a>>b; if(a<0&&b>0)fu=2; if(a>0&&b<0)fu=3; if(a<0&&b<0)f=-1; if(a==0){cout<<b;return 0;} if(b==0){cout<<a;return 0;} a=abs(a); b=abs(b); if(a>b&&fu==3)f=1; if(b>a&&fu==3)f=-1; if(b>a&&fu==2)f=1; if(b<a&&fu==2)f=-1; if(fu==1)c=a+b; if(fu>1)c=max(a,b)-min(a,b); c*=f; cout<<c; return 0; }
-
-1
曾经的我:
#include<iostream> using namespace std; int main (){ int a,b; cin>>a>>b; cout<<a+b; return 0; }
现在的我:
#include<iostream> #include<string> using namespace std; int n1[501]; int m1[501]; int result[501]; string n,m; int main(){ cin>>n>>m; int ns,ms; ns=n.size(); ms=m.size(); for(int i=ns-1,j=1;i>=0;i--,j++){ n1[j]=n[i]-'0'; } for(int i=ms-1,j=1;i>=0;i--,j++){ m1[j]=m[i]-'0'; } int j=ns>ms?ns:ms; for(int i=1;i<=j;i++){ result[i]+=m1[i]+n1[i]; result[i+1]=result[i]/10; result[i]=result[i]%10; } if(result[j+1]){ j++; } for(int i=j;i>=1;i--){ cout<<result[i]; } return 0; }
感觉没啥变化
-
-1
注:此代码不支持C++23(O2)。
#include<bits/stdc++.h> #define short long long #define int long long #define float long double #define double long double #define char wchar_t #define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) using namespace std; signed main(){ ios; int a,b;//定义a,b cin>>a>>b;//输入a,b cout<<a+b;//输出a+b return 0;//结束程序 }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 10198
- 已通过
- 4606
- 上传者