当前位置:网站首页>C language code record
C language code record
2022-06-30 05:52:00 【Pie Daxing's good friend Dingdang cat】
List of articles
Chinese questions ( Class B )
1001
#include <stdio.h>
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
Law 1 :
#include <stdio.h>
#include <string.h>
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] != '\0') {
// printf("%c\n", a[i]);
sum = sum + (a[i] - '0');
i++;
}
// printf("sum:%d\n", sum);
re[0] = sum / 100;
sum = sum % 100;
re[1] = sum / 10;
sum = sum % 10;
re[2] = sum;
// printf(" Three figures :%d %d %d\n", 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;
}
【 pit 】 \color{red}{【 pit 】} 【 pit 】 Character array definition
How to assign values to this array ?
1、 When defining, directly assign values with strings
char a[10]=“hello”;
Be careful : You can't define it before assigning it a value , Such as char a[10]; a[10]=“hello”; This is wrong !
2、 Assign values to the characters in the array one by one
char a[10]={‘h’,‘e’,‘l’,‘l’,‘o’};
3、 utilize strcpy
char a[10]; strcpy(a, “hello”);
easy wrong love condition : \color{red}{ Error prone situation :} easy wrong love condition :
1、char a[10]; a[10]=“hello”;// How can a character hold a string ? Besides, a[10] There is no such thing as !
2、char a[10]; a=“hello”;// This is easy to happen ,a Although it's a pointer , But it already points to the... Allocated in the stack 10 Character space , Now this situation a And points to... In the data area hello Constant , The pointer here a There's chaos , Don't allow !
also : Cannot use relational operator “==” To compare two strings , Only use strcmp() Function to handle .
Law two :
#include <stdio.h>
#include <string.h>
int main() {
// Read in characters and numbers
char input[105];
int len, i, sum;
sum = 0;
scanf("%s", input);
// Calculate the sum of the numbers
len = strlen(input);
for (i = 0; i < len; i++) {
sum += input[i] - '0';
}
// Chinese Pinyin
char change[15][6] = {
"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int a[5];
i = 0;
while (sum != 0) {
a[i] = sum % 10; //a[i]i=0 It's a bit
sum = sum / 10;
i++;
}
int j;
for (j = i - 1; j > 0; j--) {
printf("%s ", change[a[j]]);
}
printf("%s", change[a[0]]);
return 0;
}
Method 1 calculates the sum of bits in the case of position character length
Method 2 uses the function of character length to calculate the sum of the number of characters
Method 2: copy the character array , Directly assign values when defining
1003
#include <stdio.h>
#include <string.h>
int main() {
int n, i, j, flag;
char str[105];
scanf("%d", &n);
getchar();
int p, a, t;
int x, y, z, len;
for (i = 0; i < n; i++) {
p = t = 0;
flag = 1;
x = y = z = 0;
str[0] = '\0';
gets(str);
len = strlen(str);
for (j = 0; j < len; j++) {
if (str[j] != 'P' && str[j] != 'A' && str[j] != 'T') {
if (i != n - 1) {
printf("NO\n");
} else
printf("NO");
flag = 0;
break;
}
}
if (flag == 1) {
for (j = 0; j < len; j++) {
if (str[j] == 'P') {
x = j; //p Prior to j individual a
p++;
}
if (str[j] == 'T') {
y = j - x - 1;
t++;
}
}
z = len - x - y - 2;
} else {
continue;
}
// printf("x:%d y:%d z:%d\n", x, y, z);
if (z == x * y && p == 1 && t == 1 && y >= 1) {
if (i != n - 1) {
printf("YES\n");
} else
printf("YES");
} else {
if (i != n - 1) {
printf("NO\n");
} else
printf("NO");
}
}
return 0;
}
边栏推荐
- 旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
- Leader: who can use redis expired monitoring to close orders and get out of here!
- Basic operations of C language
- Solitidy - fallback 回退函数 - 2种触发执行方式
- Rotating frame target detection mmrotate v0.3.1 learning configuration
- UE4_ Editor UMG close window cannot destroy UMG immediately
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
- Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
- MySQL数据库用户管理
- 09- [istio] istio service entry
猜你喜欢

Projet Web de déploiement du serveur Cloud

Codeforces C. Andrew and Stones

Vfpbs uploads excel and saves MSSQL to the database
![[GPU] basic operation](/img/76/6b22368e3addd30aef1dd2258ee49a.jpg)
[GPU] basic operation

Here comes the nearest chance to Ali

Leetcode56. consolidation interval

Cisco vxlan configuration

强烈推荐十几款IDEA开发必备的插件

MySQL事物

Dynamic programming -- gliding wing of the strange thief Kidd
随机推荐
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
STM32F103 series controlled OLED IIC 4-pin
The average salary of software testing in 2022 has been released. Have you been averaged?
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
Xi'an Jiaotong automation control theory test simulation question [standard answer]
Vfpbs uploads excel and saves MSSQL to the database
Who is promoting the new inflection point of audio and video industry in 2022?
Answer sheet for online assignment of "motor and drive" of Xijiao 21 autumn (IV) [standard answer]
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
MySQL高级SQL语句
We strongly recommend more than a dozen necessary plug-ins for idea development
Xctf attack and defense world crypto advanced area
Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
After getting these performance test decomposition operations, your test path will be more smooth
About modifying dual system default startup item settings
强烈推荐十几款IDEA开发必备的插件
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
After reading who moved my cheese
STM32F103系列控制的OLED IIC 4针