Giter Club home page Giter Club logo

kamacoder-solutions's Introduction

Hi 👋

大家好,我是程序员Carl。哈工大师兄,ACM亚洲区域赛铜牌,毕业后在腾讯、百度采坑多年,以下资料会对大家很有帮助:

我的开源项目:

学习规划 🌱

开源项目 🔭

小游戏 😄

  • Gomoku:可联机对战的五子棋项目

开发的工具 📫

我的公众号⚡:

kamacoder-solutions's People

Contributors

cavemancave avatar challis-yin avatar charon2121 avatar chenjunyi1999 avatar coolppw avatar dida-lj avatar dingxinliang88 avatar he-weilai avatar huanheart avatar it-talon avatar liwangshengya avatar liwei1101 avatar moutainman avatar okatu-loli avatar q763562998 avatar renyanwei1122 avatar sandroya avatar shishuochen avatar silk-coding avatar stoneyezhenxu avatar sunshine-zd avatar tftree avatar wexception avatar whilewu avatar yakultgo avatar yibingguo avatar yihao-bit avatar youngyangyang04 avatar yueny avatar zsy-site avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

kamacoder-solutions's Issues

21.构造二叉树-使用cin进行输入数据会超时并出现潜在的数组或指针越界,而getline不会,请问一下为什么。

#include<iostream>
#include<string>
using namespace std;
class Tree {
public:
    Tree(char value) {
        val = value;
    }
    char val;
    Tree* leftTree = NULL;
    Tree* rightTree = NULL;
};
Tree* GetBinnaryTree(string preorder, string midorder) {
    if (preorder.size() == 0 || midorder.size() == 0) {
        return NULL;
    }
    char value = preorder[0];
    Tree* root = new Tree(value);
    int index = 0;
    while (midorder[index] != value) {
        index++;
    }
    string left_midorder = "";
    for (int i = 0; i < index; i++) {
        left_midorder += midorder[i];
    }
    string right_midorder = "";
    for (int i = index + 1; i < midorder.size(); i++) {
        right_midorder += midorder[i];
    }
    string left_preorder = "";
    for (int i = 1; i <= left_midorder.size(); i++) {
        left_preorder += preorder[i];
    }
    string right_preorder = "";
    for (int i = left_midorder.size() + 1; i < preorder.size(); i++) {
        right_preorder += preorder[i];
    }
    root->leftTree = GetBinnaryTree(left_preorder, left_midorder);
    root->rightTree = GetBinnaryTree(right_preorder, right_midorder);
    return root;
}
void nextorder(Tree* root) {
    if (root == NULL) {
        return;
    }
    nextorder(root->leftTree);
    nextorder(root->rightTree);
    cout << root->val;
}
int main() {
    while (true) {
        string preorder, midorder;
        cin >> preorder >> midorder;
        Tree* root = GetBinnaryTree(preorder, midorder);
        nextorder(root);
        cout << endl;
    }
    return 0;
}

[Go] 2. A+B问题II - 看不出来我的答案和正确答案有任何区别,但是说代码不通过

package main

import "fmt"

