C语言编程练习(三)(VS2015)

转载自:https://blog.csdn.net/xia0liang/article/details/53157455

【程序14】
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

1.程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n〈〉k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	int n, i;
	scanf("%d", &n);
	printf("n=");
	for (i = 2; i <= n; i++)
	{
		while (n != i)
		{
			if (n%i == 0)
			{
				printf("%d*", i);
				n = n / i;
			}
			else
				break;
		}
	}
	printf("%d", n);
	system("pause");
}

【程序15】
题目:利用条件运算符的嵌套来完成此题:学习成绩〉=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
1.程序分析:(a〉b)?a:b这是条件运算符的基本例子。
2.程序源代码:

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	int score;
	char grade;
	printf("please input a score\n");
	scanf("%d", &score);
	grade = score >= 90 ? 'A' : (score >= 60 ? 'B' : 'C');
	printf("%d belongs to %c", score,grade);
	system("pause");
}

【程序16】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。

2.程序源代码:

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	int a, b, num1, num2, temp;
	printf("please input two numbers:\n");
	scanf("%d,%d",&num1,&num2);
	if (num1 < num2)
	{
		temp = num1;
		num1 = num2;
		num2 = temp;
	}
	a = num1; b = num2;
	while (b != 0)
	{
		temp = a%b;
		a = b;
		b = temp;
	}
	printf("公约数:%d\n", a);
	printf("公倍数:%d\n", num1*num2 / a);
	system("pause");
}

【程序17】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为’\n’.     
2.程序源代码:

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	char c;
	int letters = 0, space = 0, digit = 0, others = 0;
	printf("please input some characters\n");
	while ((c = getchar()) != '\n')
	{
		if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')
			letters++;
		else if (c == ' ')
			space++;
		else if (c >= '0'&&c <= '9')
			digit++;
		else
			others++;
	}
	printf("输入的字符串中letters=%d space=%d digit=%d others=%d\n", letters, space, digit, others);
	system("pause");
}

【程序18】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	int a, n, count = 1;
	long int sn = 0, tn = 0;
	printf("please input a and n\n");
	scanf("%d,%d", &a, &n);
	printf("a=%d,n=%d\n", a, n);
	while (count <= n)
	{
		tn = tn + a;
		sn = sn + tn;
		a = a * 10;
		count++;
	}
	printf("a+aa+…=%ld\n", sn);
	system("pause");
}

【程序19】
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程找出1000以内的所有完数。
1. 程序分析:请参照程序14.(与原作者的编程不同)
2.程序源代码:

#include "stdio.h"
#include "stdlib.h"
//#include <windows.h>
int main()
{
	//SetConsoleOutputCP(437);
	int i, j, sum;
	for (i = 2; i <= 1000; i++)
	{
		sum = 0;
		for (j = 1; j<i; j++)
		{
			if (i%j == 0)
			{
				sum += j;
			}
		}
		if (sum == i)
			printf("It's a Perfect n:%d\n", i);
	}
	system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值