175 条题解
-
-4
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); } }
}
-
-4
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; }
-
-4
这是一道基本的题,不会做这道题的都不好意思说自已学过OI
(雾)伪代码
导入头文件 主函数 { 整型 a, b 输入 a, b 输出 a, b }
真实代码
#include <bits/stdc++.h>// 万能头 int main()// 主函数 { int a, b;// 要加英文分号,int为整型 scanf("%d %d", &a, &b);// 输入,%d是整形输入,&一定要加!! printf("%d", a + b);// 输出,C/C++支持直接用运算符 return 0;// 返回,代表着程序的结束(注:这条代码只是习惯的问题,觉得麻烦可以不写;返回值一般是0,当然也可以返回别的数) }
-
-5
这里估计大部分都是写了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; }
-
-5
这是一道十分经典的题
重要的地方就在于输入输出
C++解法
#include <iostream> //引入输入输出流头文件 using namespace std; int mian() //主函数 { int a, b; //定义两个变量:a和b cin >> a >> b; //读入它们 cout << a + b; //输出他们的和 return 0; }
C解法
#include <stdio.h> //C语言的格式化输入输出头文件 int main() //主函数 { int a, b; //定义变量:a和b scanf("%d,%d", &a, &b); //这里运用了一个函数,他是scanf(格式化输入输出) //它的参数可以分成两大块,分别是格式和变量 //格式可以使任意的,在格式字符串中要用%d来代替一个整数型变量 //在后面的变量要和前面的代替符号(%d)一一对应,切记,变量前面需要加"&"(取址符) printf("%d", a + b); //这里又运用了一个和上面函数相对应的函数:printf(格式化输出) //同scanf,它前面是格式字符串,后面是对应的变量,但是,与scanf不同的是,后面的变量千万不要加"&" return 0; }
Pascal解法
var {这是pascal特有的变量定义域} a, b:longint; {定义两个长正型变量,这里注意不要用integer,会爆的哦} begin {pascal中程序开始关键字(也可以用于循环或分支中代表开始)} write(a + b); {输出他们的和} end. {pascal中程序结束的关键字(也可以用于循环或分支中代结束)}
珍爱生命杜绝抄袭(我不会告诉你我加了防抄袭)
-
-7
虽然这种简单题写个快读确实是大材小用,但是有了快读,这个程序的主程序就可如python一般简洁因此就有了这篇题解,大家就当学快读的写法吧qwq
#include <bits/stdc++.h> using namespace std; inline int read() { int X = 0; bool f = false; char ch = getchar(); while (ch > '9' || ch < '0') {f |= ch == '-'; ch = getchar();} while (ch <= '9' && ch >= '0') {X = (X << 3) + (X << 1) + (ch ^ 48); ch = getchar();} return f ? -X : X; } inline void write(int X) { if (X == 0) {putchar('0'); return;} if (X < 0) {putchar('-'); X = -X;} short num[21], cnt = 0; while (X) {num[++cnt] = X % 10; X /= 10;} while (cnt) putchar(num[cnt--] ^ 48); return; } int main() { write(read() + read()); return 0; }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 10304
- 已通过
- 4651
- 上传者