Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用c++;_C_Loops_Nested Loops - Fatal编程技术网

用c++;

用c++;,c,loops,nested-loops,C,Loops,Nested Loops,有没有其他方法可以用更少的循环来完成这个程序呢?效率不高 #include <iostream> using namespace std; int main() { int i,j,n,s; cout<<"Enter the value of n"; cin>>n; for(i=1;i<=n;i++){ for(s=1;s<=n-i;s++){ cout<<"

有没有其他方法可以用更少的循环来完成这个程序呢?效率不高

#include <iostream>

using namespace std;

int main() {
    int i,j,n,s;
    cout<<"Enter the value of n";
    cin>>n;
    for(i=1;i<=n;i++){
        for(s=1;s<=n-i;s++){
            cout<<" ";
        }
        char ch=97;
        int k=1;
        for(j=1;j<=(i*2-1);j++) {
            if (k%2!= 0){
                cout <<ch;
                ch++;
            } else {
                cout<<" ";
            }
            k++;
        }
        cout<<endl;
    }
}

请尝试以下代码-

#include<stdio.h>
main()
{
    int i,j,k,num;
    printf("Enter the number of letter \n");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
            for(j=num-1;j>i;j--)
                    printf(" ");
            for(k=0;k<=i;k++)
                    printf("%c ",(97+k));
            printf("\n");
    }
}
#包括
int main(){
char*pat=“a b c d e f g h i j k l m n o p q r s t u v w x y z”;
inti,n;
printf(“输入n:”);
scanf(“%d”和“&n”);

对于(i=1;比什么更简单?看起来应该可以使用嵌套在另一个循环中的一个循环来实现。循环的数量更少?为什么不简单地以这种格式打印它们:p你的问题不清楚,但我想这可以通过一个循环,每行作为字符串来实现。请付出一些努力,我可以告诉你在哪里可以找到类似的代码E
#include<stdio.h>
main()
{
    int i,j,k,num;
    printf("Enter the number of letter \n");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
            for(j=num-1;j>i;j--)
                    printf(" ");
            for(k=0;k<=i;k++)
                    printf("%c ",(97+k));
            printf("\n");
    }
}
sathish@ubuntu:~/c/basics$ ./a.out 
Enter the number of letter 
4
   a 
  a b 
 a b c 
a b c d 
#include <stdio.h>

int main(){
    char *pat = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
    int i, n;
    printf("input n : ");
    scanf("%d", &n);
    for(i = 1;i<=n;++i)
        printf("%*s%.*s\n", n - i, "", (i << 1) - 1, pat);

    return 0;
}
#include <iostream>
#include <string>

using namespace std;

int main() {
    int n;
    cout << "Enter the value of n : ";
    cin >> n;
    string spaces(n, ' ');
    string pat("a b c d e f g h i j k l m n o p q r s t u v w x y z");
    for(int i=1;i<=n;i++){
        cout << spaces.substr(i);
        cout << pat.substr(0, (i << 1) -1);
        cout << endl;
    }
}