标签:数学 png highlight 存在 如何 ret ace string 额外
(一)题1:

解题思路:通过二维数组存取输入数据,之后通过一个函数判断是否存在‘E‘‘A‘‘S‘‘Y‘四个字母,最后根据返回值打印difficult/easy
如何判断:传入二维数组的每一行(一行等于一个输入数据),定义2个指针,然后while循环读取
代码:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std;
bool match(char a[]){
int i = 0, j = 0;
char temp[] = {‘E‘, ‘A‘, ‘S‘, ‘Y‘};
while( i<1000&&j<=3 ){
if( a[i]==temp[j] ){
i++;
j++;
}else{
i++;
}
}
if( i>=1000 ){
return false;
}else{
return true;
}
}
int main(void){
cout << "输入字符串个数:" << " ";
int n;
cin >> n;
cout << "Sample In:" << endl;
char str[][1000] = {‘0‘};
for(int i=0; i<n; i++){
cin >> str[i];
}
cout << "Sample Out:" << endl;
for(int i=0; i<n; i++){
if(match(str[i])){
cout << "easy" << endl;
}else{
cout << "difficult" << endl;
}
}
return 0;
}
(二)题2:

解题思路:这道题目更像是一道数学题,找规律,找小三角形数目

代码:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std;
int main(void){
cout << "输入几组数据:" << " ";
int n;
cin >> n;
int a[n];
cout << "Simple In:" << endl;
for(int i=0; i<n; i++){
cin >> a[i];
}
cout << "Simple Out:" << endl;
for(int i=0; i<n; i++){
if( a[i]<3 ){
cout << "边数输入错误" << endl;
}
if( a[i]%2==0 ){
cout << (2*a[i]-4)*0.5 << endl;
}
if( a[i]%2!=0 ){
cout << (2*a[i]-5)*0.5 << endl;
}
}
return 0;
}
(三)题3:

解题思路:这道题目首先通过定义二维数组(2行N列)存储数据,第一行存储每个人的折扣率,第二行存储每个人的折扣上限
然后需要根据折扣率进行排序,从小到大(折扣率越小,折扣力度越大),根据总价格t分类,如果t超过所有人的折扣
上限之和,那么肯定需要额外掏钱,否则则根据排序结果进行依次扣钱
代码:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std;
//数组a已经根据折扣率从小到大排序完成
int zhekou(double a[2][100], int n, double t){
int sum1 = 0;
int sum2 = 0;
int sum = 0;//一共应该付多少钱
for(int i=0; i<n; i++)
sum1 += a[1][i];//计算全部折扣金额上限
if( sum1>t ){//全部折扣金额>总价格,说明可以用折扣付钱
for(int j=0; j<n-1; j++){//从折扣力度最大到倒数第二个
t -= (int)a[1][j];//每次都去掉当前这个人的折扣上限
if(t>0){//如果不够付
sum += (int)(a[1][j]*a[0][j]);//sum加上折扣后的钱
}
int i = j+1;//下一个人
if( i<n && t<a[1][i] ){//如果正好下一个人够付了
sum += (int)(t*a[0][i]);//只需要加上需要付的那部分折扣后的钱
}
}
}else{//全部折扣金额<总价格,说明要额外加钱
for(int j=0; j<n; j++)
sum2 += (int)(a[1][j]*a[0][j]);//全部折扣金额(折扣后的钱)
t = t - sum1;//去除全部折扣金额
sum = sum2 + t;//额外的钱要加上
}
return sum;
}
int main(){
int N;//N个人
double T;//总价T
cout << "输入总人数:" << " ";
cin >> N;
cout << "输入总价格:" << " ";
cin >> T;
double a[2][100];
for(int i=0; i<N; i++){
double temp1 = 0;//折扣率
double temp2 = 0;//折扣上限
cin >> temp1;
a[0][i] = temp1;//折扣率
cin >> temp2;
a[1][i] = temp2;//折扣上限
}
//按照折扣率排序,从小到大
for(int i=0; i<N-1; i++){
for(int j=0; j<N-i-1; j++){
if( a[0][j] > a[0][j+1] ){
double tempp1 = a[0][j];
a[0][j] = a[0][j+1];
a[0][j+1] = tempp1;
double tempp2 = a[1][j];
a[1][j] = a[1][j+1];
a[1][j+1] = tempp2;
}
}
}
//传入zhekou(a, N, T)函数中的数组a已经有序了
int number = zhekou(a, N, T);
cout << "最小金额" << number << endl;
return 0;
}
标签:数学 png highlight 存在 如何 ret ace string 额外
原文地址:https://www.cnblogs.com/Whgy/p/12303928.html