1 条题解

  • 0
    @ 2023-6-11 12:21:13

    C :

     #include <stdio.h>
      #include <string.h>
     
     int main()
      {
          int n, i, j;
         scanf("%d", &n);
         char str[20000] = "0", s[20000];
         while (n--)
        {
             for (i = 0, j = 0; str[i]; i++, j++)
            {
                 if (str[i] == '0')
                {
                     s[j] = '1';
                 }
                else if (str[i] == '1')
               {
                     s[j] = '0';
                     s[++j] = str[i];
                 }            
            }
             s[j] = '\0';
            strcpy(str, s);
        }
        puts(str);
        return 0;
    }
    

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    	string s = "0",t;
    	int n,i,j;
    	cin>>n;
    	for(i=1;i<=n;i++){
    		t = "";
    		for(j=0;j<s.size();j++){
    			if(s[j]=='0'){
    				t = t + '1'; 
    			}else if(s[j]=='1'){
    				t = t + "01";
    			}
    		} 
    		s = t;
    	}
    	cout<<s;
    	return 0;
    }
    
    

    Java :

    
    import java.util.Scanner;
     
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		String a = "0";
    		while (n > 0) {
    			String b = "";
    			for (int i = 0; i < a.length(); i++) {
    				if (a.charAt(i) == '0')
    					b += "1";
    				else if (a.charAt(i) == '1')
    					b += "01";
    			}
    			a = b;
    			n--;
    		}
    		System.out.println(a);
    	}
    }
    
    • 1

    信息

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