| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1366 人关注过本帖
标题:三数比较大小有简化的方法吗?
只看楼主 加入收藏
young_zhou
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-2-24
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:12 
三数比较大小有简化的方法吗?
我作业写了一个程序,但写得很复杂,想了解一下是否有方法把程序简化一下,谢谢大家的指导..
要求:
Suppose viewers have voted and we have counted the votes.We want to know who the winner is.Write a program that asks for the number of votes each contestant these three numbers to determine the result of the contest.There are seven possible outcomes.  Your program must determine which of the following is true and display it on the screen:
 
     “Amanda wins”
     “Ben wins”
     “Chris wins”
     “Amanda and Ben tie for first place”
     “Amanda and Chris tie for first place”
     “Ben and Chris tie for first place”
     “Amanda, Ben and Chris all tie for first place”
程序如下:
#include <iostream>
using namespace std;

int main() {
    int aVote = 0;
    int bVote = 0;
    int cVote = 0;
 
    cout << "How many votes did Amanda received? ";
    cin >> aVote;
    cout << "How many votes did Ben received? ";
    cin >> bVote;
    cout << "Hoe many votes did Chris received? ";
    cin >> cVote;
   
    if (aVote > bVote) {
              if (aVote > cVote)
                 cout << "Amanda wins" << endl;
              else if (aVote = cVote)
                   cout << "Amanda and Chris tie for first place" <<endl;
              else
                   cout << "Chris wins" << endl;
    }
    else if (aVote = bVote) {
         if (aVote > cVote)
            cout << "Amanda and Ben tie for first place" << endl;
         else if (aVote = cVote)
              cout << "Amanda, Ben and Chris all tie for first place" << endl;
         else
              cout << "Chris wins" << endl;
    }
    else {
         if (bVote > cVote)
            cout << "Ben wins" << endl;
         else if (bVote =cVote)
              cout << "Ben and Chris tie for first place" << endl;
         else
              cout << "Chris wins" << endl;
    }
    system("PAUSE");
    return 0;
}

搜索更多相关主题的帖子: determine following possible 
2012-02-24 04:39
young_zhou
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-2-24
收藏
得分:0 
因为在外国学习,所以作业要求都是英文的,如有需要可以看我的翻译,不过准确度就不能保证了
假设观众投票,我们把投票数算出来了.我们想知道谁是赢家.写一个程序,询问每个选手的票数,要求这三个数字来确定投票结果.共有七种可能结果。你的程序必须把正确的结果显示在屏幕上:

     “Amanda 赢”
     “Ben 赢”
     “Chris 赢”
     “Amanda 和 Ben 并列第一名”
     “Amanda 和 Chris 并列第一名”
     “Ben 和 Chris 并列第一名”
     “Amanda,Ben 和 Chris 并列第一名”
2012-02-24 04:58
杨松松
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:98
专家分:142
注 册:2011-12-3
收藏
得分:1 
果然写法有些不一样啊,得仔细瞅瞅
2012-02-24 08:08
杨松松
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:98
专家分:142
注 册:2011-12-3
收藏
得分:0 
想了一下,三个数比较可以用这种方法(a>b)?((a>c)?a:c):((b>c)?b:c)这是我想到的最简法了。这只适用于三数不等
2012-02-24 09:27
姚杰
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:169
专家分:477
注 册:2010-6-1
收藏
得分:1 
我给你个简便的程序吧:
#include<stdio.h>
main()
{
int a,b,c,max;
scanf("%d %d %d",&a,&b,&c);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf("%d\n",max)
}
你的程序是用C++编写的,我的是用C写的,如果有必要的话自己改成c++吧。改下输入输出语句和开头一句就可以了

[ 本帖最后由 姚杰 于 2012-2-24 11:24 编辑 ]

持之以恒,别留遗憾,加油
2012-02-24 11:21
天天涯涯
Rank: 4
等 级:业余侠客
帖 子:215
专家分:267
注 册:2011-10-17
收藏
得分:1 
五楼的代码简洁
2012-02-24 11:42
王逢
Rank: 2
等 级:论坛游民
帖 子:30
专家分:32
注 册:2011-10-7
收藏
得分:1 
这个书上都有源程序,谭浩强的
2012-02-24 12:11
a451446626
Rank: 1
等 级:新手上路
帖 子:17
专家分:9
注 册:2012-2-20
收藏
得分:1 
五楼的还是不行啊。。没有所问题都反应出来。。。如果有并列第一名的了
2012-02-24 17:58
kifine
Rank: 2
等 级:论坛游民
帖 子:48
专家分:49
注 册:2011-5-9
收藏
得分:1 
并列的话,再加三个if语句可行?
2012-02-24 18:34
a451446626
Rank: 1
等 级:新手上路
帖 子:17
专家分:9
注 册:2012-2-20
收藏
得分:0 
#include<stdio.h>
int main()
{
    float a,b,c;
    printf("请输入三人投票数:\n");
    scanf("%f,%f,%f",&a,&b,&c);
    if(a>b&&a>c)printf("A是第一名");
    if(b>a&&b>c)printf("B是第一名");
    if(c>a&&c>b)printf("C是第一名");
    if(a==b&&b==c)printf("三人并列第一");
    if(a==b&&b>c)printf("A和B并列第一");
    if(a<b&&b==c)printf("B和C并列第一");
    if(a==c&&c>b)printf("A和C并列第一");
    return 0;
}
        
2012-02-24 19:24
快速回复:三数比较大小有简化的方法吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018243 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved