C++题目1

来源:互联网 发布:php程序员简历 编辑:程序博客网 时间:2024/05/18 02:24
1.  给定等式  A B C D E                             其中每个字母代表一个数字,且不同数字对应不
                    D F G                                       同字母。编程求出这些数字并且打出这个数字的
             +      D F G                                      算术计算竖式。
             ───────

                X Y Z D E


#include <iostream>using namespace std;int main(){int a, b, c, d, e, f, g, x, y, z;for (a = 0; a < 10; a++)for (b = 0; b < 10; b++)if (b == a)continue;elsefor (c = 0; c < 10; c++)if (c == a || c == b)continue;elsefor (d = 0; d < 10; d++)if (d == a || d == b || d == c)continue;elsefor (e = 0; e < 10; e++)if (e == a || e == b || e == c || e == d)continue;elsefor (f = 0; f < 10; f++)if (f == a || f == b || f == c || f == d || f == e)continue;elsefor (g = 0; g < 10; g++)if (g == a || g == b || g == c || g == d || g == e || g == f)continue;elsefor (x = 0; x < 10; x++)if (x == a || x == b || x == c || x == d || x == e || x == f || x == g)continue;elsefor (y = 0; y < 10; y++)if (y == a || y == b || y == c || y == d || y == e || y == f || y == g || y == x)continue;elsefor (z = 0; z < 10; z++)if (z == a || z == b || z == c || z == d || z == e || z == f || z == g || z == x || z == y)continue;elseif (a * 10000 + b * 1000 + c * 100 + d * 10 + e + d * 100 + f * 10 + g + d * 100 + f * 10 + g == x * 10000 + y * 1000 + z * 100 + d * 10 + e)cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << ' ' << g << ' ' << x << ' ' << y << ' ' << z<<endl;return 0;}



 2. A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
  人参加了竞赛:
   (1)A参加时,B也参加;
   (2)B和C只有一个人参加;
   (3)C和D或者都参加,或者都不参加;
   (4)D和E中至少有一个人参加;
   (5)如果E参加,那么A和D也都参加。

#include <iostream>using namespace std;int main(){int a, b, c, d, e;for (a = 0; a <= 1; a++)for (b = 0; b <= 1; b++)for (c = 0; c <= 1; c++)for (d = 0; d <= 1; d++)for (e = 0; e <= 1; e++)if ((!a || b)&(!b || !c)&((c&d) || (!c&!d))&(d || e)&((!e&!a&d) || (a&d)))cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d << " e=" << e << endl;elsecontinue;return 0;}




  3. 打印一个 N*N 的方阵,N为每边                                         N=15  打印出下面图形
 字符的个数(3<N<20), 要求最                                       TTTTTTTTTTTTTTT
 外一层为"T", 第二层为"J", 从第三层                                           TJJJJJJJJJJJJJT
 起每层依次打印数字 1,2,3,...                                                       TJ11111111111JT
 (右图以N为15为例)                                                             TJ12222222221JT
                                                                                                          TJ12333333321JT
                                                                                                          TJ12344444321JT
                                                                                                          TJ12345554321JT
                                                                                                          TJ12345654321JT
                                                                                                          TJ12345554321JT
                                                                                                          TJ12344444321JT
                                                                                                          TJ12333333321JT
                                                                                                          TJ12222222221JT
                                                                                                          TJ11111111111JT
                                                                                                          TJJJJJJJJJJJJJT
                                                                                                          TTTTTTTTTTTTTTT


#include <iostream>using namespace std;int main(){char a[30][30];int i, j, k, N;cout<<"please input N: ";cin>>N;for (i = 0; i<N; a[0][i] = 'T', a[N - 1][i] = 'T', a[i][0] = 'T', a[i][N - 1] = 'T', i++);for (i = 1; i<N - 1; a[1][i] = 'J', a[N - 2][i] = 'J', a[i][1] = 'J', a[i][N - 2] = 'J', i++);for (j = 49, i = 2; i<N / 2 + 1; i++, j++)for (k = i; k<N - i; a[i][k] = j, a[k][i] = j, a[N - i - 1][k] = j, a[k][N - i - 1] = j, k++);for (i = 0; i<N; i++){for (k = 0; k < 15; k++)cout << " ";for (j = 0; j < N; j++)cout<<a[i][j];cout << endl;}}

