oj

c++上机oj题目汇总(2018信通版)纯干货

我与影子孤独终老i 提交于 2020-01-16 02:12:39
学长学姐回忆版本1-5 题组一 题组二 题组三 题组四 题组五 #P.S. 以下内容纯自己复习时手打的,方法不一定为最简,题目个人理解的也不一定准确,只是单纯希望能够帮助到要上机考试的小伙伴,欢迎指出问题,大家共同进步~ (代码注释保留了当时的错误原因,经验想法与激动的心情,N个!这种忽略就好------ (感谢志协学长学姐提供的题目) 题组一 1.输出平方 2.输出十个正整数中的最小合数 3.从小到大输出一个五位数中各个数字 4.输入两个含五个元素的数组,并将他们和它们的合数组倒序输出。每个数字之间有一个空格,行末无空格。 5.雇员类 //一 //1.输出平方 void main ( ) { int n ; cin >> n ; cout << n * n ; } //2.输出十个正整数中的最小合数 #include < cmath > int judge ( int m ) { bool ju = false ; for ( int n = 2 ; n <= sqrt ( m ) + 1 ; n ++ ) { if ( ( m % n == 0 ) && m != 2 && m != 1 ) { ju = true ; break ; } } return ju ; } int main ( ) { int a [ 10 ] ; for ( int k = 0 ; k < 10

力扣OJ 143. 重排链表

你说的曾经没有我的故事 提交于 2020-01-14 12:22:47
题目: 给定一个单链表 L:L0→L1→…→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例 1: 给定链表 1->2->3->4, 重新排列为 1->4->2->3. 示例 2: 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路: 分为三步: (1)算出链表长度,从中间断开,变成2个链表 (2)后半段链表反转,输出新的head即原来的tail (3)把2个链表交叉串起来。 代码: int GetLength(ListNode *p)//获取链表长度 { int ans=0; while(p) { ans++; p=p->next; } return ans; } ListNode * Reverse(ListNode *p)//链表反转,返回新的head { if(p==NULL)return p; ListNode * q1; ListNode * q2; q1=p->next, p->next=NULL; while(q1) { q2

Leetcode OJ: Path Sum II

∥☆過路亽.° 提交于 2020-01-14 03:10:04
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22 , 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 上一题只需要判断是否存在,这题则需要列出路径,依旧递归,递归关系如下: 1. 当到叶子节点时,判断节点值是否与对应要求的和相等,相等则返回当前节点值,作为一个路径。 2. pathSum(root, sum) = 当前点 与 (pathSum(root->left, sum - root->val), pathSum(root->right, sum - root->val))路径组合 代码如下: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL),

【离散数学】 SDUT OJ 偏序关系

半腔热情 提交于 2020-01-13 13:52:46
偏序关系 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定有限集上二元关系的关系矩阵,确定这个关系是否是偏序关系。 Input 多组测试数据,对于每组测试数据,第1行输入正整数n(1 <= n <= 100),第2行至第n+1行输入n行n列的关系矩阵。 Output 对于每组测试数据,若为偏序关系,则输出yes,反之,则输出no。 Sample Input 4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 4 1 0 0 1 0 1 0 0 0 0 1 0 1 0 0 1 Sample Output yes no Hint 偏序关系形式定义:设R是集合A上的一个二元关系,若R满足自反性、反对称性、传递性,则称R为A上的偏序关系。 #include <stdio.h> #include <stdlib.h> int main() { int i, j, k, n; int a[110][110]; while(~scanf("%d", &n)) { int flag = 1; for(i=0; i<n; i++) { for(j=0; j<n; j++) { scanf("%d", &a[i][j]); } } for(i=0; i<n; i++)

LeetCode OJ 39. Combination Sum

人走茶凉 提交于 2020-01-13 05:01:32
Given a set of candidate numbers ( C ) and a target number ( T ), find all unique combinations in C where the candidate numbers sums to T . The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination ( a 1, a 2, … , a k) must be in non-descending order. (ie, a 1 ≤ a 2 ≤ … ≤ a k). The solution set must not contain duplicate combinations. For example, given candidate set 2,3,6,7 and target 7 , A solution set is: [7] [2, 2, 3] Subscribe to see which companies asked this question 【题目解析】

2020年1月12日 林大OJ习题

