小编Gre*_*ill的帖子

确定数组的长度以提高内存效率

用C语言编写一个函数:

  • 将存储在字符串中的句子作为其唯一参数(例如,"这是一个短句子".).
  • 返回一个字符串,该字符串由每个单词中的字符数(包括标点符号)组成,空格分隔数字.(例如,"4 2 1 5 9").

我写了以下程序:

 int main()    
    {  
            char* output;  
            char *input = "My name is Pranay Godha";  
            output = numChar(input);  
            printf("output : %s",output);  
            getch();  
            return 0;  
    }  


char* numChar(char* str)   
{   
        int len = strlen(str);  
    char* output = (char*)malloc(sizeof(char)*len);   
    char* out = output;  
    int count = 0;  

    while(*str != '\0')  
    {
        if(*str != ' ' )
        {
            count++;
        }
        else
        {

            *output = count+'0';
            output++;
            *output = ' ';
            output++;
            count = 0;

         }
         str++;
   } …
Run Code Online (Sandbox Code Playgroud)

c memory

1
推荐指数
1
解决办法
416
查看次数

如何修改O(n)列表长度函数以在O(1)中运行?

从这个链接,是ilist.c文件的副本 http://www.student.cs.uwaterloo.ca/~cs136/assignments/a5/

我需要做的是如何将此O(n)函数转换为在O(1)中运行:

// computes the number of elements in il
int ilength(ilist il)
{
    int i = 0;
    while (!iempty_huh(il))
    {
        i++;
        il = irest(il);
    }
    return i;
}
Run Code Online (Sandbox Code Playgroud)

我可以编辑ilist.c文件中的函数(我假设结构和图标)但我无法编辑ilist.h文件.我根本不知道如何处理转换为O(1),任何想法都会有所帮助!

c memory big-o data-structures

1
推荐指数
1
解决办法
258
查看次数

Perl printf问题

如何使用printf打印此示例输出?

******************************************************************************
**  XYZ Corporation                   Date: March 27, 1989(Use current date)**
**  999 John Doe street                                                     **
**  Ypsilanti, MI. 48197.                                                   **
**                                                                          **
**  Pay to the order of:  ?                                                 **
**             The amount of: ?     dollars, and    ?    cents              **
**                                                                          **
**                                 signed:                                  **
**                                         President, XYZ Corporation.      **
**                                                                          **
**--------------------------------------------------------------------------**
**                          SUMMARY                                         **
**  Social security number:   ?                                             **
**  Regular pay:              ?                                             **
**  Overtime pay:             ?                                             **
**  Gross pay:                ?                                             **
** …
Run Code Online (Sandbox Code Playgroud)

perl printf

1
推荐指数
1
解决办法
307
查看次数

我们在条件三元运算符中使用逗号时发现的东西?

好吧,我在三元运算符中有一个关于逗号的问题.剪掉垃圾,代码如下:

void test_comma_in_condition(void)
{
    int ia, ib, ic;

    ia = ib = ic = 0;
    bool condition=true;

    cout<<"Original:"<<endl;
    cout<<"ia: "<<ia<<endl;
    cout<<"ib: "<<ib<<endl;
    condition?(ia=1, ib=2):(ia=11, ib=12);
    cout<<"After:"<<endl;
    cout<<"ia: "<<ia<<endl;
    cout<<"ib: "<<ib<<endl;

    ia = ib = ic = 0;
    condition?ia=1, ib=2, ic=3:ib=22,ia=21, ic=23;
    cout<<"The operation must be bracketed, or you'll see..."<<endl;
    cout<<"ia: "<<ia<<endl;
    cout<<"ib: "<<ib<<endl;
    cout<<"ic: "<<ic<<endl;

    condition?ia=1, ib=2, ic=3:ia=21, ib=22, ic=23;
    cout<<"The operation must be bracketed, or you'll see..."<<endl;
    cout<<"ia: "<<ia<<endl;
    cout<<"ib: "<<ib<<endl;
    cout<<"ic: "<<ic<<endl; 

    return;
}
Run Code Online (Sandbox Code Playgroud)

输出将如下:

Original:
ia: 0 …
Run Code Online (Sandbox Code Playgroud)

c++ ternary

1
推荐指数
2
解决办法
1929
查看次数

以精度为条件的 while 循环

是否可以在 C++ 中进行 while 循环,直到小数达到一定精度?我想对 arctan 和 pi 进行长时间的计算,并且我希望循环运行,直到使用公式 pi=4(arctan (1.0)) 将 pi 计算到小数点后第 10 位。我正在使用泰勒级数公式手动计算反正切。我知道这些计算有内置函数,但这是一项家庭作业,所以我必须这样做。我不是在寻找问题的解决方案,只是寻找使用精度的循环是否可行。谢谢!

编辑:

我仍然坚持这个!即使在仔细阅读了每个人的提示之后,我也无法为 while 循环提出正确的论点。真的需要帮助。这是我想出的代码:

#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main(void)
{
 int i;
 long double result;
 long double pi;
 int y=3;
 int loopcount=0;
 long double precision;

   cout<<"Start\n";

   result=1-(pow(1,y)/y);

  do
    {
     y=y+2;
     result=result+(pow(1,y)/y);
     y=y+2;
     result=result-(pow(1,y)/y);

      pi=4*(result);
      precision=(pi*(pow(10,11))/10);

      loopcount++;
    }
  while(//This is the problem!);

  cout<<"Final Arctan is:"<<endl;
     cout<<setprecision(20)<<result<<endl;
  cout<<"Final Pi is:"<<endl;
       cout<<setprecision(9)<<pi1<<endl;
  cout<<"Times looped:"<<endl;
       cout<<loopcount<<endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ math floating-point while-loop

1
推荐指数
1
解决办法
2121
查看次数

家庭作业再次请求帮助.递归地乘以Evens?

下面的引号是我的实际作业,下面是我当前的代码.有人能指出我正确的方向.

编写一个名为multiplyEvens的递归方法,它返回前n个偶数整数的乘积.例如,multiplyEvens(1)返回2,multiplyEvens(4)返回384(因为2*4*6*8 = 384).如果传递的值小于1,则抛出IllegalArgumentException.

  private static int multiplyEvens(int n)
  {  
      if(n%2 == 0)
      {           
          System.out.println(n*n);
          return multiplyEvens(n*n);
                      // I'm lost
      }
      System.out.println();
      return n; 
  }
Run Code Online (Sandbox Code Playgroud)

java math recursion

1
推荐指数
2
解决办法
3494
查看次数

计算Scheme中的对数

我有一个作业,我需要计算输入中的对数.

这是我到目前为止:

(define x 0)

(define number-of-pairs
  (lambda (v)
    (if (pair? v)
        (+ x 1)
        (+ x 0))))
Run Code Online (Sandbox Code Playgroud)

然后我用它如下:

(number-of-pairs (cons (cons 'a 'b) 'c))
Run Code Online (Sandbox Code Playgroud)

它应该产生2,但它产生1,因为它只通过函数一次.如果我试试

(number-of-pairs 10)
Run Code Online (Sandbox Code Playgroud)

因为没有对,所以它会产生0.

recursion scheme

1
推荐指数
1
解决办法
1117
查看次数

舍入错误:总和

我有一个大学powerpoint幻灯片声称做了一个继承的总和

1/i ^ 2

索引"i"从0到2260,不同于做相同数字的总和但从最大到最小(从1/2260 ^ 2到1/0 ^ 2).

试图在C上执行此操作,我有以下结果:

Increasing Order Sum: 1.644491e+00.
Decreasing Order Sum: 1.644491e+00.
Relative Error (abs(Incr. Sum - Decr. Sum) / abs(Incr. Sum)) : 2.700465e-15.
Run Code Online (Sandbox Code Playgroud)

有人能解释我为什么会这样吗?我对这些幻灯片没有解决方案.

提前致谢!:)

c math floating-point

1
推荐指数
1
解决办法
118
查看次数

在 Android 中安排活动

安排活动的最佳方式是什么?

我正在考虑新的日历 API,但它至少需要互联网连接才能创建新日历。

是否有任何 API 可以创建不需要互联网连接的时间表?

android scheduling android-calendar

1
推荐指数
1
解决办法
3103
查看次数

函数调用后字符串更改

我可能犯了一个非常明显的错误,但我看不到它,我希望有人可以帮助我.

我想在我的C++程序中将2个字符串添加到对向量中.容器看起来像vector< pair < word , type > > vec.这个词可以是任何东西,类型是名词,形容词,动词等.例如< Hogwarts , noun>,< Hungry, adjective>等等.

这是我在单词bank中添加单词的地方.FileStrings只是从File I/O创建的另一个容器.这里一切都很好.

for(unsigned int i = 0; i < fileStrings.size(); ++i)
{
    cout << "Adding " << fileStrings[i] << ", " << fileStrings[i + 1] << endl;
    theWordBank.addWord(fileStrings[i], fileStrings[++i]);
}
Run Code Online (Sandbox Code Playgroud)

这是我迷路的地方.打印出newWord时,它与newType完全相同.我无法理解它,我不明白newWord如何从上面的代码片段中失去它的价值.

void addWord(const string& newWord, const string& newType)
{
    mWords.push_back(make_pair(newWord, newType));
    std::cout << "newWord " << newWord << ", " << newType << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

再一次,结果应该是类似的< Hogwarts, noun> …

c++

1
推荐指数
1
解决办法
57
查看次数