zoukankan      html  css  js  c++  java
  • 谭浩强第六章习题答案

    谭浩强的第六章是循环,拿来练练手,程序是亲手写的,不一定是最优的,但一定对。另外12、13、15感觉比较扯就没写。

    下面是代码:

    //1.cpp
    #include <stdio.h>
    #define MAX(m,n) (((m) > (n) ? (m) : (n)))
    #define MIN(m,n) (((m) < (n) ? (m) : (n)))
    
    int main(void)
    {
    	int m, n;
    	int i;
    	int a, b;
    	printf("Input m and n:\n");
    	scanf("%d %d",&m,&n);
    	for(i=MIN(m,n);i>0;i--)
    	{
    		if(!(m%i) && !(n%i))
    			break;
    	}
    	a=i;
    	for(i=MAX(m,n);;i++)
    	{
    		if(!(i%m) && !(i%n))
    			break;
    	}
    	b=i;
    	printf("a=%d\tb=%d\n",a,b);
    	return 0;
    }
    //2.cpp
    #include <stdio.h>
    
    int main(void)
    {
    	char c;
    	int num=0, letter=0, blank=0, digit=0, other=0;
    
    	while((c=getchar()) != '\n')
    	{
    		num++;
    		if((c>='a' && c <='z') || (c>='A' && c<='Z'))
    			letter++;
    		else if(c>='0' && c <='9')
    			digit++;
    		else if(c==' ')
    			blank++;
    		else
    			other++;
    	}
    	printf("There are %d characters,%d letters,%d digits, %d blanks,%d other characters.\n)"
    		,num,letter,digit,blank,other);
    	return 0;
    }
    //3.cpp
    #include <stdio.h>
    
    int main(void)
    {
    	int a, n, tmp, sum;
    	a=n=sum=tmp=0;
    	printf("input a and n:\n");
    	scanf("%d %d", &a, &n);
    	for(int i=0; i<n; i++)
    	{
    		tmp = tmp * 10 + a;
    		sum += tmp;
    	}
    	printf("sum = %d\n", sum);
    	return 0;
    }
    //4.cpp
    #include <stdio.h>
    
    int main(void)
    {
    	int n;
    	long tmp=1, sum=0;
    	printf("input n:\n");
    	scanf("%d", &n);
    	for(int i=1; i<=n; i++)
    	{
    		tmp = tmp * i;
    		sum +=tmp;
    	}
    	printf("sum = %d\n", sum);
    	return 0;
    }
    
    //5.cpp
    #include <stdio.h>
    
    int main(void)
    {
    	int k, sum1=0;
    	float sum=0;
    	k = 100;
    	for(int i=1; i<=k; i++)
    	{
    		sum1 += i;
    	}
    	k = 50;
    	for(i=1; i<=k; i++)
    	{
    		sum1 += i * i;
    	}
    	k = 10;
    	for(i=1; i<=k; i++)
    	{
    		sum += 1.0 / i;
    	}
    	sum += sum1;
    	printf("sum = %.2f\n", sum);
    	return 0;
    }
    
    //6.cpp
    #include <stdio.h>
    
    int main()
    {
    	int num, tmp;
    	for(num=100; num<1000; num++)
    	{
    		tmp = (num/100)*(num/100)*(num/100) + (num%100/10)*(num%100/10)*(num%100/10) + (num%10)*(num%10)*(num%10);
    		if(num == tmp)
    			printf("%d\t",num);
    	}
    	printf("\n");
    	
    	return 0;
    }
    //7.cpp
    #include <stdio.h>
    
    int main()
    {
    	int i, j, k=0;
    	int num, sum=0;
    	int p[5];	//先运行一次根据k的值设置数组p的元素个数
    	printf("Input the max num:\n");
    	scanf("%d",&num);
    	for (i=1; i<=num; i++)
    	{
    		for (j=1; j<=i/2; j++)
    		{
    			if (!(i%j))
    			{
    				sum += j;
    			}
    			if(sum > i)
    				break;
    		}
    		if (sum == i)
    		{
    			p[k++] = i;
    		}
    		sum = 0;
    	}
    	for (i=0; i<k; i++)
    	{
    		printf("%d its factors are",p[i]);
    		for (j=1; j<=p[i]/2; j++)
    		{
    			if (!(p[i]%j))
    			{
    				printf(" %d,",j);
    			}
    		}
    		printf("\n");
    	}
    	//printf("k=%d",k);
    	//printf("\n");
    	return 0;
    }
    //8.cpp
    #include <STDIO.H>
    #define NUM 20
    
    int main()
    {
    	int a[NUM]={2,3};
    	int b[NUM]={1,2};
    	float sum=2.0+3.0/2;
    	for (int i=2; i<NUM; i++)
    	{
    		a[i] = a[i-1] + a[i-2];
    		b[i] = a[i-1];
    		sum += (float)a[i] / b[i];
    	}
    	printf("sum=%.3f\n",sum);
    	
    	return 0;
    }
    //9.cpp
    #include <STDIO.H>
    #define HIGHT 100
    
    int main()
    {
    	float h=HIGHT;
    	float sum=0.0;
    
    	for (int i=0; i<10; i++)
    	{
    		sum += h;
    		h = h / 2;
    	}
    	printf("sum=%.2fm,h=%.2fm\n",sum,h);
    	return 0;
    }
    //10.cpp
    #include <STDIO.H>
    
    int main()
    {
    	int num=1;
    	for (int i=1; i<10; i++)
    	{
    		num = (num + 1) * 2;
    	}
    	printf("num=%d\n",num);
    	
    	return 0;
    }
    //11.cpp
    #include <math.h>
    #include <stdio.h>
    
    int main()
    {
    	float x1,x2, a;
    	printf("Input a:\n");
    	scanf("%f",&a);
    	x1 = a / 2;
    	x2 = (x1 + a/x1) / 2;
    	while (fabs(x1-x2) >= 1e-5)
    	{
    		x1 = x2;
    		x2 = (x1 + a / x1) / 2;
    	}
    	printf("x=%.3f\n",x2);
    
    	return 0;
    }
    //14.cpp
    #include <STDIO.H>
    
    int main()
    {
    	int a,i,j,k;
    	for (i=0; i<4; i++)
    	{
    		for (j=0;j<3-i;j++)
    			printf("  ");
    		for(k=0;k<2*i+1;k++)
    			printf("* ");
    		printf("\n");
    	}
    	for(i=0; i<3; i++)
    	{
    		for (j=0; j<i+1; j++)
    			printf("  ");
    		for (k=0; k<5-2*i; k++)
    			printf("* ");
    		printf("\n");
    	}
    	return 0;
    }
    



  • 相关阅读:
    网络分析(二)定向与非定向
    Flex 找不到html文件,不能自动生成html问题解决
    常用的功能点记录
    git常规操作命令整理
    语境驱动测试7原则
    探索式测试的问与答
    测试建模:Google ACC
    探索式测试:基于测程的测试管理(SessionBased Test Management)
    用Excel展示SQL Server中的数据 (III): IronPython与自动化
    在Ajax中使用Flash实现跨域数据读取
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218674.html
Copyright © 2011-2022 走看看