比较大的数组因尽量声明再main函数外,否则程序可能无法运行
如果要从数组A复制K个元素到数组B,可以调用memcpy(b,a,sizeof (int) *K)
它包含在头文件 string.h
中
memset(a, 0 ,sizeof(a) )
在string.h
中定义,作用是清零数组a
“在多数情况下,最好在做一件事之前检查是不是可以做,而不是做完后悔。因为‘悔棋’往往比较麻烦。” 即编写程序时,先要进行的是分析,而不是直接做,最好能写下伪代码。磨刀不误砍柴功,这样做不仅仅能提高编写代码的速度,也能提高AC的正确率。
scanf("%s", s)
不能读入空格,TAB和回车。注意,注意:字符串s前面没有&。可以用scanf("%s",s[i])
读取第s[8][9]
中的第i
个字符串。sprintf(buf,"%d%d%d",a,b,c)
表达的意思:将整数 a b c 转换成字符并保存在字符数组buf
中。详情请移步keenleung
的博客《C++:sprintf()的用法strlen(s)
可以用于返回字符数组的“0\”之前的长度strcpy(a,b)
、strcmp(a,b)
和strcat(a,b)
注意,它们都在string.h
中声明。getchar()
等价于 fgetc(stdin)
,它们都能从缓冲区中读入空格、TAB和回车键。
fgets(buf,10,fin)
读取完整的一行,并放入字符数组buf
中,当一个字符都没有是,fegets
返回值为NULL。
常量数组的定义 char s [] = "1 2 3 4 5 s g e j o m";
又是常量数组可以达到意想不到的效果。
isalpha
函数可以用来判断是否为字母,它在ctype.h
中定义,在头文件ctype.h
中定义的还有isalpha
、isdigit
、isprint
函数,同样可以用来判断字符的属性,touppper
, tolower
工具可以用来转换大小写。
“字典序”:就是字符串在字典中的顺序。例如:“abc的字典序比bcd小”。
进位制:十进制是“逢十进一”,顾名思义,二进制就是“逢二进一”。
进制转换:
/**
* @author Miubai
* Score
*/
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 500
int main (){
int n;
char s[MAX];
cin >> n;
while ( n -- ){
int count = 0 ;
int sum = 0;
scanf("%s",s);
for (int i = 0;i < strlen(s);i++){
if(s[i] == ‘O‘) {
count++;
sum += count;
} else
count = 0;
}
cout << sum << endl;
}
return 0;
}
/**
* @author Miubai
* Digit Counting UVA - 1225
*/
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 10001
char s[MAX];
int count[10];
int main() {
int n,N;
cin >> N;
while (N--) {
memset(count, 0, sizeof(count));
cin >> n;
for (int i = 1; i <= n; i++) {
sprintf(s, "%d", i);
for (int j = 0; j < i; j++) {
switch (s[j]) {
case ‘0‘:
count[0]++;
break;
case ‘1‘:
count[1]++;
break;
case ‘2‘:
count[2]++;
break;
case ‘3‘:
count[3]++;
break;
case ‘4‘:
count[4]++;
break;
case ‘5‘:
count[5]++;
break;
case ‘6‘:
count[6]++;
break;
case ‘7‘:
count[7]++;
break;
case ‘8‘:
count[8]++;
break;
case ‘9‘:
count[9]++;
}
}
}
for (int i = 0; i <= 9; i++) {
cout << count[i] << " ";
}
cout << endl;
}
return 0;
}
笔者注:数数字(Digit Counting UVA - 1225)这题样例已经过了但是未能AC,笔者还未找出错误原因,望大佬指正。
原文:https://www.cnblogs.com/Miubai-blog/p/12803078.html