C语言程序设计:现代设计方法习题笔记《chapter4》

第一题

示例代码:

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a two-digit number: ");
	int number,ten_n,g_n;
	scanf_s("%d", &number);
	ten_n = number / 10;
	g_n = number % 10;
	printf("The reversal is %d%d", g_n, ten_n);
	return 0;
}

输出:

​​​​​​​

第二题

​​​​​​​ ​​​​​​​

解题思路:这道题的解题思路有很多,一种思路就是分别求出每个位上的数字,然后排序输出,不难给出代码。

示例代码:

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a two-digit number: ");
	int number, hund_n, ten_n, g_n;
	scanf_s("%d", &number);
	hund_n = number / 100;
	ten_n = (number-hund_n*100) / 10;
	g_n = (number-hund_n*100) % 10;
	//g_n = number-hund_n*100-ten_n*10;
	printf("The reversal is %d%d%d", g_n, ten_n, hund_n);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

第三题

解题思路:%1d代表一位整数,所以输入的数字是按照个数组合起来的,而不是眼睛看起来通常意义上的几百,如此可以解答。

示例代码:

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a two-digit number: ");
	int  hund_n, ten_n, g_n;
	scanf_s("%1d", &hund_n);
	scanf_s("%1d", &ten_n);
	scanf_s("%1d", &g_n);
	
	//g_n = number-hund_n*100-ten_n*10;
	printf("The reversal is %d%d%d", g_n, ten_n, hund_n);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​

第四题

解题思路:根据题中提示,需要用累除连续除4次8,然后将余数排列出来即可。

示例代码

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a number betweeen 0 and 32767: ");
	int number;
	scanf_s("%d", &number);
	int a, b, c, d, e;
	int temp_y, temp_r;
	temp_y = number % 8;
	a = temp_y;

	temp_r = number / 8;
	b = temp_r % 8;

	temp_r = temp_r / 8;
	c = temp_r % 8;

	temp_r = temp_r / 8;
	d = temp_r % 8;

	temp_r = temp_r / 8;
	e = temp_r % 8;
	printf("%d%d%d%d%d", e, d, c, b, a);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​

第五题

题目分析:这个题不难,关键在于审题,计算规则要看清楚,别搞错了。

示例代码

cpp 复制代码
#include<stdio.h>
int main()
{
	int b, a1, a2, a3, a4, a5, b1, b2, b3, b4, b5;
	printf("Enter the first 11 digits of UPC: ");
	scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&b, &a1, &a2, &a3, &a4, &a5, &b1, &b2, &b3, &b4, &b5);
	int result;
	int first_sum,second_sum, total;
	first_sum = b + a2 + a4 + b1 + b3 + b5;
	second_sum = a1 + a3 + a5 + b2 + b4;
	total = 3 * first_sum + second_sum;
	result = 9 - (total - 1) % 10;
	printf("Check digit: %d", result);
	return 0;
}

输出

​​​​​​​ ​​​​​​​

第六题

题目分析:这种长臭的题,耐心看,提取出有用信息转换为代码语言。

示例代码

cpp 复制代码
#include<stdio.h>
int main()
{
	int a1, a2, a3, a4, a5,a6, b1, b2, b3, b4, b5, b6;
	printf("Enter the first 11 digits of UPC: ");
	scanf_s("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%d", &a1, &a2, &a3, &a4, &a5,&a6, &b1, &b2, &b3, &b4, &b5,&b6);
	int result;
	int first_sum, second_sum, total;
	first_sum = a6 + a2 + a4 + b2 + b4 + b6;
	second_sum = a1 + a3 + a5 + b1 + b3+b5;
	total = 3 * first_sum + second_sum;
	result = 9 - (total - 1) % 10;
	printf("Check digit: %d", result);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

相关推荐
wei_shuo16 小时前
从数据中台到数据飞轮:实现数据驱动的升级之路
1024程序员节·数据飞轮
玖剹14 天前
矩阵区域和 --- 前缀和
数据结构·c++·算法·leetcode·矩阵·动态规划·1024程序员节
jamison_11 个月前
文心一言与 DeepSeek 的竞争分析:技术先发优势为何未能转化为市场主导地位?
人工智能·ai·chatgpt·gpt-3·1024程序员节
NaZiMeKiY1 个月前
HTML5前端第六章节
前端·html·html5·1024程序员节
jamison_12 个月前
颠覆未来:解锁ChatGPT衍生应用的无限可能(具体应用、功能、付费模式与使用情况)
ai·chatgpt·1024程序员节
NaZiMeKiY2 个月前
HTML5前端第七章节
1024程序员节
earthzhang20212 个月前
《Python深度学习》第四讲:计算机视觉中的深度学习
人工智能·python·深度学习·算法·计算机视觉·numpy·1024程序员节
明明真系叻2 个月前
2025.3.2机器学习笔记:PINN文献阅读
人工智能·笔记·深度学习·机器学习·1024程序员节·pinn
bitenum2 个月前
【C++/数据结构】队列
c语言·开发语言·数据结构·c++·青少年编程·visualstudio·1024程序员节
IT学长编程3 个月前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节