zoukankan      html  css  js  c++  java
  • 爬动的蠕虫、二进制的前导的零、求组合数、Have Fun with Numbers、近似求PI

    7-46 爬动的蠕虫 (15 分)

    一条蠕虫长1寸,在一口深为N寸的井的底部。已知蠕虫每1分钟可以向上爬U寸,但必须休息1分钟才能接着往上爬。在休息的过程中,蠕虫又下滑了D寸。就这样,上爬和下滑重复进行。请问,蠕虫需要多长时间才能爬出井?

    这里要求不足1分钟按1分钟计,并且假定只要在某次上爬过程中蠕虫的头部到达了井的顶部,那么蠕虫就完成任务了。初始时,蠕虫是趴在井底的(即高度为0)。

    输入格式:
    输入在一行中顺序给出3个正整数N、U、D,其中D<U,N不超过100。

    输出格式:
    在一行中输出蠕虫爬出井的时间,以分钟为单位。

    输入样例:

    12 3 1
    

    输出样例:

    11
    
    #include <stdio.h>
    int main()
    {
    	int N,U,D,t=0,sum=0;
    	scanf("%d %d %d",&N,&U,&D);
    	while(1){
    		t++;//上爬1分钟 
    		sum=sum+U;
    		if(sum>=N){
    			printf("%d
    ",t);
    			break;
    		}
    		t++;//休息1分钟 
    		sum=sum-D;
    	}
    	return 0;
    }
    

    7-47 二进制的前导的零 (10 分)

    计算机内部用二进制来表达所有的值。一个十进制的数字,比如24,在一个32位的计算机内部被表达为00000000000000000000000000011000。可以看到,从左边数过来,在第一个1之前,有27个0。我们把这些0称作前导的零。

    现在,你的任务是写一个程序,输入一个整数,输出在32位表达下它前导的零的个数。

    输入格式:
    一个整数,在32位的整数可以表达的范围内。

    输出格式:
    一个整数,表达输入被表达为一个32位的二进制数时,在第一个1之前的0的数量。

    输入样例:

    256
    

    输出样例:

    23
    
    #include <stdio.h>
    
    int main () {
        int i, input;
        long int temp=1;
        scanf("%d",&input);
        while (temp <= input){
            i++;
            temp *= 2;
        }
        if (input < 0 )         //负数情况下
            printf("0");
        else{
             printf("%d",32 - i);
        }   
        return 0 ;
    }
    

    7-48 求组合数 (15 分)

    本题要求编写程序,根据公式C​n​m​ =​n!/m!(n−m)!​​​​​ 算出从n个不同元素中取出m个元素(m≤n)的组合数。

    建议定义和调用函数fact(n)计算n!,其中n的类型是int,函数类型是double。

    输入格式:
    输入在一行中给出两个正整数m和n(m≤n),以空格分隔。

    输出格式:
    按照格式“result = 组合数计算结果”输出。题目保证结果在double类型范围内。

    输入样例:

    2 7
    

    输出样例:

    result = 21
    
    #include <stdio.h>
    int main()
    {
    	double fact(int n);
    	int m,n;
    	scanf("%d %d",&m,&n);
    	double sum;
    	sum = fact(n)/(fact(m)*fact(n-m));
    	printf("result = %.0f",sum);//%。0f指定输出时不要小数部分
    	return 0;
     } 
     double fact(int n){//求阶乘 
     	double sam=1;//应为0!=1,1!=1,这里pro的初始值设为1,是为了处理传入0的情况
     	int i;
     	for(i=1;i<=n;i++){
     		sam = sam*i;
    	}
    	return sam;
     }
    

    7-49 Have Fun with Numbers (20 分)

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

    Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

    Input Specification:
    Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

    Output Specification:
    For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

    Sample Input:

    1234567899
    

    Sample Output:

    Yes
    2469135798
    
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	
    	char c[25];//接收初始值
    	int num[25];//存取加倍后数据
    	int flag[10] = {0};//记录初始值每个数字出现次数
    	int flag2[10] = { 0 };//记录加倍后每个数字出现次数
    	int p = 0;//进位值
    	int count = 0, t, flag1 = 0;
    	gets(c);
    	for (int i = strlen(c) - 1; i >= 0; i--)
    	{
    		t = (c[i] - '0');
    		flag[t]++;//记录初始值每个数字出现次数
    		t = (t * 2 + p);
    		num[count++] = t % 10;
    		p = t / 10;
    	}
    	if (p)//还有进位
    		num[count++] = p;
    	for (int i = 0; i < count; i++)
    		flag2[num[i]]++;//记录加倍后每个数字出现次数
    	for (int i = 0; i < count; i++)
    	{
    		if (flag[num[i]] != flag2[num[i]])//数字出现次数不同
    		{
    			printf("No
    ");
    			flag1 = 1;
    			break;
    		}
    	}
    	if (!flag1)
    		printf("Yes
    ");
    	for (int i = count-1; i >= 0; i--)
    		printf("%d",num[i]);
    	return 0;
    }
    

    7-50 近似求PI (15 分)

    本题要求编写程序,根据下式求π的近似值,直到最后一项小于给定精度eps。
    01

    输入格式:
    输入在一行中给出精度eps,可以使用以下语句来读输入:

    scanf("%le", &eps);
    输出格式:
    在一行内,按照以下格式输出π的近似值(保留小数点后5位):

    PI = 近似值
    输入样例:

    1E-5
    

    输出样例:

    PI = 3.14158
    

    思路:只要最后一项小于给定精度 就跳出循环 保留五位小数输出即可

    #include <stdio.h>
    int main()
    {
    	double eps,i,temp=1,sum=1;
    	scanf("%le", &eps);
    	for(i=1;temp>eps;i++){
    		temp = temp*i/(2*i+1);
    		sum = sum + temp;
    	}
    	printf("PI = %.5f
    ",2*sum);
    	return 0;
    }
    
    欢迎查阅
  • 相关阅读:
    HDU 5640 King's Cake
    HDU 5615 Jam's math problem
    HDU 5610 Baby Ming and Weight lifting
    WHU1604 Play Apple 简单博弈
    HDU 1551 Cable master 二分
    CodeForces659C Tanya and Toys map
    Codeforces 960E 树dp
    gym 101485E 二分匹配
    Codeforces 961E 树状数组,思维
    Codeforces Round #473 (Div. 2) D 数学,贪心 F 线性基,模板
  • 原文地址:https://www.cnblogs.com/gh110/p/11892799.html
Copyright © 2011-2022 走看看