func main() {
    for {
        var n int
        if _, err := fmt.Scanf("%d", &n); err != nil {
            return
        }
        
        for i := 0; i < n; i++ {
            var a, b int
            if _, err := fmt.Scanf("%d %d", &a, &b); err != nil {
                return
            }
            
            fmt.Println(a + b)
        }
    }
}
========[test0.in]=========
100
-3543 -17732
-23566 -9705
3425 -30929
-15910 20397
0 0
1 -1
-1 1
16846 19268
19777 3611
20811 10225
28426 30096
22460 29806
10089 15157
25034 20362
2944 2802
27086 4111
1408 6789
12703 1863
7452 8227
28327 19646
9278 7266
31805 30538
7161 31574
2095 26441
26048 20730
28061 1578
5333 13522
12814 7983
29699 207
22717 13680
5008 30579
52 31524
29550 5429
32284 19996
7601 4391
27988 23952
17734 20694
25152 23838
15585 21362
14692 26896
23095 22575
13361 32734
6510 17386
22558 13555
5329 2576
17403 180
9417 7356
28546 20927
15368 10271
2352 17264
21512 7461
26195 7594
32180 20309
14691 468
27237 10746
661 26913
271 16512
28334 22549
21437 15904
6165 27461
2362 30912
24109 8542
3857 11028
18145 1640
10168 6666
7809 9292
10505 10597
18892 23700
32497 9127
32334 3955
18028 14645
21740 13317
5297 31414
27325 17879
16041 21180
1051 11178
13478 17062
26106 31604
18002 26309
22905 16296
25318 1469
3511 8631
10652 12078
19432 7639
24166 11668
12712 9219
5856 20332
25998 19011
13187 19353
31237 29741
31004 17669
29271 5036
24189 12683
7691 26368
25203 10261
15985 22534
32248 23041
10875 24762
1705 15306
25806 6394 
2
2 4
9 21
========[test0.out]=========
Expected						      |	Yours
-21275								-21275
-33271								-33271
-27504								-27504
4487								4487
0								0
0								0
0								0
36114								36114
23388								23388
31036								31036
58522								58522
52266								52266
25246								25246
45396								45396
5746								5746
31197								31197
8197								8197
14566								14566
15679								15679
47973								47973
16544								16544
62343								62343
38735								38735
28536								28536
46778								46778
29639								29639
18855								18855
20797								20797
29906								29906
36397								36397
35587								35587
31576								31576
34979								34979
52280								52280
11992								11992
51940								51940
38428								38428
48990								48990
36947								36947
41588								41588
45670								45670
46095								46095
23896								23896
36113								36113
7905								7905
17583								17583
16773								16773
49473								49473
25639								25639
19616								19616
28973								28973
33789								33789
52489								52489
15159								15159
37983								37983
27574								27574
16783								16783
50883								50883
37341								37341
33626								33626
33274								33274
32651								32651
14885								14885
19785								19785
16834								16834
17101								17101
21102								21102
42592								42592
41624								41624
36289								36289
32673								32673
35057								35057
36711								36711
45204								45204
37221								37221
12229								12229
30540								30540
57710								57710
44311								44311
39201								39201
26787								26787
12142								12142
22730								22730
27071								27071
35834								35834
21931								21931
26188								26188
45009								45009
32540								32540
60978								60978
48673								48673
34307								34307
36872								36872
34059								34059
35464								35464
38519								38519
55289								55289
35637								35637
17011								17011
32200								32200


==============================

time_space_table:
/1001/sample.in:Accepted mem=1672k time=10ms
/1001/test0.in:Wrong Answer mem=1672k time=62ms

feature 0054.替换数字.md 替换数字 C++ 题解提交

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;
int main() {
    string s;
    while (cin >> s) {
        int count = 0; // 统计数字的个数
        int sOldSize = s.size();
        for (int i = 0; i < s.size(); i++) {
            if (s[i] >= '0' && s[i] <= '9') {
                count++;
            }
        }
        // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小
        s.resize(s.size() + count * 5);
        int sNewSize = s.size();
        // 从后先前将空格替换为"number"
        for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
            if (s[j] > '9' || s[j] < '0') {
                s[i] = s[j];
            } else {
                s[i] = 'r';
                s[i - 1] = 'e';
                s[i - 2] = 'b';
                s[i - 3] = 'm';
                s[i - 4] = 'u';
                s[i - 5] = 'n';
                i -= 5;
            }
        }
        cout << s << endl;
    }
}

请问2. A+B问题II,里的while1, try, except有什么作用, 为什么去掉这三会不通过。报错信息如下,看都看不懂呜呜

========[test0.out]=========
Expected | Yours
-21275 -21275
-33271 -33271
-27504 -27504
4487 4487
0 0
0 0
0 0
36114 36114
23388 23388
31036 31036
58522 58522
52266 52266
25246 25246
45396 45396
5746 5746
31197 31197
8197 8197
14566 14566
15679 15679
47973 47973
16544 16544
62343 62343
38735 38735
28536 28536
46778 46778
29639 29639
18855 18855
20797 20797
29906 29906
36397 36397
35587 35587
31576 31576
34979 34979
52280 52280
11992 11992
51940 51940
38428 38428
48990 48990
36947 36947
41588 41588
45670 45670
46095 46095
23896 23896
36113 36113
7905 7905
17583 17583
16773 16773
49473 49473
25639 25639
19616 19616
28973 28973
33789 33789
52489 52489
15159 15159
37983 37983
27574 27574
16783 16783
50883 50883
37341 37341
33626 33626
33274 33274
32651 32651
14885 14885
19785 19785
16834 16834
17101 17101
21102 21102
42592 42592
41624 41624
36289 36289
32673 32673
35057 35057
36711 36711
45204 45204
37221 37221
12229 12229
30540 30540
57710 57710
44311 44311
39201 39201
26787 26787
12142 12142
22730 22730
27071 27071
35834 35834
21931 21931
26188 26188
45009 45009
32540 32540
60978 60978
48673 48673
34307 34307
36872 36872
34059 34059
35464 35464
38519 38519
55289 55289
35637 35637
17011 17011
32200 32200

==============================
time_space_table:
/1001/sample.in:AC mem=15744k time=71ms
/1001/test0.in:WA mem=15744k time=80ms

golang 19题链表反转 超时

代码如下

package main

import "fmt"

func main() {
	for {
		var n int
		_, err := fmt.Scan(&n)
		if err != nil {
			return
		}
		if n == 0 {
			fmt.Println("list is empty")
			continue
		}
		var head *Node
		dummyNode := &Node{next: head}
		temp := dummyNode
		for n > 0 {
			var num int
			_, err = fmt.Scan(&num)
			if err != nil {
				return
			}
			newNode := &Node{val: num}
			temp.next = newNode
			temp = temp.next
			n--
		}
		head = dummyNode.next
		show(head)
		show(head.reverse())
	}
}

func show(head *Node) {
	if head == nil {
		return
	}
	cur := head
	for cur != nil {
		fmt.Printf("%d ", cur.val)
		cur = cur.next
	}
	fmt.Println()
}

type Node struct {
	val  int
	next *Node
}

func (l *Node) reverse() *Node {
	if l == nil || l.next == nil {
		return l
	}
	var pre *Node
	cur := l
	for cur != nil {
		temp := cur.next
		cur.next = pre
		pre = cur
		cur = temp
	}
	return pre
}

题36 网络路径和

第36题这种输入方式是不是有点考察的本末倒置了,[[2,3,1],[2,5,3],[4,2,1]]这种输入方式对于java来说有直接的函数可以用,但是对于c++或者其它语言来说,输入就要花费好长时间来想想怎么处理,可不可以卡尔考虑一下解决一下类似这种题目的输入问题,把问题的考察点重点放在算法而不是输入上?谢谢啦!

共同祖先问题

请问为什么最后要比较长度呢?即使长度有大小关系,怎么通过长度的大小关系判断小明和小宇的关系呢?

Update 0057.爬楼梯.md Go 题解

package main
import (
	"fmt"
)

func main(){
    var n,m int
    fmt.Scanf("%d %d",&n,&m)
    var dp = make([]int,n + 1)
    dp[0] = 1
    for  i := 1;i <= n;i++ {
        for j := 1;j <= m;j++ {
            if i - j >= 0 {
                dp[i] += dp[i - j]
            }
        }
    }
    fmt.Println(dp[n])
    
}

go 判题系统或有误

如下所示,这是本人使用 go 提交 0002 的记录,答案全部正确但系统判定为 答案错误

