c++的刷题(1)

                     c++的刷题(1)

目录标题

1.输出“Hello,World”

#include <iostream>
using namespace std;
 
int main() 
{
    cout << "Hello, World!";  //输出Hello,World
    return 0;
}

2.编写一个程序将英里转换成公里,要求用户输入一个表示英里的数字。

#include<iostream>
using namespace std;
int main()
{
   float x,y; //x为英里,y为公里。
   cin>>x;    //输入英里
   y=x/1.609;   
   cout<<y<<endl;  //输出公里
   return 0;
}

3.编写一个程序,提示用户输入两个整数值。将这些值保持在int变量val1和val2中。编写你的程序求这两个值中的最小值,最大值,和,差,乘积和比率,并且将结果输出给用户。

#include<iostream>
using namespace std;
int main()
{

    int val1,val2,d,c,e,f,g,h;
    cin>>val1>>val2;   //输入val1和val2
    if(val1>val2)   //比较val1和val2
        d=val1;
        else
            d=val2;
    cout<<d<<endl;
    c=val1+val2;
    e=val1-val2;
    f=val1*val2;
    g=val1/val2;
    cout<<c<<endl;  //输出val1加val2
    cout<<e<<endl;  //输出val1减val2
    cout<<f<<endl;  //输出val1乘val2
    cout<<g<<endl;   //输出val1除val2
    return 0;
}

4.编写一个程序,提示用户输入三个整型值,然后按数值大小顺序用逗号隔开的方式输出这些值。因此,如果用户输入值为10 4 6,输出值应为4,6,10.如果有两个值相同,那么将它们按序一起输出。因此,输入4 5 4将会输出4,4,5。

方法1:

#include<iostream>
using namespace std;
int main() 
{
	float a,b,c;   //定义三个浮点型常量
	cout<<"请输入三个整数:"<<endl;  
	cin >>a>>b>>c;   //输入a,b,c;
	if(a>b)      //如果a>b
    {
        if(b>c)    //如果b>c
            cout<<a<<">"<<b<<">"<<c<<endl;  //输出a,b,c
        else  //如果b<c
        {
            if(a>c)  //如果a>c
                cout<<a<<">"<<c<<">"<<b<<endl;  //输出a,c,b
	    else  //如果a<c
            cout<<c<<">"<<a<<">"<<b<<endl;  //输出c,a,b
        }
    }
    else  //如果a<b
    {
        if(b>c) //如果b>c
	    if(a>c)   //如果a>c
	        cout<<b<<">"<<a<<">"<<c<<endl;  //输出b,a,c
        else //如果a<c
	        cout<<b<<">"<<c<<">"<<a<<endl;//输出b,c,a
        else  //如果b<c
            cout<<c<<">"<<b<<">"<<a<<endl; //输出c,b,a
    }
    return 0;
}

方法2:

#include <iostream>

using namespace std;

//1.输入三个整数x,y,z,按从小到大输出。(10分)
int main()
{
    int arr[3];
    for(int i=0;i<3;i++){
        cin>>arr[i];
        if(i>0){
            if(arr[i]<arr[i-1]){
                int tmp = arr[i-1];
                arr[i-1] = arr[i];
                arr[i] = tmp;
            }
        }
    }
    for(int j =1;j<3;j++){
        if(arr[j]<arr[j-1]){
                int tmp = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = tmp;
            }
    }
    for(int z=0;z<3;z++){
        cout << arr[z];
    }

    return 0;
}


5.输入一个数,并且输出他们。

#include<iostream>
using namespace std;
int main()
{
    int thisisanumber;
    cout<<"Please enter a number: ";
    cin>>thisisanumber;  //输入变量
    cout<<"You entered: "<<thisisanumber<<"\n"; //输出变量
    return 0;
}

6.程序编写一个字符串变量,提示用户输入他或者她的名字,然后进行输出。

#include<iostream>
#include<string>;
using namespace std;
int main()
{
  string user_name;
  cout<<"Please enter your name: ";
  cin>>user_name; //输入名字
  cout<<"Hi "<<user_name<<"\n"; //输出名字
  return 0;
}

7把用户的姓,空格,用户的名这三个单独的字符串合并成一个字符串。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string user_first_name;
    string user_last_name;
    cout<<"please enter your first name: "; 
    cin>>user_first_name;   //输入姓
    cout<<"please enter your last name: ";
    cin>>user_last_name;    //输入名
    string user_full_name=user_first_name+" "+user_last_name;
    cout<<"your name is: "<<user_full_name<<"\n"; //输入姓加名
    return 0;
}

8.输入一个值,如果输入值小于10,输出"You entered a value less than 10"

#include<iostream>

using namespace std;
int main()
{
    int x;
    cout<<"Enter a number: ";
    cin>>x;
    if(x<10)
    {
        cout<<"You entered a value less than 10"<<"\n";
    }
    return 0;
}

9.输入一个数,判断是否小于0,如果小于0输出"You enter a negative number",否则输出"You entered a non-negative number"

#include<iostream>

using namespace std;
int main()
{
    int x;
    cout<<"Enter a number: ";
    cin>>x;
    if(x<0)
    {
        cout<<"You entered a negative number"<<"\n";
    }
    else
        cout<<"You entered a non-negative number\n";
    return 0;
}

10.输入一个数,如果小于0输出"You entered a negative number",如果等于0输出"You entered zero",如果大于0输出"You entered a positive number".

#include<iostream>

using namespace std;
int main()
{
    int x;
    cout<<"Enter a number: ";
    cin>>x;
    if(x<0)
    {
        cout<<"You entered a negative number"<<"\n";
    }
    else if(x==0)
    {
        cout<<"You enter zero\n";
    }
    else
        cout<<"You entered a positive number\n";
    return 0;
}

11.程序读取用户的输入,和密码xyzzy进行比较。如果输入的内容和密码不同,程序便会立即从main函数返回。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string  x;
    cout<<"Enter a password: ";
    getline(cin,x,'\n');
    if(x=="xyzzy")
    {
        cout<<"Access allowed"<<"\n";
    }
    else
    {
        cout<<"Bad password.Denied access!"<<"\n";
        //使用return可以方便地结束程序
        return 0;
    }
    //继续执行!
}

12.程序读取用户的输入,跟密码xyzzy和使用者名字root进行比较,。如果输入的内容和密码不同,程序便会立即从main函数返回。

  #include<iostream>
#include<string>
using namespace std;
int main()
{
    string  x,y;
    cout<<"Enter a username: ";
    getline(cin,y,'\n');
    cout<<"Enter a password: ";
    getline(cin,x,'\n');
    if(x=="xyzzy"&&y=="root")
    {
        cout<<"Access allowed"<<"\n";
    }
    else
    {
        cout<<"Bad password.Denied access!"<<"\n";
        //使用return可以方便地结束程序
        return 0;
    }
    //继续执行!
}

13.编写一个程序,用while循环语句来输出从0到9的数字。

#include<iostream>

using namespace std;
int main()
{
    int i=0; //不要忘记声明变量
    while(i<10) //如果i小于10就循环
    {
        cout<<i<<'\n';
        i++;  //因为满足条件,所以i值增加
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值