假设有一个由数字 1,2,4,3,5,6,7 组成的数组。我想使用堆排序打印 1,3,5,7,2,4,6 。我一直在尝试修改基本的堆排序,但无法获得正确的输出。
你能帮忙吗?
#include<bits/stdc++.h>
using namespace std;
int heapsize;
int make_left(int i)
{
    return 2*i;
}
int make_right(int i)
{
    return (2*i)+1;
}
void max_heapify(int a[],int i)
{
  //  cout<<heapsize<<endl;
    int largest=i;
   // printf("current position of largest is %d and  largest is %d\n",largest,a[largest]);
    int l=make_left(i);
    int r=make_right(i);
 //   printf("current position of left is %d and  left element is %d\n",l,a[l]);
   // printf("current position of right is %d and  right element is %d\n",r,a[r]);
    if(a[l]>=a[largest] && l<=heapsize && a[l]%2!=0)
        largest=l; …Run Code Online (Sandbox Code Playgroud) 我有一个多个偶数和奇数的数组,我如何将这个数组变成奇数元素的数组,例如:忽略奇数元素但偶数元素加1;
Input_array = [3,67,2,34,55,73,96,21,13,15,16]
Output_array = [3,67,3,35,55,73,97,21,13,15,17]
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
Output_array = [(x+1) for x in Input_array if x%2 == 0]
Run Code Online (Sandbox Code Playgroud)
但这只Output_array包含(偶数元素+ 1)并非所有元素[3, 35, 97, 17]
我正在尝试将字符串转换为包含该字符串的集合。我怎样才能在不分裂的情况下做到这一点?
当我写:
set("abc")
Run Code Online (Sandbox Code Playgroud)
结果是:
{'a','b','c'}
但我希望它是:
{“abc”}
对于课堂上的作业,我正在一个名为student()的类中创建对象。它涉及用户输入学生信息,然后以一种不错的格式输出学生信息。但是,控制台窗口中的每一行都会打印出“无”字样,并要求用户输入。我不确定为什么要打印出来,我想解决这个问题。
我相信问题出在我在定义函数init(自我)的地方,我在其中分配数据成员,但是我已经以多种方式更改了代码,但还没有运气。
class student(): 
     def __init__(self):
        self.name = input(print('What is the Student Name?: '))
        self.address = input(print('What is the Student address?: '))
        self.city = input(print('In which city does the Student reside?: '))
        self.state = input(print('In which state does the Student reside?: '))
        self.zip = input(print('In which zip code does the student reside?: '))
        self.id = input(print('What is the Student ID?: '))
        self.gpa = input(print('What is the Student GPA?: '))
        return
def formatInfo(list):
    for student in list:
        print('Student Name: …Run Code Online (Sandbox Code Playgroud) 我在 view.py 文件中有这个函数,我在它上面传递一个 ID 并渲染某个页面
def more_about_child(request,pk):
    child = get_object_or_404(Child_detail,pk=pk)
    academic = Academic.objects.filter(Student_name=child)  
    context={ 
        'child':child, 
        'academic':academic,
    }
    return render(request,'functionality/more/more.html',context)
Run Code Online (Sandbox Code Playgroud)
而这个 urls.py 文件属于上面的 views.py 文件
from . import views
from django.urls import path
urlpatterns=[
    #more_about_child
    path('more/<int:pk>/',views.more_about_child,name='more'),
]
Run Code Online (Sandbox Code Playgroud)
然后我有这个功能,我想重定向到包含上述 urls.py 文件中的 id 的页面
def add_academy(request,pk):
    child = get_object_or_404(Child_detail, pk=pk)
    academic = Academic.objects.get(Student_name=child)
    form = AcademicForm(request.POST, instance=academic)
    if form.is_valid():
        form.save()
        return redirect('more') #it fails to redirect to this url,because it contain an ID in it
    else:
        form=AcademicForm()
    context = {
        'academic':academic,
        'child':child,
        'form':form, …Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串的变量,"02100030000000000000000000D5010008D501000804"我使用字节分隔字符串
For i As Integer = 1 To (stringReader.Length - 1) Step 2
                'Get the successive 2-character substrings, parse as bytes and add to total
                Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
                sum = sum + CInt(b)
            Next    
Run Code Online (Sandbox Code Playgroud)
我正在将字符串转换为直接integers.eg:(string"10"到Integer10和).这很好.但每当我将字符串"02"转换为Integer时,我只得到Integer(2)并且我需要Integer(02).我该怎么办?
我的代码是:
stringReader = fileReader.ReadLine()      
byt = stringReader(1) + stringReader(2)      
Run Code Online (Sandbox Code Playgroud)
stringreader包含类似的东西 "100030000000000000000000D5010008D501000804"
字节分离
For i As Integer = 1 To (stringReader.Length - 1) Step 2
                'Get the successive 2-character substrings, parse as bytes and add to total
                Dim b …Run Code Online (Sandbox Code Playgroud) 我尝试了很多数字,但是它的运行时间像O(1)一样。为什么这么快?有人可以解释一下**运算符的实际工作原理。
我想在 python 中创建一个具有以下功能的计时器:
timer.start() - 应该启动计时器
timer.pause() - 应该暂停计时器
timer.resume() - 应该恢复计时器
timer.get() - 应该返回当前时间
计时器应该从 0 向上运行。它用于测量时间,而不是触发回调函数。
所以如果你启动它,它应该开始像 0 1 2 3 这样的秒数,如果你暂停它,它应该是静止的 3,但不会更进一步。在它恢复之后,它会继续 4 5 6 等等
我怎样才能做到这一点?
计时器的暂停/恢复功能不是重复的,因为我不关心回调。
我想在C#的Console Application中添加两个输入.我不希望添加的用户输入像
2
8
Run Code Online (Sandbox Code Playgroud)
产量:10
我希望它像
2 8 
Run Code Online (Sandbox Code Playgroud)
产量:10
如何在两个输入之间接受空格字符并将它们相加?我应该为两个输入使用一个int变量还是两个int变量?谢谢.
containsKey 与 containsValue 有何不同?
public Dictionary<string, string> dictionary = new Dictionary<string, string>();
if(dictionary.ContainsValue("123"))
{
}
if(dictionary.ContainsKey("123"))
{
}
Run Code Online (Sandbox Code Playgroud) 所以我的函数需要过滤一个列表,以便它返回一个列表,只列出一个值,当一个函数应用于它时,它返回一个正值,而不使用任何循环.我的代码目前是:
def positive_places(f, xs):
    """takes a function f and list xs and returns
    a list of the values of xs which satisfy
    f>0"""
    y = list(map(f, xs))
    x = filter(lambda i: i > 0, y)
    return x
Run Code Online (Sandbox Code Playgroud)
这当前返回函数的所有正输出值的列表,但是我需要来自原始列表xs的相应值.
在此先感谢您的帮助!