package main

import "fmt"

func main() {
	for {
		var n, x, y int
		_, err := fmt.Scanf("%d", &n)
		if err != nil {
			break
		}
		for i := 0; i < n; i++ {
			fmt.Scanf("%d %d", &x, &y)
			fmt.Println(x+y)
		}
	}
}
========[test0.out]=========
Expected						      |	Yours
-21275								-21275
-33271								-33271
-27504								-27504
4487								4487
0								0
0								0
0								0
36114								36114
23388								23388
31036								31036
58522								58522
52266								52266
25246								25246
45396								45396
5746								5746
31197								31197
8197								8197
14566								14566
15679								15679
47973								47973
16544								16544
62343								62343
38735								38735
28536								28536
46778								46778
29639								29639
18855								18855
20797								20797
29906								29906
36397								36397
35587								35587
31576								31576
34979								34979
52280								52280
11992								11992
51940								51940
38428								38428
48990								48990
36947								36947
41588								41588
45670								45670
46095								46095
23896								23896
36113								36113
7905								7905
17583								17583
16773								16773
49473								49473
25639								25639
19616								19616
28973								28973
33789								33789
52489								52489
15159								15159
37983								37983
27574								27574
16783								16783
50883								50883
37341								37341
33626								33626
33274								33274
32651								32651
14885								14885
19785								19785
16834								16834
17101								17101
21102								21102
42592								42592
41624								41624
36289								36289
32673								32673
35057								35057
36711								36711
45204								45204
37221								37221
12229								12229
30540								30540
57710								57710
44311								44311
39201								39201
26787								26787
12142								12142
22730								22730
27071								27071
35834								35834
21931								21931
26188								26188
45009								45009
32540								32540
60978								60978
48673								48673
34307								34307
36872								36872
34059								34059
35464								35464
38519								38519
55289								55289
35637								35637
17011								17011
32200								32200

==============================
time_space_table:
/1001/sample.in:AC mem=1728k time=10ms
/1001/test0.in:WA mem=1748k time=68ms


得分 : 50.000

51. 平移二叉树(第七期模拟笔试)C++

#include<vector>

#include<algorithm>


#include<iostream>
using namespace std;

// 这一层旋转k , 下一层旋转 2 * k
void myreverse(vector<vector<int> > &tree, int i ,  int k ){
    if(i == tree.size() ){
        return;
    }
    
    // cout<< "i="<< i<< "\tk = "<< k << endl;
    auto &nums = tree[i]; 
    
    if(nums.size() <= k ){
         k = k  % nums.size();
    }
    reverse(nums.begin(), nums.end());
    reverse(nums.begin(), nums.begin() + k);
    reverse(nums.begin() + k, nums.end());
    // 
    // cout<< "调用了"<< "i="<< i+1<< "\tk = "<<2*  k << endl;
    int cnt = 0;
    for(int i = 0; i< k && i< nums.size(); ++i){
        if(nums[i] != -1){
            cnt++;
        }
    }
     myreverse(tree, i+1, 2* cnt);
    
    
}

void printtree(vector<vector<int> > &tree){
    cout<< "tree high = "<< tree.size() << endl;
    for(auto & nums : tree){
        for(int n : nums){
            cout<< n<< ' ';
        }
        cout<< endl;
    }    
}


int main(){
    int k , n;
    cin>> k >> n;
    
    int nodes = 1;
    int ct = 0, nzct = 0;
    vector<int> temp;
    vector<vector<int> > tree;
    for(int i = 0; i< n; ++i){
        int a;
        cin>> a;
        temp.push_back(a);
        ct++;
        
        if(a!= -1){
            nzct ++;
        }
        if(ct == nodes){
            nodes = nzct *  2;
            nzct = 0;
            ct = 0;
            tree.push_back(temp);
            temp.clear();
        }
        
    }

    
    for(int i = tree.size()-2; i> 0; --i){
        auto & nums = tree[i];
        // 每层都右旋, 每次右旋转都向下传递
        myreverse(tree, i,  k);
        // printtree(tree);

    }
    
    for(auto & nums : tree){
        for(int n : nums){
            cout<< n<< ' ';
        }
    }
    
    
    cout<< endl;
    
    return 0;
    
    

}

