| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1559 人关注过本帖
标题:4周c语言学渣渣求教><
取消只看楼主 加入收藏
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
4周c语言学渣渣求教><
在写学c语言后的第一个assignment
目前我已经学的有 if else for和while loop  1d 2d arrays
现在遇到一个问题是 输入5个数字 (1-9之间)
假设输入的是2 2 3 4 2或者 2 8 2 9 2或者9 1 2 9 9 总之就是任意五个数字
怎样检测其中有三个2或者9或者任意数字 并且能够储存此数字

试过用counter计数 一个挨着一个比较 但是要是三个2不挨在一起就没法检测出了

求教啊各位 用我学过的这些知识能解决不?

[ 本帖最后由 elainehemy 于 2013-4-23 14:42 编辑 ]
搜索更多相关主题的帖子: 数字 counter 语言学 知识 
2013-04-23 11:33
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
回复 2楼 TonyDeng
我现在能想到的就是第一个和第二三四五个比较 第二个和第三四五比较 以此类推 但是这样是不是太麻烦了 还有更好的方法么

EE苦逼学渣女屌一枚
2013-04-23 11:57
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
回复 4楼 TonyDeng
多谢大神的回复 感激不尽
但是你这个程序是适用于我给出的那五个数字呀 如果这个五个数字是1-9之间random的呢
我写的那两组就是举个例子~

EE苦逼学渣女屌一枚
2013-04-23 14:31
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
回复 10楼 lz1091914999
额。。。这个只用于这个2诶 我给的数组就是举个例子~
如果是输入1-9之间任意的五个数呢?

EE苦逼学渣女屌一枚
2013-04-23 14:32
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
回复 4楼 TonyDeng
我找到方法拉~ 感谢版主的提点!!!我实在太迟钝嘞。。。。

EE苦逼学渣女屌一枚
2013-04-23 15:00
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
程序代码:
//A program which does the scoring for game CSEzee
//CSEzee is played by rolling five 9-sided(1-9) dice.
//The program will give the score of the input by following CSEzee rules.
//
//20/04/2013

#include <stdio.h>
#define N_DICE 5
#define NUMBER 9


int main(void) {
    int x[N_DICE],number_table[9] = {0};
    int n[NUMBER]={1,2,3,4,5,6,7,8,9};
    int i,j, score = 0, score2 = 0;
    
    //storage of five input
    i = 0;
    for (i = 0;i < N_DICE; i++) {
        scanf("%d", &x[i]);
    }
    for (i = 0;i < N_DICE; i++) {
        //5 interger from 1 to five must be supplied
        if (1 <= x[i]&&x[i] <=9) {
            //sum of any throw
            for (i = 0;i < N_DICE;i++) {
                score = score + x[i];
            }
            //the number_table is used to memorize the number
            //of times of the appearance of interger 1..9
            for (i = 0;i < N_DICE;i++) {
                for (j = 0;j < NUMBER;j++) {
                    if (x[i]==n[j])
                        number_table[j] = number_table[j] + 1;
                }
            }
            for (j = 0;j < NUMBER;j++) {
                if (number_table[j] == 2) {
                    score2 = 14 + 2 * n[j];
                }else if (number_table[j] == 3) {
                    score2 = 15 + 3 * n[j];
                }else if (number_table[j] == 4) {
                    score2 = 16 + 4 * n[j];
                }else if (number_table[j] == 5) {
                    score2 = 17 + 5 * n[j];
                }
            }
            //the hihgest score will be printed
            for (j = 0;j < NUMBER;j++) {
                if (score > score2) {
                    printf("CSEzee score is %d: sum.\n",score);
                }else if (number_table[j] == 2) {
                    printf("CSEzee score is %d: pair of %d's\n",score2,n[j]);
                }else if (number_table[j] == 3) {
                    printf("CSEzee score is %d: three %d's\n",score2,n[j]);
                }else if (number_table[j] == 4) {
                    printf("CSEzee score is %d: four %d's\n",score2,n[j]);
                }else if (number_table[j] == 5) {
                    printf("CSEzee score is %d: five %d's\n",score2,n[j]);
                }
            }
            
        }else{
            printf("Invalid input: 5 integers 1..9 must be supplied.\n");
        }
    }
    return 0;
}

EE苦逼学渣女屌一枚
2013-04-23 21:42
elainehemy
Rank: 1
来 自:悉尼
等 级:新手上路
帖 子:8
专家分:1
注 册:2013-4-23
收藏
得分:0 
刚刚收到静夜思的私信说要我po出来解决办法~
但是我这个有个问题就是 如果只是sum的话 输出的sum就会重复9遍 如图
图片附件: 游客没有浏览图片的权限,请 登录注册


我也知道是怎样造成的 因为比了九次循环了九次所以就print了九次 但我不知道怎么改。。。

求教大神 求教版主~~

EE苦逼学渣女屌一枚
2013-04-23 21:47
快速回复:4周c语言学渣渣求教><
数据加载中...
 
   



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

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