第一天入门c++;学习语法结构cin与cout输入与输出结构写了一个顺序与倒序输出;
题目:输入一个四位数,eg:1234要求把这个数字翻转过来变成4321输出;
#include<bits/stdc++.h>//万能头文件
using namespace std;//定义命名空间
int main(){//主程序
int s,a,b,c,d,sum;//定义变量
cin>>s; //读取
a=a/1000; //求原数的千位
b=b%100/10;
c=c%10;//求原数的百位
d=d/1; //求原数的个位
sum=d*1000+c*100+b*10+a; //换位 实现倒序输出
cout<<sum;
return 0;//返回值
}
第二个关于浮点数
题目 :输入一个摄氏度转化为华氏度
#include<bits/stdc++.h>
using namespace std;
int main(){
double f,c;
cin>>c;
f=9/5.0*c+32;
cout<<f<<endl;
return 0;
}
第二天学习一个根号函数
题目 用sqrt函数计算三角形面积问题,且保留一位小数。用cout<<fixed<<setprecision(X)语句保留小数
# include<bits/stdc++.h>
using namespace std;
int main(){
double c,a,b,d,s;
cin>>a>>b>>d;
c=(a+b+d)/2.0;
s=sqrt(c*(c-a)*(c-b)*(c-d));
cout<<fixed<<setprecision(1)<<s<<endl;
return 0;
}
分支语句if else语句
题目:W和w同时说出1则输出gogogo;w说1W说0则输出standby
#include<bits/stdc++.h>
using namespace std;
int main(){
int w,W;
cin>>w>>W;
cout<<"gogogo"<<endl;}
if(w=W)
{cout<<"gogogo";}
else
cout<<"standby"<<endl;
return 0;
}
for语句
#include<bits/stdc++.h>
using namespace std;
int main(){
for(int a=0;a<=4;a++){
cout<<"hapa";}
return 0;
}
while语句
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
cin>>a;
while(a<6){
cout<<"hapa";
break;
}
return 0;
}
习题题目:[问题描述]
给定一个整数,判断它能否被3,5,7整除,并输出以下信息:
1、能同时被3,5,7整除 (直接输出3 5 7,每个数中间一个空格) ;
2、只能被其中两个数整除(输出两个数,小的在前,大的在后。例如: 3 5或者 3 7或者5 7,中间用空格分隔)
3、只能被其中一个数整除 (输出这个除数);
4、不能被任何数整除,输出小写字符n”,不包括单引号
[输入格式]
输入一行,包括一个整数。
[输出格式]
输出一行,按照描述要求给出整数被3,5,7整除的情况。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
if(n%3==0&&n%5==0&&n%7==0){
cout<<"3 5 7";}
else if(n%3==0&&n%5==0){
cout<<"3 5";}
else if(n%7==0){
cout<<"7";}
else {
cout<<n;}
return 0;
}