A+B问题III中给出的Python代码不具有普适性

在A+B问题III中的Python代码中:


import sys

while True:
s = input().split() # 一行一行读取
a, b = int(s[0]), int(s[1])
if not a or not b: # 遇到 0, 0 则中断
break
print(a + b)


该模板只适用于当前题型,a,b= int(s[0]), int(s[1]),再加上后面的判断 if not a or not b ,这两行代码并不具有普适性,如果改为:


import sys

while True:
s = list(map(int,input().split())) # 一行一行读取
if not any(s):
break # 遇到 0, 0 则中断
print(sum(s))


这里面的if not any(s): break 具有普适性,希望建议能被采纳!!

7.平均绩点的C++写法规范化

给定的代码看起来是正确的,

然而,还有一些边界情况需要考虑和改进:

  1. 输入为空行的情况:如果输入的一行文本为空行,那么 s.size() 的值为 0,会导致除以零的错误。为了避免这种情况,可以在进入循环之前添加一个判断条件,如果 s 为空行,则直接跳过当前循环。

  2. 输入中包含多个连续空格的情况:如果输入中有多个连续空格,那么这些连续空格会被视为多个分隔符,导致计算的结果不准确。可以考虑在处理空格时,只跳过第一个连续空格,将后续连续空格视为非法字符。

下面是对代码的进一步改进,解决了上述边界情况:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    string s;
    while (getline(cin, s)) {
        if (s.empty()) continue; // 跳过空行

        float sum = 0;
        int count = 0;
        int flag = 1;

        for (int i = 0; i < s.size(); i++) {
            if (s[i] == ' ') {
                // 跳过连续的空格
                if (i > 0 && s[i - 1] == ' ') continue;
                else continue;
            }
            else if (s[i] == 'A') { sum += 4; count++; }
            else if (s[i] == 'B') { sum += 3; count++; }
            else if (s[i] == 'C') { sum += 2; count++; }
            else if (s[i] == 'D') { sum += 1; count++; }
            else if (s[i] == 'F') { sum += 0; count++; }
            else {
                flag = 0;
                cout << "Unknown" << endl;
                break;
            }
        }

        if (flag && count > 0) {
            printf("%.2f\n", sum / count);
        }
    }

    return 0;
}

通过添加对空行和连续空格的处理,上述代码能够更准确地计算评分的平均值,并避免了除以零的错误。

feature 0055.右旋字符串.md C++ 题解提交

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;
int main() {
    string s;
    while (cin >> s) {
        int count = 0; // 统计数字的个数
        int sOldSize = s.size();
        for (int i = 0; i < s.size(); i++) {
            if (s[i] >= '0' && s[i] <= '9') {
                count++;
            }
        }
        // 扩充字符串s的大小,也就是每个空格替换成"number"之后的大小
        s.resize(s.size() + count * 5);
        int sNewSize = s.size();
        // 从后先前将空格替换为"number"
        for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
            if (s[j] > '9' || s[j] < '0') {
                s[i] = s[j];
            } else {
                s[i] = 'r';
                s[i - 1] = 'e';
                s[i - 2] = 'b';
                s[i - 3] = 'm';
                s[i - 4] = 'u';
                s[i - 5] = 'n';
                i -= 5;
            }
        }
        cout << s << endl;
    }
}

第七题平均绩点,采用另一种方式输出两位小数

