码迷,mamicode.com
首页 > 其他好文 > 详细

最终为123

时间:2014-10-02 18:09:03      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

1. 设定一个任意数字串,数出这个数中的偶数个数,奇数个数,及这个数中所包含的所有位数的总数,将答案按 --” 的位序,排出得到新数。重复进行,最后会得到 123

解法一:

 

 1 #include<iostream>
 2 #include<string.h>
 3 #include<cstdio>
 4 using namespace std;
 5 int main()
 6 {
 7     char num[100];
 8     for(int i=0; i<100; i++)  //初始化数组
 9     {
10         num[i] = a;
11     }
12     int count = 0;
13     int oddCount = 0;
14     int evenCount = 0;
15     int totalNum = 0;
16     printf("请输入数字:");
17     char input;
18     while((input =getchar())!= &&(input!=\n))
19     {
20             num[count] = input;
21             count++;
22 
23     }
24     string LinkStr = "";//最后拼接成的字符串
25     int step = 0;  //总共步数
26     while(LinkStr!="123")
27     {
28         for(int j=0; j<100 && num[j]!=a; j++)
29         {
30             int k = num[j]-0;  //将char转化成int
31             if(k%2)
32             {
33                 oddCount++;
34             }
35             else
36             {
37                 evenCount++;
38             }
39         }
40         totalNum = oddCount+evenCount;
41 
42         /*下面是将totalcount,oddCount和evenCount组合成 “偶-奇-总”的形式*/
43 
44         char t1[100],t2[100],t3[100];
45         string temp1,temp2,temp3;
46         sprintf(t1,"%d",evenCount);
47         sprintf(t2,"%d",oddCount);
48         sprintf(t3,"%d",totalNum);
49         temp1 = t1;
50         temp2 = t2;
51         temp3 = t3;
52         string  temp = temp1+temp2;
53         LinkStr = temp+temp3;
54         step++;
55         cout<<"Step"<<step<<":";
56         cout<<"now the number is:"<<LinkStr<<endl;
57 
58         /*重置三个计数变量*/
59         totalNum = 0;
60         oddCount = 0;
61         evenCount = 0;
62 
63         /*更新num数组*/
64         for(int i=0; i<20; i++)
65         {
66             num[i] = a;
67         }
68         int LinkStrSize = LinkStr.length();
69         for(int i = 0; i<LinkStrSize; i++)
70         {
71             num[i] = LinkStr[i];
72         }
73     }
74     printf("输出结果能为123");
75 }

 

解法二:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int media(int num)
 5 {
 6     int m = 1;
 7     while(num)
 8     {
 9         num /= 10;
10         m *= 10;
11     }
12 
13     return m;
14 }
15 
16 int main()
17 {
18     int numb, odd = 0, even = 0, lastNumb = 0;
19     cout<<"please input the number:";
20     cin >> numb;
21     while(numb != lastNumb || numb != 123)
22     {
23         lastNumb = numb;
24         odd = 0;
25         even = 0;
26         while(numb)
27         {
28             if((numb % 10) %2)
29                 odd++;
30             else
31                 even++;
32             numb /= 10;
33         }
34         numb = odd + even;
35         numb = media(numb) * odd + numb;
36         numb = media(numb) * even + numb;
37         cout << "now number is:" << numb << endl;
38     }
39     cout<< numb;
40     return 0;
41 }

 

 

最终为123

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/ziquan/p/4004227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!