女生的网名这么多〃 提交于 2020-01-12 23:56:46
2020年1月12日 找回信心题 在寒假休闲了几天后决定重新开始,于是从一些很简单的水题开始找回信心,寒假加油!! 林大OJ 103 军训 题目思路很简单,就是计算n的阶乘。但本题的关键是他防止溢出的方法:结果对int的取模。这里可以理解为对int所能表示的最大的数:2的32次方减1取模,直接取模输出即可AC。 # include <bits/stdc++.h> using namespace std ; const int N = pow ( 2 , 31 ) - 1 ; long long f ( int n ) { int i ; long long ans = 1 ; for ( i = 1 ; i <= n ; i ++ ) { ans = ans * i ; } return ans ; } int main ( ) { int n ; while ( scanf ( "%d" , & n ) != EOF ) { cout << ( int ) f ( n ) % N << endl ; } return 0 ; } 林大OJ 108 组合–亢龙无悔 直接考查排列组合公式的应用。中了函数毒的我直接把阶乘和组合都写成了函数。。。。 # include <iostream> using namespace std ; int f ( int n ) { int i ;

oj 按顺序输出前M位数

♀尐吖头ヾ 提交于 2020-01-12 15:16:35
## 百万级的数怎么“排序呢?”避免超过1s,建立hash即可,时间在百万级,而排序难免在nlogn,在千万级。 # include <iostream> using namespace std ; # define OFFSET 500000 int num [ 1000000 ] ; //栈容易爆 int main ( ) { int N , M ; while ( scanf ( "%d%d" , & N , & M ) != EOF ) { for ( int i = - 500000 ; i < 500001 ; i ++ ) { num [ i + OFFSET ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { int x ; scanf ( "%d" , & x ) ; num [ x + OFFSET ] = 1 ; } for ( int i = 500000 ; i >= - 500000 ; i -- ) { if ( num [ i + OFFSET ] == 1 && M > 0 ) { M -- ; printf ( "%d" , i ) ; if ( M ) printf ( " " ) ; else { printf ( "\n" ) ; break ; } } } } } ``` 来源: CSDN 作者:

九度OJ 1480 最大上升子序列和 -- 动态规划

柔情痞子 提交于 2020-01-10 05:15:54
题目地址: http://ac.jobdu.com/problem.php?pid=1480 题目描述: 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, ...,aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中序列和最大为18,为子序列(1, 3, 5, 9)的和. 你的任务,就是对于给定的序列,求出最大上升子序列和。注意,最长的上升子序列的和不一定是最大的,比如序列(100, 1, 2, 3)的最大上升子序列和为100,而最长上升子序列为(1, 2, 3)。 输入: 输入包含多组测试数据。 每组测试数据由两行组成。第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000(可能重复)。 输出: 对于每组测试数据,输出其最大上升子序列和。 样例输入: 7 1 7 3 5 9 4 8 样例输出: 18 来源: 2012年北京大学计算机研究生机试真题 状态 dp[i] 表示以i结尾的递增子序列的和。 状态转移方程 dp[i] =

林大OJ习题 2020年1月7日

三世轮回 提交于 2020-01-08 02:16:01
合并字符串 (也可以直接使用函数 strcat()) # include <stdio.h> # include <stdlib.h> # include <string.h> int main ( ) { char a [ 100 ] , b [ 100 ] ; int n , m , i , j ; while ( scanf ( "%s %s" , a , b ) != EOF ) { getchar ( ) ; n = strlen ( a ) ; m = strlen ( b ) ; for ( i = 0 ; i < m ; i ++ ) { if ( b [ i ] != '\0' ) { a [ n ] = b [ i ] ; n ++ ; } else { break ; } } a [ n ] = '\0' ; for ( j = 0 ; j < n ; j ++ ) { printf ( "%c" , a [ j ] ) ; } printf ( "\n" ) ; } return 0 ; } 来源: CSDN 作者: neuer_yao 链接: https://blog.csdn.net/neuer_yao/article/details/103882184

【题解】Comet OJ Round 70 简要题解

∥☆過路亽.° 提交于 2020-01-03 09:12:48
【题解】Comet OJ Round 70 简要题解 A 将放在地上的书按照从小到大排序后,问题的本质就变成了合并两个序列使得字典序最小。可以直接模拟归并排序。直接用循环和 std::merge 实现这个过程。复杂度 \(O(n)\) //@winlere #include<cstdio> #include<algorithm> using namespace std; int data[100003],in[100003],data2[100003],ans[100003],cnt,n,m; int main(){ scanf("%d%d",&n,&m); for(int t=1;t<=m;++t) scanf("%d",data+t),in[data[t]]=1; for(int t=1;t<=n;++t) if(!in[t]) data2[++cnt]=t; merge(data+1,data+m+1,data2+1,data2+cnt+1,ans+1); for(int t=1;t<=n;++t) printf("%d\n",ans[t]); return 0; } B 由于题目保证了字符串之间互不为前缀,所以由 给定字符串的排名 连接而成形成排列的字典序就是这个字符串的字典序。 现在问题就是求出 \(\sum|S|\le 1e6\) 的这么多字符串的字典序