练习题代码


313.计算 3456*1234
【知识点】c++程序基础基础框架

326.数字翻转
【知识点】整数取各个位上的位数,%符号,
获取a的个位:a%10 ; 十位:a/10%10 ;百位:a/10/10%10,以此类推

383.勾股定理
【知识点】勾股定理,幂函数:pow(a,b)  开方函数:sqrt()  头文件<cmath>
保留a的小数点后n位printf(%.nf”,a);


【参考代码】
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdio>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    double c;
    c = sqrt( pow(a,2) + pow(b,2) );
//  cout<< fixed << setprecision(2) << c <<endl;//c++语言输入输出流
    printf( "%.2lf",c );//c语言格式化输入输出
    return 0;
}


299.计算日期
【知识点】取余运算,条件判断,多分支语句
多分支语句格式:
if(条件1){  }
else if(条件2){  }
else if(条件3){  }
......
else if(条件n){  }
else{ }
【参考代码】
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	int n;
    cin>>n;
    n=(1+n)%7;//每过7天,回到当前的日期
    if( n==0 ) cout<< "sunday" <<endl;
    else if( n==1 ) cout<< "monday" <<endl;
    else if( n==2 ) cout<< "tuesday" <<endl;
    else if( n==3 ) cout<< "wednesday" <<endl;
    else if( n==4 ) cout<< "thursday" <<endl;
    else if( n==5 ) cout<< "friday" <<endl;
    else cout<< "saturday" <<endl;
	return 0;
}

300.苹果和虫子
【知识点】取整函数:向下取整floor() , 向上取整:ceil() , 头文件<cmath>
【参考代码】
#include<iostream>
#include<cmath>
using namespace std;
int main(){
	int n,x,y;
    cin>>n>>x>>y;
    double c = y*1.0/x;
    if( c<=n ) cout<< n - ceil(c) <<endl; //吃掉的苹果不足一个时也算作一个
	else cout<<0<<endl;// 当y小时后吃掉的苹果大于总数n时,剩下的苹果数量为0   
	return 0;
}

456.田忌赛马
【知识点】分支语句,逻辑判断
【参考代码】
#include<iostream>
using namespace std;
int main(){
	int a,b,c,a1,b1,c1;
    cin>>a>>b>>c;
    cin>>a1>>b1>>c1;
    if(a1>a||b1>b||c1>c) cout<<"WIN";
    else cout<<"LOSE";
    return 0;
}

585.2进制转换
【知识点】二进制转换:除2取余,逆序排列;while循环;
【参考代码】
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n;
	cin>>n;
	int cnt = 0;//表示数字的第cnt+1位;
	int num = 0;//使用十进制数num 来表示转换后的二进制
	while(n>0){
		num = num + n%2*pow(10,cnt); // 各数码*位权之和
		cnt++;
		n/=2;
	}
	cout<<num<<endl;
	return 0;
}






430.序列最大值
【知识点】数值比较,打擂台思想。
打擂台方式找最大值:假设最大值为maxn ,元素依次和最大值maxn比较,大于maxn时将该元素赋值给maxn,直到比较完所有元素。
【参考代码】
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n;
    int x;
    cin>>n;
   
    int maxn = -100;
    int tmp = n;
    while( tmp--){
        cin>>x;
        if( x>maxn ) maxn = x;
    }
    cout<<maxn;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值