//第4题未完成


5. 输入一个十进数,将其转换成 N 进制数(0<N<=16)。

#include<iostream>#include<vector>#include<cmath>using namespace std;int main(){char fun(int &c);int a,b,c,i=0,j;double d,e,f;cout<< "please input a  number:" << endl;cin >> d;cout << "please input the transfer<16:" << endl;cin >> b;if (b > 16)cout << "sorry,the input number is error." << endl;else{a = floor(d);e = d - a;vector<char>ivec,ivec2;while (a != 0){c = a%b;c = fun(c);a = a / b;ivec.push_back(c);}while (i != 6){f = e*b;j = floor(f);e = f - j;j = fun(j);ivec2.push_back(j);++i;}cout << "the " <<b<< " sequence is:" << endl;for (vector<char>::reverse_iterator iter = ivec.rbegin(); iter != ivec.rend(); ++iter)cout << *iter;cout << ".";for (vector<char>::iterator iter2 = ivec2.begin(); iter2 != ivec2.end(); ++iter2)cout << *iter2;cout << endl;}return 0;} char fun(int &c){if (c < 10)c = c + 48;elseswitch (c){case 10:c = 'A'break;case 11:c = 'B'break;case 12:c = 'C'break;case 13:c = 'D'break;case 14:c = 'E'break;case 15:c = 'F'break;}return c;}


 6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:


   ① 倒填,例如N=5                        ② 蛇形填数                                   ③ 回转填数

                                                                                         
 │25│24│23│22│21│                        │ 1│ 3│ 4│10│11│                      │ 1│16│15│14│13│
                                          
 │20│19│18│17│16│                        │ 2│ 5│ 9│12│19│                      │ 2│17│24│23│12│
                                        
 │15│14│13│12│11│                        │ 6│ 8│13│18│20│                     │ 3│18│25│22│11│
                                      
 │10│ 9│ 8│ 7│ 6│                              │ 7│14│17│21│24│                    │ 4│19│20│21│10│
                               
 │ 5│ 4│ 3│ 2│ 1│                               │15│16│22│23│25│                   │ 5│ 6│ 7│ 8│ 9│
 

  

   

#include<iostream>#include<vector>using namespace std;int main(){const int N = 5;vector<int>ivec;int static j = 1;for (int i = 1; i <= N*N; ++i)ivec.push_back(i);cout << "the first sequence is " << endl;for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter){cout << *iter<<'\t';if (j % N == 0)cout << endl;++j;}cout << "the second sequence is " << endl;for (vector<int>::reverse_iterator iter = ivec.rbegin(); iter != ivec.rend(); ++iter){cout<< *iter<<'\t';if (j % N == 0)cout << endl;++j;}cout << "the third sequence is " << endl;int sum = 0, k = 0, t = 0, start = 0, end = 0;    vector<vector<int> >ivec2(N, vector<int>(N));for (sum = 0; sum < 2 * N - 1; ++sum){start = (sum < N) ? 0 : sum - (N - 1); //越过对角线后start和end范围递减 end = (sum < N) ? sum : (N - 1);for (k = start; k <= end; ++k)//(sum%2)控制环绕方向==0和!=0的时候分别向不同的对角方向环绕//随着k的增加sum-k渐小ivec2[k][sum-k]时行渐大列渐小,//ivec2[sum-k][k]时相反(sum % 2 == 0) ? (ivec2[k][sum - k] = ++t) : (ivec2[sum - k][k] = ++t);}for (sum = 0; sum < N; ++sum){for (k = 0; k < N; ++k)cout << ivec2[sum][k] << '\t';cout << endl;}cout << "the firth sequence is " << endl;vector<vector<int> >ivec3(N, vector<int>(N));sum = 0, k = 0, t = 0, start = 0, end = 0;int m = 0;for (sum = 0; sum < 2 * N - 1; ++sum){m = sum / 4;end = N - m;start = N - m - 2;switch (sum % 4){case 0:for (k = m; k < end;++k)ivec3[k][m] = ++t;break;case 1:for (k = m + 1; k < end;++k)ivec3[start+1][k] = ++t;break;case 2:for (k = start; k>=m;--k)ivec3[k][end-1] = ++t; break;case 3:for (k = start; k>m;--k)ivec3[m][k] = ++t;break;}}for (sum = 0; sum < N; ++sum){for (k = 0; k < N; ++k)cout << ivec3[sum][k] << '\t';cout << endl;}return 0;}

  


