P10 习题2.1
#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
#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
#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
#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
#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
#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;
}