C++看不懂 高手幫我看一下

1.a(4.25) write a program that reads in the size of the side of a square and then prints a hollow squareof that asterisks and blanks. Your program should work for squares of all side between 1 and 20.

for example,if your program reads a size of 5,it should print

2.b(4.26)A palindrome is a number or a text phrase that reads the same

backward as forward. for example ,each of the following five-digit integers is a palindrome :12321,55555,45554 and 11611 . write a program that reads in a five-difit integer and determines whether it is a palindrome .[hint: use the division and modulus operators to separate the number into its individual digits.]

1 Answer

Rating
  • 大豬
    Lv 4
    1 decade ago
    Favorite Answer

    1.a(4.25)

    #include <iostream>

    #include <cstdlib>

    using namespace std;

    int main()

    {

    int num, i, j;

    cin >> num;

    if (1 <=num && num <=20)

    {

    for (j=1;j<=num;j++)cout << "*";

    cout << endl;

    for (i=2;i<=num-1;i++)

    {

    cout << "*";

    for (j=2;j<=num-1;j++)cout << " ";

    cout << "*" << endl;

    }

    if (num>1)

    {

    for (j=1;j<=num;j++)cout << "*";

    cout << endl;

    }

    }

    else cout << "輸入錯誤!" << endl;

    system("pause");

    return 0;

    }

    2.b(4.26)

    //C++

    #include <iostream>

    #include <cstdlib>

    using namespace std;

    int main()

    {

    char digits[5];

    int num=-1;

    cin >> num;

    if (10000 <=num && num <=99999)

    {

    int temp =num, i;

    for(i=0; i < 5; i++)

    {

    digits[i]=temp%10;

    temp=temp/10;

    }

    bool flag=true;

    for (i=0; i < 2; i++)

    if (digits[i]!=digits[4-i])

    { flag=false;

    break;

    }

    if (flag)

    cout << "It is a palindrome" << endl;

    else

    cout << "It is NOT a palindrome" << endl;

    }

    else cout << "輸入錯誤!" << endl;

    system("pause");

    return 0;

    }

Still have questions? Get your answers by asking now.