当前位置:网站首页>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;
}
边栏推荐
- The definition of strain was originally from stretch_ Ratio started
- Set of XXL job principles
- Ultra simple STM32 RTC alarm clock configuration
- Leetcode search insert location
- 【LeetCode】236. Nearest common ancestor of binary tree
- How does WPS cancel automatic numbering? Four options
- Rotating frame target detection mmrotate v0.3.1 training dota data set (II)
- Is it safe to open an account and trade with a compass?
- InputStream to inputstreamsource
- Rotating frame target detection mmrotate v0.3.1 learning configuration
猜你喜欢
![[deep learning] data segmentation](/img/16/798881bbee66faa2fb8d9396155010.jpg)
[deep learning] data segmentation

OSPF - authentication and load balancing summary (including configuration commands)

Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand

Leetcode search insert location

聲網,站在物聯網的“土壤”裏

Codeforces C. Andrew and Stones

8 ways to earn passive income

雲服務器部署 Web 項目

Inno setup the simplest user-defined interface effect

Use of OpenCL thread algebra library viennacl
随机推荐
What are membrane stress and membrane strain
Luogup2756 pilot pairing scheme problem (maximum flow)
[deep learning] data segmentation
Database SQL language 05 SQL exercise
Promise knowledge points
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
/Path/to/ idiom, not a command
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
Attempt to redefine 'timeout' at line 2 solution
Delete the repeating elements in the sorting list (simple questions)
Redistemplate common method summary
Inno setup the simplest user-defined interface effect
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
Learning about functions QAQ
旋转框目标检测mmrotate v0.3.1 学习配置
1380. lucky numbers in matrices
OpenCL线程代数库ViennaCL的使用
Leetcode56. consolidation interval
Use of tornado template
Database SQL language 06 single line function