c语言刷题代码记录

c语言刷题代码记录,第1张

c语言刷题代码记录

文章目录
  • 中文题(乙级)
    • 1001
    • 1002

题目网址

中文题(乙级) 1001
#include 

int main() {
	int n, cnt;
	cnt = 0;
	scanf("%d", &n);
	while (n != 1) {
		if (n % 2 == 1) {
			n = (3 * n + 1) / 2;
		} else {
			n = n / 2;
		}
		cnt ++;
	}
	printf("%d", cnt);
	return 0;
}
1002
#include 
#include 

int main() {
	int sum, i;
	sum = 0;
	i = 0;
	int re[5];
	char a[200], s[20][10];
	strcpy(s[0], "ling");
	strcpy(s[1], "yi");
	strcpy(s[2], "er");
	strcpy(s[3], "san");
	strcpy(s[4], "si");
	strcpy(s[5], "wu");
	strcpy(s[6], "liu");
	strcpy(s[7], "qi");
	strcpy(s[8], "ba");
	strcpy(s[9], "jiu");
	scanf("%s", a);
	while (a[i] != '') {
//		printf("%cn", a[i]);
		sum = sum + (a[i] - '0');
		i++;
	}
//	printf("sum:%dn", sum);
	re[0] = sum / 100;
	sum = sum % 100;
	re[1] = sum / 10;
	sum = sum % 10;
	re[2] = sum;
//	printf("三位数:%d %d %dn", re[0], re[1], re[2]);
	if (re[0] != 0) {
		printf("%s %s %s", s[re[0]], s[re[1]], s[re[2]]);
	} else {
		if (re[1] != 0) {
			printf("%s %s", s[re[1]], s[re[2]]);
		} else {
			printf("%s", s[re[2]]);
		}
	}
	return 0;
}

【 坑 】 color{red}{【坑】} 【坑】 字符数组定义
怎么给这个数组赋值呢?
1、定义的时候直接用字符串赋值
char a[10]=“hello”;
注意:不能先定义再给它赋值,如char a[10]; a[10]=“hello”;这样是错误的!
2、对数组中字符逐个赋值
char a[10]={‘h’,‘e’,‘l’,‘l’,‘o’};
3、利用strcpy
char a[10]; strcpy(a, “hello”);
易 错 情 况 : color{red}{易错情况:} 易错情况:
1、char a[10]; a[10]=“hello”;//一个字符怎么能容纳一个字符串?况且a[10]也是不存在的!
2、char a[10]; a=“hello”;//这种情况容易出现,a虽然是指针,但是它已经指向在堆栈中分配的10个字符空间,现在这个情况a又指向数据区中的hello常量,这里的指针a出现混乱,不允许!
还有:不能使用关系运算符“==”来比较两个字符串,只能用strcmp() 函数来处理。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5097837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存