Tag: 循环

GCC 5.1循环展开

给出以下代码 #include int main(int argc, char **argv) { int k = 0; for( k = 0; k < 20; ++k ) { printf( "%d\n", k ) ; } } 使用GCC 5.1或更高版本 -xc -std=c99 -O3 -funroll-all-loops –param max-completely-peeled-insns=1000 –param max-completely-peel-times=10000 部分循环展开,它将循环展开十次然后进行条件跳转。 .LC0: .string “%d\n” main: pushq %rbx xorl %ebx, %ebx .L2: movl %ebx, %esi movl $.LC0, […]

kbhit()双循环不能正常工作

只是为了好玩,我尝试用循环打印kbhit() ,以便按下按键后的程序无限打印该行,直到再次按下键盘。 它编译得很好,在运行时,只是给出了空白屏幕。 没有打印。 但是在单键按下结束程序。 虽然控制台没有关闭。 #include #include int main() { while(1) { if(kbhit()) { while(1) { if(kbhit()) { goto out; } printf(“Print Ed Infinitum Until Key Press”); } } } out: return 0; } 我该如何解决这个问题?

在C中制作金字塔

我必须使用C,而不是C ++制作金字塔。 我要做的就是这个,我有一个字符串“这是一个字符串”(是的,空间假设在那里),我必须从两边“删除”一个字母,并打印出来。 像这样 “this is a string ” “his is a string” “is a strin” “sa stri” ” a str” 重复此过程,直到没有更多字符。 我的老师说要使用子串,C中有没有说过的东西 从位置打印(索引0到索引x) 从位置打印(索引1到索引x-1) 我知道我必须使用for循环。

读写矩阵

我写了这段代码来输入r * c矩阵 r – >行c – >列 当r c时无法产生正确的输出。 例: r = 6,c = 2 输入:1 2 3 4 5 6 7 8 9 10 11 12 输出: 1 2 11 12 5 6 11 12 11 12 11 12 请告诉我我哪里出错了。 我不想使用恒定大小的数组。 #include void scanmatrix(int* a[],int r,int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) […]

打印无循环

可能重复: 在没有循环或条件的情况下打印1到1000 #include #include int fun(int n) { –n && fun(n); return printf( “\n%d”, n+1); } int main(void) { fun(1000); getch(); return 0; } 这是一个不使用循环或if-else打印1到1000的程序。 是否有其他方法可以不使用循环或if-else。

将此代码行转换为C.

我有以下代码行: for ( int i = index; i size; ++i ) //i,index and size are integers.al is an arraylist 当我在C中编译它时,我得到错误: ‘for’ loop initial declarations are only allowed in C99 mode 我不知道如何解决这个问题。 谢谢!

将两个矩阵乘以不同的维度

我正在编写一个乘法矩阵的应用程序。 这适用于nxn矩阵a和b的预期: for(k = 0; k < n; k++) { for(i = 0; i < n; i++) { tmp = a[i][k]; for(j = 0; j < n; j++) { c[i][j] = c[i][j] + tmp * b[k][j]; } } } 如果a是nxy且b是yxm(暗示c是nxm)。 如何修改上述循环才能工作? 谢谢

用于查找五次多项式的一个根的代码

我正在尝试编写一个代码,要求用户为5度多项式提供5个系数,并且还要求给出一个范围(两个值)程序检查是否存在解决方案(我被要求只找到一个),解决方案必须是一个整数,而系数可以是浮点数。 我正在考虑编写一个代码,该代码运行在该范围内的每个整数上,并将其替换为多余的描述,而不是我定义的多项式,并检查它是否等于零,但我决定如何制作循环。 另外,如果用户输入的区间中有多个根,那么我们必须打印根的最小值(但我也没有指示如何做到这一点)。 我将告诉你到目前为止我所写的内容,我们将不胜感激任何一种帮助: #include #define zero 0.00001 int main() { double a, b, c , d , e , f , y , beginning_of_range, end_of_range; int x; printf(“please enter the coefficients of the polynomial:\n”); scanf(“%lf%lf%lf%lf%lf”, &a, &b, &c, &d, &e); printf(“please enter two values to indicate the beginning and end of range:\n”); scanf(“%lf%lf”, &beginning_of_range, […]

简化字符串迭代

我正处于学习编码的初期(特别是C)。 在编写一个计算数组字符的函数用于学习目的时,我正在质疑自己(我很确定),如果有可能简化这个迭代: int stringlength(char* s) { int i = 0; while (s != NULL && *(s + i) != ‘\0’) i++; return i; } 我想将i++保留在迭代本身内(for循环?)。 我很欣赏你们给我的任何暗示。 如果你发现一些与我无关的问题无关 – 请告诉我。

Scanf跳过循环(Hangman)

该程序基本上要求一个秘密字符串,然后要求用户重复猜测该字符串的单个字符,直到他猜到这一切。 然而,它每隔一次运行while循环就会工作,它会跳过猜测字符的用户输入。 我该如何解决? int main(){ char guess; char test2 [50]; char * s = test2; char output [50]; char * t = output; printf(“Enter the secret string:\n”); fgets(test2, 50, stdin); for (int i=0;i<49;i++){ //fills ouput with _ spaces *(output +i)='_'; while(strcmp(s,t) != 0){ printf("Enter a guess:"); scanf("%c",&guess); printf("You entered: %c\n", guess); showGuess(guess,s, t ); // […]