C语言Matrix编程题——[Pointers and C-String]D. Liang 7.19 Guessing the capitals

[Pointers and C-String]D. Liang 7.19 Guessing the capitals

Description:

There are 49 states and the corresponding capitals of USA:

State, Capital
Alabama, Montgomery
Alaska, Juneau
Arizona, Phoenix
Arkansas, Little Rock
California, Sacramento
Colorado, Denver
Connecticut, Hartford
Delaware, Dover
Florida, Tallahassee
Georgia, Atlanta
Hawaii, Honolulu
Idaho, Boise
Illinois, Springfield
Maryland, Annapolis
Minnesota, Saint Paul
Iowa, Des Moines
Maine, Augusta
Kentucky, Frankfort
Indiana, Indianapolis
Kansas, Topeka
Louisiana, Baton Rouge
Oregon, Salem
Oklahoma, Oklahoma City
Ohio, Columbus
North Dakota, Bismark
New York, Albany
New Mexico, Santa Fe
New Jersey, Trenton
New Hampshire, Concord
Nevada, Carson City
Nebraska, Lincoln
Montana, Helena
Missouri, Jefferson City
Mississippi, Jackson
Massachusettes, Boston
Michigan, Lansing
Pennslyvania, Harrisburg
Rhode Island, Providence
South Carolina, Columbia
South Dakota, Pierre
Tennessee, Nashville
Texas, Austin
Utah, Salt Lake City
Vermont, Montpelier
Virginia, Richmond
Washington, Olympia
West Virginia, Charleston
Wisconsin, Madison
Wyoming, Cheyenne

Write a program that read in state name and guess for its capital. Upon receiving each input pair, the program reports whether the answer is correct.

Input:

The first line is an integer n (0 with a string in each line.
Each odd line i contains a string STATE which is a name of a state, and line i+1 contains a string CAPITAL which is a guess for State.
Output
For each input line, you should judge whether CAPITAL is the capital of STATE.
If the answer is “yes”, output “Your answer is correct.”
If the answer is “no”, output the correct answer, “The capital of #state is #capital.” Fill in #state with STATE and #capital with the correct capital of STATE.

Sample Input:

2
Alabama
Phoenix
Alaska
Juneau

Sample Output:

The capital of Alabama is Montgomery
Your anwswer is correct

Programme:

//Date:2020/6/3
//Author:Kamenrider Justice
#include
#include
struct State_Capital
{
   char State[100];
   char Capital[100];
};
int main()
{
   struct State_Capital list[49]=//创建结构数组
   {
      {"Alabama","Montgomery"},
      {"Alaska","Juneau"},
      {"Arizona","Phoenix"},
      {"Arkansas","Little Rock"},
      {"California","Sacramento"},
      {"Colorado","Denver"},
      {"Connecticut","Hartford"},
      {"Delaware","Dover"},
      {"Florida","Tallahassee"},
      {"Georgia","Atlanta"},
      {"Hawaii","Honolulu"},
      {"Idaho","Boise"},
      {"Illinois","Springfield"},
      {"Maryland","Annapolis"},
      {"Minnesota","Saint Paul"},
      {"Iowa","Des Moines"},
      {"Maine","Augusta"},
      {"Kentucky","Frankfort"},
      {"Indiana","Indianapolis"},
      {"Kansas","Topeka"},
      {"Louisiana","Baton Rouge"},
      {"Oregon","Salem"},
      {"Oklahoma","Oklahoma City"},
      {"Ohio","Columbus"},
      {"North Dakota","Bismark"},
      {"New York","Albany"},
      {"New Mexico","Santa Fe"},
      {"New Jersey","Trenton"},
      {"New Hampshire","Concord"},
      {"Nevada","Carson City"},
      {"Nebraska","Lincoln"},
      {"Montana","Helena"},
      {"Missouri","Jefferson City"},
      {"Mississippi","Jackson"},
      {"Massachusettes","Boston"},
      {"Michigan","Lansing"},
      {"Pennslyvania","Harrisburg"},
      {"Rhode Island","Providence"},
      {"South Carolina","Columbia"},
      {"South Dakota","Pierre"},
      {"Tennessee","Nashville"},
      {"Texas","Austin"},
      {"Utah","Salt Lake City"},
      {"Vermont","Montpelier"},
      {"Virginia","Richmond"},
      {"Washington","Olympia"},
      {"West Virginia","Charleston"},
      {"Wisconsin","Madison"},
      {"Wyoming","Cheyenne"}
   };
   int n,i,j;
   char state[100],capital[100];
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
      scanf("%s",state);
      scanf("%s",capital);
      for(j=0;j<49;j++)
      {
         if(!strcmp(state,list[j].State))//注意比较函数strcmp,若相等返回的是0
         {
            if(!strcmp(capital,list[j].Capital))
            {
               printf("Your anwswer is correct");//答案这里的answer拼错了,所以只能按他的写
            }
            else 
            {
               printf("The capital of %s is %s\n",list[j].State,list[j].Capital);
            }
            break;
         }
         else
         {
            continue;
         }
      }
   }
   return 0;
}

Java面试Offer直通车

你可能感兴趣的:(C语言Matrix)