| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 209 人关注过本帖
标题:有兴趣的来猜一下,等待您的指点
取消只看楼主 加入收藏
dandanbaobei
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-2-20
结帖率:0
收藏
 问题点数:0 回复次数:0 
有兴趣的来猜一下,等待您的指点
这是个猜数问题,电脑随机出一组数(四个数字),你自己输入四个,和电脑的一样就赢了。 不一样电脑就提示其中有几个是对的,并且对应那几位也是对的。如果全对就赢了。
但是这个程序我写的有点不对,应为输出结果有点误差,帮忙看一下!

#include<stdio.h>
 #include<stdlib.h>
 #include<time.h>
 int main()
 {
     int woshu,
         jishu,
         xhwo,
         xhji,
     //    jige,
     //    jiwei,
         wei;
     int f1=0,
         f2=0,
         f3=0,
         f4=0,
         jige=0,
         jiwei=0;
     
    srand((unsigned int)time(NULL));
     jishu = rand()%9000+1000;                       
 //    printf("%d 输入一个数:",jishu);                    //不加这个猜的难度加大了点
     scanf("%d",&woshu);
     while(woshu!=jishu)
     {
                 f1=f2=f3=f4=0;                        
                 jige=0;
                 jiwei=0;
 
        for(xhwo=1000;xhwo>=1;xhwo=xhwo/10)
         {
             for(wei=1,xhji=1000;xhji>=1;wei++,xhji=xhji/10)
                                                                     
             {                                                        
                                                                     
                 if(woshu/xhwo%10 == jishu/xhji%10)
                 {
                     switch(wei)
                     {
                     case 1:
                         if(0==f1)
                         {
                             jige++;
                             f1=1;
                             break;
                         }
                         break;
                     case 2:
                         if(0==f2)
                         {
                             jige++;
                             f2=1;
                             break;
                         }
                         break;
                     case 3:
                         if(0==f3)
                         {
                             jige++;
                             f3=1;
                             break;
                         }
                         break;
                     case 4:
                         if(0==f4)
                         {
                             jige++;
                             f4=1;
                             break;
                         }
                         break;
                     }
                 }
             }
         }
         for(xhwo=1000;xhwo>=1;xhwo=xhwo/10)
         {
             if(woshu/xhwo%10==jishu/xhwo%10)
             {
                 jiwei++;
             }
         }
         printf("有%d个相等的\n",jige);
         printf("有%d位相同\n",jiwei);
         printf("在输入一个数:");
         scanf("%d",&woshu);
     
    }
     printf("你赢了\n");
 
    return 0;
 }
 这个程序有点瑕疵,运行的准确率有点低啊,帮忙改一下,悉心听取!
搜索更多相关主题的帖子: include 
2012-02-25 10:48
快速回复:有兴趣的来猜一下,等待您的指点
数据加载中...
 
   



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

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