机试指南第二章 暴力求解

P10 习题2.1

http:///t.cn/E9lOOZQ
在这里插入图片描述

#include <iostream>
#include <iomanip>
//http://t.cn/
using namespace std;

bool judge(int x){//判断是否有某一位是7
    while (x!=0){
        if (x%10 == 7 ){return true;}
        x = x/10;
    }
    return false;
}

int main(){
    int n;
    cin>>n;
    int sum = 0;
    for (int i=1;i<=n;i++){
        if (i%7 != 0&&!judge(i)){sum += i*i;}
    }
    cout<<sum;
}

P10 习题2.2

http:///t.cn/E9ldhru
在这里插入图片描述

#include <iostream>

using namespace std;

int main(){
    int n;
    cin>>n;
    int big = 0;
    int small = 0;
    int smaller = 0;
    for (big=0;big<=20;big++){//最多能买100/5=20只大的
        for (small=0;small<=34;small++){//最多能买100/3<34只小的
            for(smaller=0;smaller<=100;smaller++){//鸡总数100,小小鸡不超过一百
                if (smaller+small+big == 100){//鸡总数刚好100
                    if (smaller/3+small*3+big*5<=n){//价格不超
                        cout<<"x="<<big<<",y="<<small<<",z="<<smaller<<endl;
                    }
                }
            }
        }
    }
    return 0;
}

P10 习题2.3

http://t.cn/E9jqijR
在这里插入图片描述
在这里插入图片描述

#include <iostream>

using namespace std;

int main(){
    int n,x,y,z;
    cin>>n>>x>>y>>z;
    int perprice = 0;//火鸡单价
    int allprice;//火鸡总价
    int a,b;//账单价格 aXYZb
    for(int i=1;i<10;i++){
        for(int j=0;j<10;j++){
            allprice = i*10000+x*1000+y*100+z*10+j;
            if (allprice%n == 0){//可以整除,满足要求
                if (allprice>perprice*n){//选单价贵的
                    perprice = allprice/n;
                    a = i;b = j;
                }
            }
        }
    }
    if (perprice == 0){cout<<'0';return 0;}
    cout<<a<<' '<<b<<' '<<perprice;
    return 0;
}

P12 例题2.5

在这里插入图片描述

#include <iostream>

using namespace std;

int main(){
    int n;
    char a,b;
    cin>>n>>a>>b;
    int ar[100][100];
    int ncopy = n;//拷贝n
    int c = 1;
    while(ncopy!=-1){//n必然是奇数,当n=1,是最内圈。
        for(int i=(n-ncopy)/2;i<=(n-ncopy)/2+ncopy-1;i++){
            for(int j=(n-ncopy)/2;j<=(n-ncopy)/2+ncopy-1;j++){
                ar[i][j] = c;//从1,一直累加,奇数外圈,偶数内圈
            }
        }
        ncopy -= 2;
        c++;
    }
    if (n!=1){//挖去四个角
        ar[0][0] = -1;
        ar[0][n-1] = -1;
        ar[n-1][0] = -1;
        ar[n-1][n-1] = -1;
    }
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            if (ar[i][j] == -1){cout<<' ';}//四个角,输出空的情况
            else if (ar[i][j]%2 == 1){cout<<a;}
            else{cout<<b;}
        }
        cout<<endl;
    }
    return 0;
}

P22 习题2.6

http:///t.cn/E9Yz0LE
在这里插入图片描述

#include <iostream>

using namespace std;

int daytab[2][13] = {//闰年和非闰年月份天数
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
};

bool isleapyear(int year){//判断闰年
    if (year%400 == 0 || (year%100!=0 && year%4 == 0)){return true;}
    else {return false;}
}

int numyear(int year){//返回某年的天数
    if (isleapyear(year)){return 366;}
    else {return 365;}
}

int main(){
    int date1,date2,year1,year2,month1,month2,day1,day2;
    cin>>date1>>date2;
    int result1 = 0;//year1的1月1日到date1已经过了的天数
    int result2 = 0;//year1的1月1日到date2已经过了的天数
    int loop = 2;
    while(loop--){
        if (loop == 1){
            day1 = date1%100;
            day2 = date2%100;
        }
        if (loop == 0){
            month1 = date1%100;
            month2 = date2%100;
        }
        date1 /= 100;
        date2 /= 100;
        year1 = date1;
        year2 = date2;
    }
    int row = isleapyear(year1);//判断闰年
    for (int i=0;i<month1;i++){
        result1 += daytab[row][i];//加每个月份
    }
    result1 +=day1;//加当月天数
    row = isleapyear(year2);
    for (int i=0;i<month2;i++){
        result2 += daytab[row][i];
    }
    while(year2>year1){//加上year1,year2差的天数
        year2--;
        result2 += numyear(year2);
    }
    result2 += day2;
    result2 -= result1;
    result2 ++;
    cout<<result2;
    return 0;
}

P22 习题2.7

http:///t.cn/E9YZLbi
在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

//公元1年1月1日——星期一
int daytab[2][13] = {
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
};

bool isleapyear(int year){
    if (year%400 == 0 || (year%100!=0 && year%4 == 0)){return true;}
    else {return false;}
}

