c++小程序

//输入一个华氏温度,要求输出摄氏温度。公式为 c=5(F-32)/9,取位2小数。 
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
	char c1 = 'C';
	char c2 = 'h';
	char c3 = 'i';
	char c4 = 'n';
	char c5 = 'a';
	c1 += 4;
	c2 += 4;
	c3 += 4;
	c4 += 4;
	c5 += 4;
	cout << c1 << c2 << c3 << c4 << c5 << endl;
	return 0;
}
//输入一个华氏温度,要求输出摄氏温度。公式为 c=5(F-32)/9,取位2小数。 
#include<iostream>
#include<iomanip>
using namespace std ;
int main()
{
    float f ;
    cin>>f ;
    float c = 5 * (f - 32) / 9 ;
    cout<<fixed<<setprecision(2)<<"c="<<c<<endl ;//fixed是小数点的意思,表示下面要对小数点后进行设置,setprecision是设置精度,括号中的数表示保留几位精度输出 
    //如果不用fixed,直接使用setprecision(int)则表示保留几位有效数字 
    return 0 ;
}
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	float x, y;
	cin >> x;
	if (x < 1)
		y = x;
	else if (x >= 1 && x < 10)
		y = 2 * x - 1;
	else
		y = 3 * x - 11;
	cout << y << endl;
	return 0;
}
//给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。 90分以及90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int score;
	char level;
	cin >> score;
	if (score >= 90)
		level = 'A';
	else if (score >= 80)
		level = 'B';
	else if (score >= 70)
		level = 'C';
	else if (score >= 60)
		level = 'D';
	else
		level = 'E';
	cout << level << endl;
	return 0;
}
/*题目描述:

给出一个不多于 5 位的整数,要求:

1、求出它是几位数 

2、分别输出每一位数字 

3、按逆序输出各位数字,例如原数为 321,应输出 123

输入:

一个不大于5位的数字

输出:

一共三行,第一行:位数 

第二行: 用空格分开的每个数字,注意最后一个数字后没有空格 

第三行: 按逆序输出这个数

样例输入:

12345

样例输出:

5

1 2 3 4 5

54321
*/

#include<iostream>
using namespace std;
int main()
{
	int a;
	cin >> a;
	int num[5];
	int i = 0;
	int count = 0;
	while (a)
	{
		++count;
		num[i] = a % 10;
		++i;
		a = a / 10;
	}
	cout << count << endl;
	for (i = count - 1; i >=0; --i)
	{
		if (i)
		{
			cout << num[i] << " ";
		}
		else
		{
			cout << num[i] << endl; //endl表示换行
		}
	}
	for (i = 0; i <= count - 1; ++i)
	{
		cout << num[i];
	}
	return 0;
}
#include<iostream>
using namespace std;
int main()
{
	int a;
	int out;
	cin >> a;
	if (a <= 100000)
	{
		out = a * 0.1;
	}
	else if (a > 100000 && a <= 200000)
	{
		out = 100000 * 0.1 + (a - 100000) * 0.075;
	}
	else if (a > 200000 && a <= 400000)
	{
		out = 100000 * 0.1 + (200000 - 100000) * 0.075 + (a - 200000) * 0.05;
	}
	else if (a > 400000 && a <= 600000)
	{
		out = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (a - 400000) * 0.03;
	}
	else if (a > 600000 && a <= 1000000)
	{
		out = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (a - 600000) * 0.015;
	}
	else
	{
		out = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (1000000 - 600000) * 0.015 + (a - 1000000) * 0.01;
	}
	cout << out << endl;
	return 0;
}
/*
题目描述
输入两个正整数m和n,求其最大公约数和最小公倍数。
输入
两个整数
输出
最大公约数,最小公倍数
样例输入
5 7
样例输出
1 35
*/

#include<iostream>
#include<vector>
using std::vector;
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	int bigger = a, smaller = b;
	int Appro_max = 1, multiple_min;
	if (a < b)
	{
		bigger = b;
		smaller = a;
	}
	vector<int> Approximation;
	for (int i = 1; i <= bigger; ++i)
	{
		if (bigger % i == 0)
		{
			Approximation.push_back(i);
		}
	}
	for (auto j = Approximation.end() - 1; j != Approximation.begin(); j--)
	{
		int it = (*j);
		if ((smaller % it) == 0)
		{
			Appro_max = it;
			break;
		}
	}
	multiple_min = a * b / Appro_max;
	cout << Appro_max << " " << multiple_min;
	return 0;
}
//宏定义
#include<iostream>
#include<vector>
using std::vector;
using namespace std;
#define qiuyu(x, y) x % y

