我很难理解这个问题,也很难找到答案。如何计算最坏的运行时间?
以下程序的输入是包含n个整数A的数组A。使用大-O表示法确定每个程序最坏的运行时间。
问题1:
i = 1, total = 0
while i < n/2:
total = total + A[i]
i=i*2
问题2:
total = 0
S = the set {1,2,3,4...n}
for each subset T of S
for each element x in T
total = total + A[x]
问题3:
int i = 1, j = 1;
public class HelloWorld{
public static void main(String []args){
int orig=103, reverse=0, mod;
int numOfDigits=0;
int n = orig;
while (n>0){
n /= 10;
numOfDigits++;
}
n = orig;
while (n > 0){
mod = n % 10;
reverse = reverse + (in
我正在阅读Kernighan和Ritchie的“C编程语言”( the C Programming Language)一书,我陷入了一个主题。
主题编号1.6讨论数组。在这本书中,他们包括了一个计算数字,空格和其他字符的程序。这个程序是这样的:
#include <stdio.h>
main(){
int c,i,nother,nwhite;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;++i)
ndigit[i]=0;
对于下面的伪码,最糟糕的时间复杂度大O表示法是什么?(假设函数调用是O(1)),我对大O表示法非常陌生,所以我不确定答案,但我认为O(log(n))是因为while循环参数每次乘以2,还是仅仅是O(loglog(n))?还是我在这两方面都错了?任何输入/帮助都是值得赞赏的,我正试图掌握最糟糕的时间复杂度的大O表示法的概念,我刚刚开始学习。谢谢!
i ← 1
while(i<n)
doSomething(...)
i ← i * 2
done
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
unsigned seed;
cout << "Input a whole number between 0 and 65535 to\n initialize the random number generator: ";
cin >> seed;
srand(seed);
int number;
number = rand();
int count;
for (count =
这个程序要求我输入一个从1到9的数字,用这个数字它必须打印3个不同的语句,时间与我输入的数字相同。例如,如果我输入数字5,则所有3条语句必须每个打印5次。问题是,当我输入一个数字时,它显示的次数与我输入的数字不一样。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
printf("Please enter a number from 1 to 9= ");
scanf("%d", &num);
我必须制作一个程序,读取用户号从1到100,然后显示该数字的所有平方,不知何故它总是错误的。
import math
numUser = int(input("Enter a value between 1 and 100: "))
counter = 1
if numUser >= 100 or numUser =< 0:
print("Enter a valid number!")
else:
while counter =< 100:
squares = pow(numUser, 2)
我正试着在c++中猜猜我的数字游戏,但是电脑需要猜我的数字。但问题是我被困在这个无限循环中。我只是一个初学者,所以这是一个非常基础的程序。
这是我的代码:
int secretNumber = rand() %100 + 1; // random number between 1-100
int tries=0;
int input;
cout <<"typ your number\n";
cin >> input;
do
{
cout <<secretNumber <<endl;
++tries;
我想知道这个while循环会执行多少次。这是一个使用XOR和AND添加两个数字的函数。
def Add(x, y):
# Iterate till there is no carry
while (y != 0):
# carry now contains common
# set bits of x and y
carry = x & y
# Sum of bits of x and y where at
# least one of the bits is not
我必须在java 长数据类型中打印整数k的所有正幂。我的代码是:
int k = Integer.parseInt(args[0]);
int i = 1;
long a = k;
while (a <= Long.MAX_VALUE) {
System.out.print(i + " ");
System.out.println(a);
i++;
a = (long) Math.pow(k, i);
}
当我运行这个程序时,会创建一个无限循环。
但是,当我运行这个程序时,
我们打算用java创建一个程序,在这个程序中,计算机随机猜测1-100之间的数字,并允许用户猜测这个数字。如果数字低于随机数,程序应该说:更低!对于更高的,程序应该说:更高!如果用户猜对了数字,它应该说祝贺你在X次尝试中猜对了数字,这就是我到目前为止所拥有的,当我在cmd中执行时,它只是垃圾邮件更高或更低,我需要帮助来解决它。
import java.util.Scanner;
import java.util.Random;
public class GuessingGame{
public static void main(String[] args) {
i
为我的计算机科学课做作业,却不知道我做错了什么。我必须编写一个程序,将1到100之间的所有方格(1,4,9,16,25,36,49,64,81,100)相加,作为作业的一部分,我需要使用"while“语句,所以没有"for”语句会有帮助。我的当前代码如下所示。
import java.util.Scanner;
public class While42B {
public static void main(String []args ) {
Scanner reader = new Scanner(System.in);
int n =
以下是我的作业要求:
打印1到100之间的所有数字(含1到100),每行用制表符平均间隔10个数字。
使用最大2循环和1 if语句。
我对如何做到这一点有很好的理解,但我只是很难在每10个数字之后找到一条新的行。
到目前为止,这就是我所拥有的:
import java.util.Scanner;
public class Table {
public static void main (String[] args) {
int counter, value;
counter = 1;
value = 0;
我正在尝试写一个程序,它会告诉你输入的数字是否是质数,并将从1到100的所有质数写到一个文件中,并显示这些数字。这就是我到目前为止所拥有的,但我迷路了。
bool isPrime(int);
int _tmain(int argc, _TCHAR* argv[])
{
int num, answer, choice, i, numb=1;
do
{
cout<< "Enter a number and I will tell you if it is prime or not."<<endl;
这是以“n”开头的“数字”和“10”的“其他数字”的程序。
void divideme()
static int count=0; //initalised a variable which I'll be returning the value of.
int n;
cin>>n;//taken input of variable which I want to divide by another number (say 10 in this case)
int &rem=n;//created a reference variab
Ruby有一个内置的loop命令,它将永远地执行块(或者直到被break停止)。但是,当将它与功能相似的while true进行比较时,它要慢得多:
require "benchmark/ips"
NUMBER = 100_000_000
def fast
index = 0
while true
break if index > NUMBER
index += 1
end
end
def slow
index = 0
loop do
break if index > NUMBER
index += 1
我做了一个函数,其中脉宽调制信号产生在输出(PORTD),而不使用脉宽调制控制寄存器内的知情同意微控制器(PIC18F452)。为了使输出端的LED慢慢变暗,我试图把脉冲从一个周期的0%提高到一个方波周期的100%,同时保持方波频率常数。一切都应该按计划进行,除了第二个参数被传递到pwm函数,在某种程度上,当从655重新设置到666 (也就是说,当占空比为65%时)。在此事件之后,传递给pwm函数的值从0开始。因为它不应该在从655到656的转换中重置,而是在从1000到1001的过渡时重置。
void main(void) {
TRISD = 0x00; //port D set a
// a java program that displays the maclaurinseries math function
// sin x = x - x^3/3! + x^5/5! - x^7/7!
// sin x = (-1)^n * x^(2n+1) / (2n+1)!
import java.util.Scanner;
public class maclaurinseries
{
public static void main(String args[])
{
double x;
所以我对这个挑战有一些想法。我应该,而不是猜测程序从1-100中随机选择的数字,这次我自己随机选择一个数字,并让程序猜测它。所以我让它工作了,但它并不完全是我想要的,这是我到目前为止得到的:
#I'm deciding to go with number 43.
import random
print("Hello computer, welcome to my guessing game!")
guess_list=[]
computer_guess=""
tries=1
while computer_guess!=43:
computer_
我创建了一个简单的程序,向用户询问电话号码的最后7位数字。我使用了一个do - while循环,但是我意识到,如果用户输入一个以'0‘开头的数字,程序将不会将其计算为1位数。我做什么好?到目前为止,这就是我所拥有的:
import java.util.Scanner;
public class PhoneCode {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int phoneNum;
do
我是一个java脚本编程的新手。我想打印从0到100的整数。这是我的代码。
var outputAreaRef = document.getElementById("outputArea");
var i = 0; //Initialize counter
var number = 0;
while ( i <= 100) // Test Counter
{
number = number + 1 + "<br />";
i = i + 1; // Increment counter
我希望我的程序接受用户号输入,并输出从1到输入号的总和(使用while循环)。例:如果输入值为4,则之和为10,即1+2+3+ 4。
我的代码编译,但返回一个永不结束的1,直到我的创建者停止响应。
import java.util.Scanner;
import java.io.*;
public class SumLoopWhile {
public static void main(String[] args) {
int number;
int sum = 1;
Scanner in = new Scanner (System.in);