#include<iostream>
using namespace std;
#include<iomanip>
int main(){
    string str;
    while(getline(cin, str)){
        float sum = 0;
        int count = 0;
        int flag = 1;
        for(int i = 0; i<str.size(); i++){
            if(str[i] == 'A'){
                sum += 4;
                count++;
            }else if(str[i] == 'B'){
                sum += 3;
                count++;
            }else if(str[i] == 'C'){
                sum += 2;
                count++;
            }else if(str[i] == 'D'){
                sum += 1;
                count++;
            }else if(str[i] == 'F'){
                sum += 0;
                count++;
            }else if(str[i] == ' '){
                continue;
            }else{
                cout<<"Unknown"<<endl;
                flag = 0;
                break;
            }
        }
        if(flag)cout<<fixed<<setprecision(2)<<sum/count<<endl;
    }
}

7.平均绩点,用 HashMap 的方法写出来,可读性更高一点

7.平均绩点

import java.util.*;
import java.lang.*;


public class Main{
    public static Map<String,Integer> map = new HashMap<>();
    static{
        map.put("A",4);
        map.put("B",3);
        map.put("C",2);
        map.put("D",1);
        map.put("F",0);
    }
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] items = line.split(" ");
            double sum = 0.0;
            int count = 0;
            boolean flag = false;
            for(String item:items){
                if(map.get(item) != null){
                    sum += map.get(item);
                    count++;
                }else{
                    flag = true;
                }
            }
            if (flag) {
                System.out.println("Unknown");
            } else {
                System.out.printf("%.2f\n", sum / count);
            }
        }
    }
}

faeture:Update 0057.爬楼梯.md C++题解

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main() {
    int n, m;
    while (cin >> n >> m) {
        vector<int> dp(n + 1, 0);
        dp[0] = 1;
        for (int i = 1; i <= n; i++) { // 遍历物品
            for (int j = 1; j <= m; j++) { // 遍历背包
                if (i - j >= 0) dp[i] += dp[i - j];
            }
        }
        cout << dp[n] << endl;
    }
}

46. 携带研究材料,go语言代码超时,但我找不到优化的点了

`package main

import "fmt"

func main() {
var m, n int
fmt.Scan(&m, &n)
materialSpaces := make([]int, m)
for i := range materialSpaces {
fmt.Scan(&materialSpaces[i])
}
materialValues := make([]int, m)
for i := range materialValues {
fmt.Scan(&materialValues[i])
}
dp := make([]int, n+1)
for i := 0; i < m; i++ {
for j := n; j >= materialSpaces[i]; j-- {
dp[j] = max(dp[j], dp[j-materialSpaces[i]]+materialValues[i])
}
}
fmt.Println(dp[n])
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
`花了1.7s

feature:Update 0056.携带矿石资源.md C++题解

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int c,n; // 容量 和 种类数量 
void chose(){
	vector<int>weight(n);
	vector<int>value(n);
	vector<int>nums(n);
	for(int i = 0;i < n;i++){
		cin >> weight[i];
	}
	for(int i = 0;i < n;i++){
		cin >> value[i];
	}
	for(int i = 0;i < n;i++){
		cin >> nums[i];
	}

	//标准的01背包
	vector<int>dp(c+1,0);
	for(int i = 0;i < n;i++){
		for(int j = c;j >= weight[i];j--){
			for(int k = 0;k <= nums[i] && (j - k*weight[i]) >= 0;k++){
				dp[j] = max(dp[j], dp[j-k*weight[i]] + k*value[i]);
			}
			
		}
	} 
	cout << dp[c] << endl;
}
 

int main(){
	cin >> c >> n;
	chose();
	return 0;
}

31. 字符串的最大价值 c++版本答案

#include
#include
#include
#include
#include <math.h>
#include
using namespace std;

// 通过计算不同部分连续字符对最终价值的贡献来得到最大的价值。
int findMaxVal(string &s, char c) {
// 1. 找到第一个字符 c 出现的位置 start 和最后一个字符 c 出现的位置 end。
int start = -1, end = -1, num = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == c) {
if (start == -1) start = i;
end = i;
num++;
}
}
// 2. 两个c中间连续字符、前后字符对应的价值
int midVal = (1 + num) * num / 2;
int leftVal = (1 + start) * start / 2;
int len = s.size() - end - 1;
int rightVal = (1 + len) * len / 2;
int totalVal = leftVal + midVal + rightVal;
return totalVal;
}