int main()
{
	int a, b;
	cin >> a >> b;
	int result = qiuyu(a, b);
	cout << result;
	return 0;
}
/*
题目描述
三角形面积=SQRT(S*(S-a)*(S-b)*(S-c)) 其中S=(a+b+c)/2,a、b、c为三角形的三边。 定义两个带参的宏,一个用来求area, 另一个宏用来求S。 写程序,在程序中用带实参的宏名来求面积area。
输入
a b c三角形的三条边,可以是小数。
输出
三角形面积,保留3位小数
样例输入
3 4 5
样例输出
6.000
*/
#include<iostream>
#include<vector>
#include<math.h>
#include<iomanip>
using std::vector;
using namespace std;
#define S(a, b, c) (a + b + c)/2
#define Area(s, a, b, c) sqrt(s * (s - a) * (s - b) * (s - c))

int main()
{
	float a, b, c;
	cin >> a >> b >> c;
	float s = S(a, b, c);
	float area = Area(s, a, b, c);
	cout << fixed << setprecision(3) << area;
	return 0;
}
/*
题目描述
给年份year,定义一个宏,以判别该年份是否闰年。提示:宏名可以定义为LEAP_YEAR,形参为y,既定义宏的形式为 #define LEAP_YEAR(y) (读者设计的字符串)
输入
一个年份
输出
根据是否闰年输出,是输出"L",否输出"N"
样例输入
2000
样例输出
L
*/
#include<iostream>
#include<vector>
using std::vector;
using namespace std;
#define LEAP_YEAR(year) (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

int main()
{
	int a;
	cin >> a;
	if (LEAP_YEAR(a) == 1)
	{
		cout << "L";
	}
	else
	{
		cout << "N";
	}
	
	return 0;
}
/*
题目描述
请设计输出实数的格式,包括:⑴一行输出一个实数;⑵一行内输出两个实数;⑶一行内输出三个实数。实数用"6.2f"格式输出。
输入
一个实数,float范围
输出
输出3行,第一行打印一遍输入的数,第二行打印两遍,第三行打印三遍。 第二行和第三行,用空格分隔同一行的数字。 实数用"6.2f"格式输出。
样例输入
0.618
样例输出
  0.62
  0.62   0.62
  0.62   0.62   0.62
*/
#include<iostream>
#include<iomanip>
using namespace std;

void shuchu(float a, int number);
void shuchu(float a, int number)
{
	for (int i = 1; i < number; ++i)
	{
		cout << fixed << setprecision(2) << a;
		cout << " ";
	}
	cout << fixed << setprecision(2) << a;
	cout << endl;

}
int main()
{
	float a;
	cin >> a;
	
	
	shuchu(a, 1);
	shuchu(a, 2);
	shuchu(a, 3);
	return 0;
	
}
/*
题目描述
分别用函数和带参的宏,从三个数中找出最大的数。
输入
3个实数
输出
最大的数,输出两遍,先用函数,再用宏。 保留3位小数。
样例输入
1 2 3
样例输出
3.000
3.000
*/
#include<iostream>
#include<iomanip>
using namespace std;
#define Y(a, b, c) (a > b ? (a > c ? a : c):(b > c ? b : c))
double bijiao(double a, double b, double c);
double bijiao(double a, double b, double c)
{
	double max = a;
	if (a < b)
	{
		max = b;
	}
	if (max < c)
	{
		max = c;
	}
	return max;
}
int main()
{
	double max_1, max_2;
	double a, b, c;
	cin >> a >> b >> c;
	max_1 = bijiao(a, b, c);
	max_2 = Y(a, b, c);
	cout << fixed << setprecision(3) << max_1 << endl;
	cout << fixed << setprecision(3) << max_2 << endl;
	return 0;
	
}
/*
题目描述
输入三个字符串,按由小到大的顺序输出
输入
3行字符串
输出
按照从小到大输出成3行
样例输入
cde
afg
abc
样例输出
abc
afg
cde
*/

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
using std::vector;
int main()
{
	string line;
	vector<string> v1;
	while (getline(cin, line))
		v1.push_back(line);
	sort(v1.begin(), v1.end());
	for (auto it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << endl;
	}



	return 0;
	
}

 

/*
题目描述
求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字(n不超过20)。

输入
n

输出
Sn的值

样例输入
5
样例输出
153
*/
//注意sum采用long long 64位,int 16位
#include<iostream>

using namespace std;


int main()
{
	int n;
	cin >> n;
	long long sum = 0;   
	long long factorial = 1;
	for (int i = 1; i <= n ; i++)
	{
		factorial = 1;
		for (int j = 1; j <= i; j++)
		{
			factorial *= j;
		}
		sum += factorial;
	}
	cout << sum;


	return 0;
	
}
/*
题目描述
打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身。 例如:153是一个水仙花数,因为153=1^3+5^3+3^3。 
输入
无
输出
无
样例输入
无
样例输出
无
*/
#include<iostream>
#include "math.h"
using namespace std;


