1 条题解

  • 0
    @ 2025-10-10 15:45:26

    C :

    #include<stdio.h>
    #include<math.h>
    int main()
    {
    	int n;
    	float sum=0;
    	scanf("%d",&n);
    	int i=1;
    	for(i=1;sum<=n;i++)
        {
           sum=sum+1.0/i;
        }
        i--;
        printf("%d",i);
        return 0;
    }
    

    C++ :

    #include <iostream>  
    using namespace std;  
    int main(){  
        int i = 1,n,x;
        double s = 0;
        cin>>x;
         while(s <= x){ 
       s = s + 1.0 / i;
       i++;
    }
    cout<<i - 1<<endl;
        return 0;  
      } 
    
    

    Pascal :

    var x,i:longint; s:real;
    begin
      read(x);
      s:=0;
      i:=1;
      while s<=x do
       begin
        s:=s+1/i;
        i:=i+1;
       end;
      writeln(i-1);
    end.
    
    

    Java :

    import java.util.Scanner;
    public class Main{
    	public static void main(String[] args) {
    			Scanner scanner=new Scanner(System.in);
    			int x=scanner.nextInt();
    			float s=0;
    			for(int n=1;;n++){
    			s=s+(float)1/n;
    			if(s>=x){
    				System.out.println(n);
    				break;
    			}
    		}
    	}
    }
    

    Python :

    x = int(input())
    s = 0
    i = 1
    while True:
        s += 1 / i
        if s > x:
            print(i)
            break
        i += 1
    
    • 1

    【入门】求恰好使s=1+1/2+1/3+…+1/n的值大于X时n的值。

    信息

    ID
    77
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    10
    已通过
    3
    上传者