XJTU University Computer Programming Assignment Week 9

first question

Title Description:
Write a program to encode a string of 4 characters into ciphertext using replacement encryption. The encryption rule is: replace the original letter with the third letter in the alphabet. Note that the last three characters are replaced with the first three, such as x replaced with a.

Input and output format:
input: 4 characters
output: 4 characters

Sample input:

ABcx

Sample output:

DEfa

样例代码

#include <stdio.h>
int main()
{
  char a[5];
  scanf("%s",a);
  for(int i=0;i<4;i++)
  {
    if(a[i]>=120||(a[i]>=88&&a[i]<=90))
    {
      if(a[i]==120)a[i]=97;
      if(a[i]==121)a[i]=98;
      if(a[i]==122)a[i]=99;
      if(a[i]==88)a[i]=65;
      if(a[i]==89)a[i]=66;
      if(a[i]==90)a[i]=67;
    }
    else
  		a[i]=a[i]+3;
  }
  printf("%s",a);
  return 0;
}

The second question

Title description:
Leap year calculation. The program inputs a positive integer Y and another positive integer N, separated by a space. Calculate the year of the Nth leap year after Y (if Y itself is a leap year, then the first leap year after Y is Y).


Program input:
YN

Program output:
N-th leap year after Y year begins

Sample input:

2005 3

Sample output:

2016

参考代码

#include<stdio.h>
int main()
{
       int y, n;
       scanf_s("%d %d", &y, &n);     //%d%d默认了两个整数之间有空格
       for (y; n > 0; y++)     //此法相较法二避免了分类讨论
       {
              if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                      n--;
       }
       printf("%d", y-1);     //之所以是输出y-1,是因为当n减到1并且此时y对应的是闰年,n会--,但此时y++仍会执行;要是不减1而输出y,得到的结果每次都是正确结果+1的年份
       //不合法语句//system ("pause");
       return 0;
}

Note: Leap year judgment is a fixed code, need to understand:

 if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)//判断闰年的语句

The third question

Method diversity
Title Description:
There is an elevator in the tallest building in a city, and the elevator runs in the order in which the number of floors is entered. The elevator was originally on the 0th floor. After running an input sequence, it stops at this floor and does not return to floor 0. Write a program to calculate the time for the elevator to run a sequence. It takes 6 seconds for each elevator to go up. Each next level takes 4 seconds. If staying on a certain floor, no matter how many people get on or off, stay for 5 seconds.

Program input:
the running sequence of the elevator. The value of the floor is greater than or equal to 1, less than 100, and 0 in the sequence indicates the end of the sequence input. For example, if the input sequence is 2 1 0, it means that the elevator will first rise from the 0th floor to the 2nd floor and then descend to the 1st floor.

Program output: the
running time of each sequence elevator (seconds)

Sample input:

2 1 0

Sample output:

26

参考代码

#include <stdio.h>
int main()
{
	int current_floor;
	int next_floor;
	int sum;
	current_floor = 0;
	sum = 0;
	int diff;
	scanf_s("%d", &next_floor);
	while (next_floor != 0)
	{
		diff = next_floor - current_floor;
		if(diff>0)
		{
			sum = sum + diff * 6;
		}
		if (diff<0)
		{
			sum = sum + (-diff) * 4;
		}
		sum = sum + 5;
		current_floor = next_floor;
		scanf_s("%d", &next_floor);//循环输入next_floor直到next_floor==0	
	}
	printf("%d", sum);
	return 0;
}

Fourth question

Method diversity
Title Description:
Write a program. When entering a string, it is required to not only count the number of characters in it, but also indicate the number of upper and lower case letters, numbers, and other characters.

Program input:
the character string to be counted

Program output: The
five values ​​are uppercase, lowercase letters, numbers, other characters, and the total number of characters, separated by spaces

Sample input:

I enjoyed reading these books very much

Sample output:

1 32 0 6 39

参考代码

#include <stdio.h>
#include <string.h>
int main()
{
  char a[100];
  gets(a,100);//用gets也行,100表示缓冲区大小(可去)
  int i;
  i=strlen(a);
  int total=0,num=0,cap=0,small=0,other=0;
  for(int j=0;j<=i;j++)
  {
    if(a[j]=='\0')break;
    if(a[j]>='A'&&a[j]<='Z')cap++;
  	else if(a[j]>='a'&&a[j]<='z')small++;
    else if(a[j]>='0'&&a[j]<='9')num++;
    else other++;
    total++;
  }
  printf("%d %d %d %d %d",cap,small, num,other,total);
  return 0;
}

Question 5

Variety of problem-solving methods
Title description:
Please enter a positive integer (eg: 7654321), and then output the integer according to the standard three-digit section format (comma is a comma in Spanish, such as 7,654,321)

** Program input: *
Positive integer to be segmented

Program output:
positive integer after subsection

Sample input:

3456789

Sample output:

3,456,789

参考代码

#include <stdio.h>
int main()
{
    int a,i;
    char s[30] = "";
    scanf("%d",&a);
    i = 0;
    while(1) 
    {
        s[i] = a%10 + '0';
        a = a/10;
        if(a==0) 
	    {
			break;
		}
        i++;
    }//数位分离
    printf("%c",s[i--]);
    for(i;i>=0;i--)
    { 
        if((i+1)%3== 0) /* i+1是是单个数字在数字字符串的位置,因为上面i是从0开始的*/
        {
            printf(",%c",s[i]);
        }
        else
        {
            printf("%c",s[i]);
        }
    }
 
    return 0;
}

Note: code with digital separation

	int a,s[100];
	int i=0;
    while(1) 
    {
        s[i] = a%10 + '0';
        a = a/10;
        if(a==0) 
	    {
			break;
		}
		i++;
    }//数位分离

Question 6

Title description:
Enter an integer n (n> = 2) to determine whether n is a prime number. If it is a prime number, it outputs "YES", otherwise it outputs "NO".

Program input:
integer n

Program output:
output "YES" for prime numbers, otherwise output "NO".

Sample input:

7

Sample output:

YES

参考代码

#include <stdio.h>
int main()
{
    int n, x;
    scanf("%d", &n);
    x = 0;
    for (int i = n; i > 0; i--)
    {
        if (n % i == 0)x++;
    }
    if (x == 2)printf("YES");
    else printf("NO");
    return 0;
}

Question 7

Topic description:
Enter a positive integer (> 1) from the keyboard, and then decompose the integer into 1 and multiply each prime factor. If the entered integer is itself a prime number, it should be decomposed into 1 and the number itself is multiplied.

Input and output format:

Input: 1 integer
output: factorization, factor output from small to large. For example: 1 2 2 * 3

Sample input:

18

Sample output:

1*2*3*3

参考代码

#include <stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1;i<=n;i++)
        if (n % i == 0&&i!=n) 
        { 
            n = n / i; 
            printf("%d*", i);
            i = 1; 
        }
        else if(n%i==0&&i==n)
        {
            printf("%d", i);
            break;
        }
    
    return 0;
}

Published 3 original articles · praised 3 · visits 576

Guess you like

Origin blog.csdn.net/paul000917/article/details/105542072