145 条题解
-
-3
这里估计大部分都是写了c++有一定基础的,那么我就用链表实现a+b问题吧
#include <iostream> struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode *dummy = new ListNode(0); ListNode *current = dummy; int carry = 0; while (l1 || l2 || carry) { int sum = carry; if (l1) { sum += l1->val; l1 = l1->next; } if (l2) { sum += l2->val; l2 = l2->next; } carry = sum / 10; sum = sum % 10; current->next = new ListNode(sum); current = current->next; } return dummy->next; } ListNode *createLinkedList(int arr[], int n) { if (n == 0) { return nullptr; } ListNode *head = new ListNode(arr[0]); ListNode *current = head; for (int i = 1; i < n; i++) { current->next = new ListNode(arr[i]); current = current->next; } return head; } void printLinkedList(ListNode *head) { ListNode *current = head; while (current) { std::cout << current->val << " "; current = current->next; } std::cout << std::endl; } int main() { int arr1[1]; int arr2[1]; std::cin >> arr1[0]; std::cin >> arr2[0]; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); ListNode *l1 = createLinkedList(arr1, n1); ListNode *l2 = createLinkedList(arr2, n2); ListNode *sum = addTwoNumbers(l1, l2); printLinkedList(sum); return 0; }
-
-3
A+B难度居然有1c++风格#include<bits/stdc++.h>//好习惯 int main(){//主函数 std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);//可要可不要,是用来加速cin cout的,相关信息可以百度 int a,b; std::cin>>>b;//因为cin是在std空间定义,我不写using就要在前面加std:: std::cout<<a+b;//建议还是加using namespace std,不然不太方便 }
c风格
#include<stdio.h> int main(){ int a,b; scanf("%d%d",&a,&b);//%d是声明变量类型,&a是返回变量地址,直接变量名会出错 printf("%d",a+b);//%d同上,a+b是计算 //注意:c语言中没有using namespace std }
其他不会 -
-3
package luogu; import java.util.Scanner; public class a1 {
public static void main(String[] args) { // TODO Auto-generated method stub int a=0; int b=0; Scanner sc=new Scanner(System.in); for(int i=1;i<=2;i++) { System.out.print("\t"); a=sc.nextInt(); b=sc.nextInt(); int c=a+b; System.out.println(c); } }
}
-
-3
Dijkstra+STL的优先队列优化。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cctype> #include <climits> #include <algorithm> #include <map> #include <queue> #include <vector> #include <ctime> #include <string> #include <cstring> using namespace std; const int N=405; struct Edge { int v,w; }; vector<Edge> edge[N*N]; int n; int dis[N*N]; bool vis[N*N]; struct cmp { bool operator()(int a,int b) { return dis[a]>dis[b]; } }; int Dijkstra(int start,int end) { priority_queue<int,vector<int>,cmp> dijQue; memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); dijQue.push(start); dis[start]=0; while(!dijQue.empty()) { int u=dijQue.top(); dijQue.pop(); vis[u]=0; if(u==end) break; for(int i=0; i<edge[u].size(); i++) { int v=edge[u][i].v; if(dis[v]==-1 || dis[v]>dis[u]+edge[u][i].w) { dis[v]=dis[u]+edge[u][i].w; if(!vis[v]) { vis[v]=true; dijQue.push(v); } } } } return dis[end]; } int main() { int a,b; scanf("%d%d",&a,&b); Edge Qpush; Qpush.v=1; Qpush.w=a; edge[0].push_back(Qpush); Qpush.v=2; Qpush.w=b; edge[1].push_back(Qpush); printf("%d",Dijkstra(0,2)); return 0; }
-
-3
甚至连变量都不用的快读。
代码如下:
#include<bits/stdc++.h> using namespace std; inline int read() { int x=0; bool flag=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') flag=0; c=getchar(); } while(c>='0'&&c<='9') { x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return (flag?x:~(x-1)); } int main() { cout<<read()+read(); return 0; }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 9107
- 已通过
- 4056
- 上传者