//第7题未完成


   

  8. 输入两个正整数X,Y,将X,Y化为二进制数,然后将这两个二进制数作二进
  制加法运算,再将结果化为十进制数输出。


       

#include<iostream>#include<vector>using namespace std;int main(){int X, Y;int count1 = 0, count2 = 0;cout << "please input X,Y:" << endl;cin >> X >> Y;cout << "the number is " << X + Y << endl;vector<int>xvec, yvec;while (X != 0){xvec.push_back(X % 2);X = X / 2;++count1;}while (Y != 0){yvec.push_back(Y % 2);Y = Y / 2;++count2;}int count = (count1 > count2) ? count1 : count2;for (int j = count1; j < count; ++j)xvec.push_back(0);for (int j = count2; j < count; ++j)yvec.push_back(0);vector<int>ivec(1,0);for (int i = 0; i <count; ++i){if (xvec[i] + yvec[i] + ivec[i] == 0){ivec[i] = 0;ivec.push_back(0);}else if (xvec[i] + yvec[i] + ivec[i] == 1){ivec[i] = 1;ivec.push_back(0);}else if (xvec[i] + yvec[i] + ivec[i] == 2){ivec[i] = 0;ivec.push_back(1);}else{ivec[i] = 1;ivec.push_back(1);}}for (vector<int>::reverse_iterator iter = ivec.rbegin(); iter != ivec.rend(); ++iter){if (iter == ivec.rbegin() && *iter == 0)continue;cout << *iter;}cout << endl;return 0;}
        

  9. 四人玩火柴棍游戏,每一次都是三个人赢,一个人输。输的人要按赢者手中的火柴
  数进行赔偿,即赢者手中有多少根火柴棍,输者就赔偿多少根。现知道玩过四次后,
  每人恰好输过一次, 而且每人手中都正好有16根火柴。问此四人做游戏前手中各有
  多少根火柴? 编程解决此问题。


#include<iostream>using namespace std;int main(){void fun(int&, int&, int&, int&);int A,B,C,D;A = B = C = D = 16;fun(D, A, B, C);fun(C, A, B, D);fun(B, A, C, D);fun(A, B, C, D);cout << " a=" << A << " b=" << B<< " c=" << C << " d=" << D << endl;return 0;}void fun(int&iint&xint&yint&z){x =  x/2;y =  y/2;z =  z/2;i = i + x / 2 + y / 2 + z / 2;}


//第10,11,12题未完成


13. 有N个硬币(N为偶数)正面朝上排成一排,每次将 N-1 个硬币翻过来放在原位
 置, 不断地重复上述过程,直到最后全部硬币翻成反面朝上为止。编程让计算机把
 翻币的最简过程及翻币次数打印出来(用*代表正面,O 代表反面)。


#include<iostream>#include<vector>using namespace std;int main(){const int N = 8;int i,start;void swap(char&);vector<char>ivec(N,'*');for (start = 0; start < N; ++start){for (int j = 0; j < N; ++j){if (j == start)continue;swap(ivec[j]);}for (i = 0; i < N; ++i)cout << ivec[i];cout << endl;}return 0;}void swap(char&x){if (x == '*')x = 'o';elsex = '*';}
 


//第14,15,16题未完成 


 17. 编写一个程序,当输入不超过60个字符组成的英文文字时,计算机将这个句子
 中的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度
 相同。例如:


    输入:


    THE PRICE OFBREAD IS ¥1 25 PER POUND


    输出:


    ABC DDEEE EFHIINO OP ¥1 25 PPR RRSTU


 并且要求只对A到Z的字母重新排列,其它字符保持原来的状态。


#include<iostream>#include<algorithm>#include<cctype>#include<string>using namespace std;int main(){string str,sco;getline(cin, str);for (string::size_type index = 0; index != str.size(); ++index){if (isalpha(str[index]))sco.push_back(str[index]);}sort(sco.begin(), sco.end());string::size_type i=0;for (string::size_type index = 0; index!= str.size(); ++index){if (isalpha(str[index])){str[index] = sco[i];++i;}}cout << str << endl;return 0;}


0 0
原创粉丝点击