int main() {
string S;
while (cin >> S) {
int val1 = findMaxVal(S, '0');
int val2 = findMaxVal(S, '1');
cout << max(val1, val2) << endl;
}
}

共同祖先问题

#include

using namespace std;
int main(){
int n;
while(cin>>n){

    int index;
    int value;
    int ming = 1;
    int yu = 2;
    int a = 1;
    for(int i = 0; i < n; i++){
        cin>>index>>value;
        if(ming == index){
            ming = value;
        }
        if(yu == index){
            yu = value;
        }
        if((ming == n+1 || yu == n+1) && a){
            a = 0;
            if(ming == yu){
                cout<<"You are my brother"<<endl;
            }else if( ming == n+1){
                cout<<"You are my younger"<<endl;
            }else{
                cout<<"You are my elder"<<endl;
            }
        }

    }
    
}
return 0;

}

第11题:共同祖先(python)

疑问:请问这道题的判断是不是有误呢?或说必须按照某种格式来呢?

背景:我尝试过用自己的解法在本地输出,输出结果与预测答案是一致的,但是无法在卡码网通过。另外,我也尝试过给推荐的方法修改大于等于小于的顺序,发现当等于的类别放在if或elif中的时候,代码也无法通过。

猜测:一般情况下,按照数值对比,等于号是不存在特殊情况的;但是如果将其地址值作为评判标准之一,就有可能无法通过。所以,麻烦大神检查一下~ 谢谢

模拟笔试数据范围

卡哥你好,模拟的笔试题,能不能稍微给个数据范围,因为题目会有多种解法,没有数据范围的话,可能不太好选择哪种解法

1. A+B问题I Java

最简单的解法

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        while (scanner.hasNext()) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int sum = a + b;
            System.out.println(sum);
        }
        
        scanner.close();
    }
}

在优化一下减少nextInt调用次数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder output = new StringBuilder();
        
        while (scanner.hasNextLine()) {
            // 一次读取一行,减少内存消耗,通过正则分割
            String[] input = scanner.nextLine().split(" ");
            int a = Integer.parseInt(input[0]);
            int b = Integer.parseInt(input[1]);
            int sum = a + b;
            output.append(sum).append("\n");
        }
        
        System.out.print(output);
        scanner.close();
    }
}

第40题 python当算法相同时会出现内存不足的情况

代码如下
`def get_count(n):
nums = [0 for _ in range(n+1)]
nums[1] = 1
for i in range(2, n+1):
if i%2 == 0:
nums[i] = nums[i//2]+1
else:
nums[i] = min(nums[i-1]+1, nums[(i+1)//2]+2)
return nums[-1]

n = abs(int(input()))
print(get_count(n=n)) `
与C++解法和Java解法算法相同,但是报错超时

Strange input format error in problem 1008.

Hi! I have submitted my answer for problem 1009. Here is the code:

import java.util.*;

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (sc.hasNextLine()) {
            int num = sc.nextInt();
            int sum = 0;
            while (num > 0) {
                int a = num % 10;
                if (a % 2 == 0) sum += a;  
                num /= 10;
            }
            System.out.println(sum);
            System.out.println();
        }
    }
}

However, it shows the input error as follows:

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at Main.main(Main.java:9)
/1008/test0.in:	    non-zero return = 1 

----time_space_table:----
/1008/sample.in:AC mem=13024k time=148ms
/1008/test0.in:RE mem=13464k time=195ms

And the explanation is non-zero return : Main函数不能返回非零的值,否则视同程序出错。 Should I ignore this and try to use nextInt() inside, or is this input format not allowed in kamacoder?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.