int numyear(int day){
    if (isleapyear(day)){return 366;}
    else {return 365;}
}

string monthname[13]={
    "","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};

string weekday[7]={
     "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday","Sunday"
};

int main(){
    int day;string month;int year;
    cin>>day>>month>>year;
    int countday = 1;
    int countmonth = 1;
    int countyear = 1;
    int result = 0;
    int monthcopy;
    for (int i=1;i<=12;i++){
        if (monthname[i] == month){
            monthcopy = i;
            break;
        }
    }
    //1 January 1
    //循环直到日月年都满足
    while(countday<day || countmonth<monthcopy || countyear<year){
        result++;//天数
        countday++;//日数
        if (countday>daytab[isleapyear(countyear)][countmonth]){//当前日数大于当月合法日数
            countday = 1;//日数重置为1
            countmonth++;//月数+1
        }
        if (countmonth>12){//月数大于12
            countmonth = 1;
            countyear++;
        }
    }
    cout<<weekday[result%7];
}

P27 习题2.9

http://t.cn/E9rDPSq
在这里插入图片描述

#include <iostream>
#include <math.h>

using namespace std;

int main(){
    int p,t,g1,g2,g3,gj;
    cin>>p>>t>>g1>>g2>>g3>>gj;
    float result;
    if (abs(g1-g2)<=t){
        result = (float)(g1+g2)/2;
    }
    else{
        if (abs(g3-g2)<=t && abs(g3-g1)<=t){
            if (g2>=g1){result = g2;}
            else {result = g1;}
            if (result<g3){result = g3;}
        }
        else if (abs(g3-g2)<=t){result = (float)(g2+g3)/2;}
        else if (abs(g3-g1)<=t){result = (float)(g1+g3)/2;}
        else {result = gj;}
    }
    printf("%0.1f",result);
    return 0;
}

P27 习题2.10

http://t.cn/E9dvHs4
在这里插入图片描述
被注释的切割字符串方法超时,其实也没差多少。。

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>;

using namespace std;

void getfildname(string str,vector <string> &fildname){
    string word = "";//各个文件夹名称
    int position  = 0;
    while(position<str.size()){
        while (position<str.size() && str[position]!='\\'){//判断各个文件夹名称分割
            word += str[position++];
        }
        fildname.push_back(word);
        word += "\\";
        position++;//跳过/
    }
}

bool comp(string s1,string s2){return s1<s2;}

int main(){
    freopen("D://case.txt","r",stdin);
    vector <string> fildname;//切割后的文件名,按顺序存储
    int n;
    string str;
    while(cin>>n && n!=0){
        while(n--){/*
            string str;
            cin>>str;
            getfildname(str,fildname);*/
            
            
            cin>>str;
            //cout<<str<<endl;
            if(str[str.length()-1]=='\\'){//考虑a/b/c/这样的输入,去掉最后一个/
                str.erase(str.length()-1,str.length());
            }
            if(find(fildname.begin(),fildname.end(),str)==fildname.end()){//第一次出现
                fildname.push_back(str);
            }
            while(str.find('\\')!=string::npos){//字符串中还有还有/
                str.erase(str.find_last_of('\\'),str.length());//删除最后一个/之后的
                if(find(fildname.begin(),fildname.end(),str)==fildname.end()){
                    fildname.push_back(str);
                }
            }
        }
        //set<string>s(fildname.begin(), fildname.end());//vector去重复
        //fildname.assign(s.begin(), s.end());

        sort(fildname.begin(),fildname.end(),comp);//排序 a a/b a/b/c
        for (int i=0;i<fildname.size();i++){
            if (fildname[i].find("\\") == string::npos){cout<<fildname[i]<<endl;}//没有/的直接输出
            else{
                int position = fildname[i].find_last_of("\\");//只需要输出最后一个/之后的内容
                for (int j=0;j<position;j++){cout<<" ";}
                cout<<" ";
                cout<<fildname[i].substr(position+1)<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}



P27 习题2.11

http://t.cn/E9dhoRA
在这里插入图片描述
https://blog.csdn.net/travelalong/article/details/18134911


#include<iostream>
#include<algorithm>
 
using namespace std;
const int N = 102;
 
int aa[N],bb[N];        //left蚂蚁位置 right蚂蚁位置
int a[N],b[N];
int main()
{
	int n;
	while(cin>>n)
	{
		int pos=0;              //蚂蚁A位置
		for(int i=1;i<=n;++i)
		{
			cin>>a[i]>>b[i];
			if(b[i]==0) pos=a[i];
		}
		int nl=0,nr=0;
		for(int i=1;i<=n;++i)
		{
			if(a[i]<pos && b[i]>0) aa[++nl]=100-a[i];
			else if(a[i]>pos && b[i]<0) bb[++nr]=a[i];
		}
		if(nl==nr) cout<<"Cannot fall!"<<endl;
		else if(nl>nr)
		{
			sort(aa+1,aa+nl+1);
			cout<<aa[nr+1]<<endl;
		}
		else
		{
			sort(bb+1,bb+nr+1);
			cout<<bb[nl+1]<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值