1 条题解

  • 0
    @ 2023-6-11 12:16:29

    C :

    #include<stdio.h>
    void main()
    {
    	int n;
    	scanf("%d",&n);
    	int a,b;
    	for(a=1;a<=n/2+1;a++){
    		
    		for(b=1;b<=a;b++){
    			printf("*");
    		}
    	
    		for(b=1;b<=2*(n/2-a)+2;b++){
    			printf(" ");
    		}
    		for(b=1;b<=a;b++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	for(a=1;a<=n/2;a++){
    		for(b=1;b<=n/2-a+1;b++){
    			printf("*");
    		}
    		for(b=1;b<=2*a;b++){
    			printf(" ");
    		}
    		for(b=1;b<=n/2-a+1;b++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	
    }
    

    C++ :

    #include <bits/stdc++.h>
     
    using namespace std;
     
    int main(){
        int n,i,j;
        cin>>n;
        n=n/2+1;
        for(i=1;i<=n;i++){
        	for(j=1;j<=i;j++){
        		cout<<"*";
    		}
    		for(j=1;j<=(n-1)*2+1-2*i+1;j++){
    			cout<<" ";
    		}
    		for(j=1;j<=i;j++){
    			cout<<"*";
    		}
    		cout<<endl;
    	}
    	for(i=n-1;i>=1;i--){
        	for(j=1;j<=i;j++){
        		cout<<"*";
    		}
    		for(j=1;j<=(n-1)*2+1-2*i+1;j++){
    			cout<<" ";
    		}
    		for(j=1;j<=i;j++){
    			cout<<"*";
    		}
    		cout<<endl;
    	}
    } 
    

    Python :

    n=int(input())
    n1=n//2
    for i in range(-n1,n1+1):
        if(i!=0):
            print("*"*(n1-abs(i)+1),end="")
            for j in range(1,abs(i)*2+1):
                print(" ",end="")
            print("*"*(n1-abs(i)+1),end="")    
            print()
        else:
            print("*"*(n1+1)*2)
    
    • 1

    【入门】打印n行的完整的蝴蝶结

    信息

    ID
    2174
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者