/// 找出m到n之间的所有同构数(m < n)
/// 一个正整数x,如果是它平方数的尾部,则称x为同构数
/// 例如,6是其平方数36的尾部,25是其平方数625的尾部,那么6和25都是同构数
/// </summary>
/// <input>m和n两个正整数,用空格分隔</input>
/// <output>连续输出同构数,数据间用空格分隔</output>
/// <sample>
/// 5 50
/// 5 6 25
/// </sample>
/// <author>Cui Shuning (崔舒宁)</author>
/// <difficulty>02</difficulty>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int IsomorphicNumber(int x, int xx);
int main()
{
int m, n;
(void)scanf("%d%d", &m, &n);
int firstNum = 0;
for (int i = m; i <= n; i++)
{
if (IsomorphicNumber(i, i * i))
{
if (firstNum == 0)
{
printf("%d", i);
firstNum = 1;
}
else
{
printf(" %d", i);
}
}
}
return 0;
}
int IsomorphicNumber(int x, int xx)
{
while (x && xx)
{
if (x % 10 != xx % 10)
{
return 0;
}
x = x / 10;
xx = xx / 10;
}
return 1;
}
判断一个数的位数t
//判断i的位数
int t=(int)log10(i)+1;
符号变化怎么写:
int sign=-1;
sign=-1*sign;//正负号反转
冒泡排序例题:
定义包含5个英文单词的字符数组(即二维字符数组),
键盘输入5个单词,按字典顺序寻找最大和最小单词并输出。
输入:5个单词
输出:最大和最小单词(按字典序)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
void BubbleWord(char word[][100], int n);
int main()
{
char word[5][100];
for (int i = 0; i < 5; i++)
{
(void)scanf("%s", word[i]);
}
BubbleWord(word, 5);
printf("max:%s min:%s\n", word[4], word[0]);
return 0;
}
/// <summary>
/// 对单词的冒泡排序
/// </summary>
/// <param name="word">待排序的数组</param>
/// <param name="n">个数</param>
void BubbleWord(char word[][100], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = n - 1; j > i; j--)
{
if (strcmp(word[j], word[j - 1]) < 0)
{
char temp[100];
strcpy(temp, word[j]);
strcpy(word[j], word[j - 1]);
strcpy(word[j - 1], temp);
}
}
}
}
一个判断是否是闰年的函数
/// 输入公元年份和月份,输出该月份的天数。
/// </summary>
/// <input>年,月(逗号分隔)</input>
/// <output>天数</output>
/// <sample>
/// 2000,2
/// 29
/// </sample>
/// <sample>
/// 2001,2
/// 28
/// </sample>
/// <author>Cui Shuning (崔舒宁)</author>
/// <difficulty>02</difficulty>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int IsLeapyear(int year);
int main()
{
//一年中每月的天数
//Number of days in each month of the year
const int days[12] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
int year, month;
(void)scanf("%d,%d", &year, &month);
if (month == 2)
{
printf("%d", IsLeapyear(year) ? 29 : 28);
}
else
{
printf("%d", days[month - 1]);
}
return 0;
}
/// <summary>
/// 判断是否为闰年
/// </summary>
/// <param name="year">年份</param>
/// <returns>是,返回1,否,0</returns>
int IsLeapyear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
一个加密字符的函数,具体见代码
///<summary>
/// 对字母x,用字母表中其后的第n个字母代替,
/// 不够n个时再从字母a循环计数
/// </summary>
/// <param name="x">替换的字母</param>
/// <param name="n">替换值</param>
/// <returns>替换后的字母,如果x不是字母,则返回x</returns>
char ShiftChar(char x, int n)
{
if ('A' <= x && x <= 'Z')
{
x = (x - 'A' + n) % 26 + 'A';
}
if ('a' <= x && x <= 'z')
{
x = (x - 'a' + n) % 26 + 'a';
}
return x;
}
一个查找字符串中是否有这个符号
/// <summary>
/// 查找一个字符是否在一个串中
/// </summary>
/// <param name="str">查找的字符串</param>
/// <param name="ch">查找的字符</param>
/// <returns>如找到返回下标(从0开始,返回第一个找到的下标)没有,返回-1</returns>
int Search(char* str, char ch)
{
int i = 0;
while (str[i])
{
if (str[i] == ch)
{
return i;
}
i++;
}
return -1;
}
降序排列
/*输入字符串(长度不超过200,不包含空格,至少有1个字符),
*除首尾字符外,将其余的字符按ascii码降序排列。
*
* Cui Shuning (崔舒宁)2020/11
*/
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4267)
#include<stdio.h>
#include<string.h>
void StrBubbleBetween(char* str, int begin, int end);
int main()
{
char inStr[200] = { 0 };
(void)scanf("%s", inStr);
StrBubbleBetween(inStr, 1, strlen(inStr) - 2);
printf("%s", inStr);
return 0;
}
void StrBubbleBetween(char* str, int begin, int end)
{
for (int i = begin; i < end; i++)
{
for (int j = end; j > i; j--)
{
if (str[j] > str[j - 1])
{
char temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
}
}
}
}
删除函数模型
/// <ID>C096</ID>
/// <date>2020/11</date>
/// <summary>
/// 输入一个长度不超过 100 的字符串,删除串中的重复字符。
/// 输入要检查的字符串,长度不超过100个字符。例如:abacaeedabcdcd。
/// 删除重复字符后的字符串。例如:abced。
/// </summary>
/// <input>一个字符串</input>
/// <output>删除重复字符后的字符串</output>
/// <sample>
/// abacaeedabcdcd
/// abced
/// </sample>
/// <author>Cui Shuning (崔舒宁)</author>
/// <difficulty>03</difficulty>
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
char* DelChar(char* str, char ch);
int main()
{
char str[100];
(void)scanf("%s", str);
int i = 0;
while (str[i])
{
DelChar(str + i + 1, str[i]);
i++;
}
printf("%s", str);
return 0;
}
/// <summary>
/// 将字符串中指定的字符删除
/// </summary>
/// <param name="str">待处理的字符串</param>
/// <param name="ch">需要删除的字符</param>
/// <returns>删除后的字符串</returns>
char* DelChar(char* str, char ch)
{
int i = 0;
while (str[i])
{
if (str[i] == ch)
{
int j = 0;
while (str[i + j + 1])
{
str[i + j] = str[i + j + 1];
j++;
}
str[i + j] = 0;
}
else
{
i++;
}
}
return str;
}