int main()
{
	int hundreds, tens, ones;
	for (int i = 100; i <= 999; i++)
	{
		
		hundreds = i / 100;
		tens = (i % 100) / 10;
		ones = (i % 100) % 10;
		if (pow(hundreds, 3) + pow(tens, 3) + pow(ones, 3) == i)
		{
			cout << i << endl;
		}
	}

	return 0;
	
}
/*
题目描述
用简单素数筛选法求N以内的素数。
输入
N
输出
2~N的素数
样例输入
100
样例输出
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
*/
//参考:https://www.cnblogs.com/wpnan/p/4073852.html
#include<vector>
#include<iostream>
#include "math.h"
using namespace std;


int main()
{
	vector<bool> data;
	vector<int> prime;
	int MAX;
	cin >> MAX;
	for (int i = 0; i <= MAX; i++)
	{
		if (i%2 == 0)
		{
			data.push_back(false);
		}
		else
		{
			data.push_back(true);
		}
	}
	for (int j = 3; j <= sqrt(MAX); j++)
	{
		if (data[j] == true)
		{
			for (int i = j + 1; i <= MAX; i++)
			{
				if (i % j == 0)
				{
					data[i] = false;
				}
			}
		}
	}
	data[2] = true;
	for (int i = 2; i <= MAX; i++)
	{
		if (data[i])
		{
			cout << i << endl;
		}
	}
	return 0;
	
}
/*
题目描述
用选择法对10个整数从小到大排序。
输入
无
输出
排序好的10个整数
样例输入
4 85  3 234 45 345 345 122 30 12
样例输出
3
4
12
30
45
85
122
234
345
345
*/
//参考:https://www.cnblogs.com/mrxy/p/8037149.html
#include<vector>
#include<iostream>
#include "math.h"
#include<string>
#include<algorithm>

using namespace std;




int main()
{
	int MIN;
	int index;
	int data[10];
	int middle;
	for (int i = 0; i < 10; i++)
	{
		cin >> data[i];
	}
	for (int i = 0; i < 9; i++)
	{
		MIN = data[i];
		index = i;
		for (int j = i + 1; j < 10; j++)
		{
			if (data[j] < data[i])
			{
				middle = data[i];
				data[i] = data[j];
				data[j] = middle;
			}
		}
	}
	for (int i = 0; i < 10; i++)
	{
		cout << data[i] << endl;
	}
	return 0;

}
/*
题目描述
求一个3×3矩阵对角线元素之和。
输入
矩阵
输出
主对角线 副对角线 元素和
样例输入
1 2 3
1 1 1
3 2 1
样例输出
3 7
*/
#include "stdafx.h"
#include<vector>
#include<iostream>
#include "math.h"
#include<string>
#include<algorithm>

using namespace std;




int main()
{
	int diag_1 = 0, diag_2 = 0, data[3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			scanf("%d", &data[i][j]);
		}
	}
	for (int i = 0; i < 3; i++)
	{
		diag_1 += data[i][i];
		diag_2 += data[i][2 - i];
	}
	cout << diag_1 << " " << diag_2 << endl;
	return 0;

}
/*
题目描述
已有一个已排好的9个元素的数组,今输入一个数要求按原来排序的规律将它插入数组中。
输入
第一行,原始数列。 第二行,需要插入的数字。
输出
排序后的数列
样例输入
1 7 8 17 23 24 59 62 101
50
样例输出
1
7
8
17
23
24
50
59
62
101
*/
#include<vector>
#include<iostream>
#include<list>
#include "math.h"
#include<string>
#include<algorithm>

using namespace std;




int main()
{
	int new_data;
	int example;
	list<int> data;
	for (size_t i = 0; i < 9; i++)
	{
		cin >> example;
		data.push_back(example);
	}
	cin >> new_data;
	for (auto i = data.begin(); i != data.end(); i++)
	{
		if (new_data < *i)
		{
			data.insert(i, new_data);
			break;
		}
	}
	for (auto i = data.begin(); i != data.end(); i++)
	{
		cout << *i << endl;
	}
	
	return 0;

}
/*
题目描述
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
输入
一行字符串
输出
统计数据,4个数字,空格分开。
样例输入
!@#$%^QWERT    1234567
样例输出
5 7 4 6
*/
#include <stdio.h>

int main() {
	int word = 0, number = 0, space = 0, others = 0;
	int ch;
	while ((ch = getchar()) != EOF) {
		if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') {
			word++;
		}
		else if ('0' <= ch && ch <= '9') {
			number++;
		}
		else if (ch == ' ') {
			space++;
		}
		else {
			others++;
		}
	}
	printf("%d %d %d %d", word, number, space, others);
	return 0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值