小编And*_*̷y̷的帖子

用while编写一个简单的函数

Python家庭作业分配要求我写一个函数"将输入作为正整数,并打印出一个乘法,表格显示所有整数乘法,包括输入数字."(也使用while循环)

 # This is an example of the output of the function
print_multiplication_table(3)
>>> 1 * 1 = 1
>>> 1 * 2 = 2
>>> 1 * 3 = 3
>>> 2 * 1 = 2
>>> 2 * 2 = 4
>>> 2 * 3 = 6
>>> 3 * 1 = 3
>>> 3 * 2 = 6
>>> 3 * 3 = 9
Run Code Online (Sandbox Code Playgroud)

我知道如何开始,但不知道接下来该做什么.我只需要一些算法帮助.请不要写正确的代码,因为我想学习.而是告诉我逻辑和推理. 这是我的推理:

  1. 该函数应将所有实数乘以给定值(n)乘以1小于n或(n-1)
  2. 该函数应将所有实数乘以n(包括n)乘以小于n或(n-2)的两倍
  3. 该函数应将所有实数乘以n(包括n)乘以小于n或(n-3)的三倍,依此类推......直到达到n
  4. 当函数达到n时,该函数还应将所有实数乘以n(包括n)乘以n
  5. 然后该函数应该停止或在while循环中"中断"
  6. 然后该函数必须打印结果

所以这就是我到目前为止所做的:

def print_multiplication_table(n): …
Run Code Online (Sandbox Code Playgroud)

python

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

如何在C中返回两个值

我有以下功能:

int conMS(time_in_seconds) {
  int minutes, seconds;
  minutes = time_in_seconds / 60;
  seconds = time_in_seconds % 60;
  return minutes, seconds;
}
Run Code Online (Sandbox Code Playgroud)

当在另一个函数中使用时,我收到几条错误消息:

warning: left-hand operand of comma expression has no effect [-Wunused-value]
warning: left-hand operand of comma expression has no effect [-Wunused-value]
 minutes,seconds = conMS(time);
warning: ‘minutes’ is used uninitialized in this function [-Wuninitialized]
warning: left-hand operand of comma expression has no effect [-Wunused-value]
 return minutes, seconds;
Run Code Online (Sandbox Code Playgroud)

无论如何,有没有办法可以从函数返回两个值.这两个值可以是任何东西:a int和a char,a float和a int......

我很抱歉,如果这对你来说很简单,但我是C的初学者,我能学到的唯一方法就是提问.另外,请尽量简化您的解释.

更新:这可以通过指针轻松完成,如下所示.

void …
Run Code Online (Sandbox Code Playgroud)

c

7
推荐指数
3
解决办法
6203
查看次数

列表中是否有任何数字?

我有一个清单:

my_list = [12,8,0,4,7,21,27,"O",29,3,"X","O","X","X"]
Run Code Online (Sandbox Code Playgroud)

该列表在整个计划中得到更新.每次更新时,我都要查看它是仅包含数字还是仅包含字符串,或两者都包含.我可以用它做什么? 我需要检查列表中的所有元素.有什么建议吗?**

**有关代码如何工作的说明请.

谢谢大家的帮助.感谢您的帮助.

python python-2.7 python-3.x

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

C中字符的大小

我有:

#include <stdio.h>
int main()
{
    char ch[] = "Hello";
    char wd[] = "World";
    char ex[] = "!";

    printf("The size of a char: %ld\n",sizeof(char));
    printf("The size of ch[]: %ld\n",sizeof(ch));
    printf("The size of ex[]: %ld\n",sizeof(ex));
    printf("The size of wd[]: %ld\n",sizeof(wd));

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

产量:

The size of a char: 1
The size of ch[]: 6
The size of ex[]: 2
The size of wd[]: 6
Run Code Online (Sandbox Code Playgroud)

我的问题:由于char的大小是1个字节,为什么ch []的大小不是5个字节?因为它有5个字符(H,e,l,l和o)
与wd []和ex []相同.这里发生了什么?
对不起,如果这是一个很容易的,但我是C的新手.

c sizeof char

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

不是SO奇怪的C行为

我正在玩C,并决定建立我自己的功能strlen()而且我{}做了.但通过这样做,我很震惊地找到了可以做的事情.这是我的代码:

#include <stdio.h>
#include <assert.h>

int mystrlen(char *s)
{
    int i = 0;
    assert(s[0] !='\0');

    for(i = 0; s[i] !='\0'; i++)
    // Placing {} here causes the error to disappear //
    return i;
}

int main(void)
{
    char hello[] = "hello";
    int len = mystrlen(hello);
    printf("Length = %d\n", len);

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

运行此代码会产生此错误:

mystrlen.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]
Run Code Online (Sandbox Code Playgroud)

但在 {} while循环之后放置后不再有错误.任何人都可以向我解释这种行为吗?

c loops for-loop

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

重载输入流运算符>>

我试图重载>>运算符,以便当用户输入分数时,cin会将分数存储到类型为Fraction的Object中.
摘自头文件:

// Calculator.h
friend istream &operator>>( istream &, Fraction &);

private:
    signed int numo; // fraction numerator
    signed int deno; // fraction denomenator
    char ch; // the '/' character
Run Code Online (Sandbox Code Playgroud)

摘自实施文件:

 //Calculator.cpp

// insertion operator overloading
istream &operator>>( istream &input, Fraction fraction)
{
    input >> fraction.numo;
    input >> fraction.ch;
    input >>fraction.deno;

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

当我尝试编译Calculator.cpp并在另一个文件中编译驱动程序函数时,我遇到了很多错误.为什么这不起作用?请彻底解释你的答案,我想学习.

更新:
**错误:变量,numo deno和ch是'私有'

c++

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

标签 统计

c ×3

python ×2

c++ ×1

char ×1

for-loop ×1

loops ×1

python-2.7 ×1

python-3.x ×1

sizeof ×1