【NYOJ】[1096]n-1位数

文章字数:289

问题描述

问题分析

用长整数储存也可以 但是涉及到统计位数比较麻烦
所以还是用字符串来模拟比较简便

用strlen来计算位数
用flag来标记在输出0之前是否已经输出过数字
用这种方法来去除前缀0
然后在结束后
检查一遍是否输出过数字
没有的话就输出一个 0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<string.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		char s[10];
		scanf("%s",s);
		int flag=0;
		for(int i=1; i<strlen(s); i++) {
			if(s[i]!='0')
				flag=1;
			if(flag) {
				printf("%c",s[i]);
			}
		}
		if(!flag)
			printf("0");
		printf("\n");
	}
	return 0;
}

标程有点打脸了……
一种乍一看挺简单 仔细看看不懂的感觉

据说%*c代表跳过一个变量
那么这里就应该代表输入时跳过第一个数字吧
以前的确是没听说过这种写法
C语言还是要好好学啊~

1
2
3
4
5
6
7
8
9
#include<cstdio>
int main() {
	int n,m;
	scanf("%d",&n);
	while(n--) {
		scanf("\n%*c%d",&m);
		printf("%d\n",m);
	}
}

题目地址:n-1位数

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...